GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Feb-2004, 21:22
tru504187211 tru504187211 is offline
New Member
 
Join Date: Feb 2004
Location: Milwaukee, WI
Posts: 2
tru504187211 is on a distinguished road

converting binary to integer...


Hello all!
This being my first post at this forum, let me say I hope I can get the assistance that I need to complete this programming portion of my senior seminar project of the bachelors degree for Computer Science.

On to my question...
I found some code that I was going to mod to use for my program and I have a few questions about this code that I found.

This is what I've got so far....
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
int main ()

unsigned int creatnum (int *num)
{
        int i;
        unsigned int a=0;
        for(i=0;i<32;i++)
        {
        a= a|num[i];
        if(i!=31)      a <<=1;
        }
        return a;
}  
The error I keep getting states that I need a semicolon on the line beginning with "unsigned in creatnum..." My other question is...what exactly is "unsigned int creatnum (int *num)" meaning? I have a general idea about unsigned ints and that, but what exactly is this entire line saying - especially the code in the paranthesis? I cannot find the problem. Am I missing a type definition or what?? I am stumped! Thankyou in advance for any assitance anybody can give me.
Last edited by dsmith : 09-Feb-2004 at 21:40. Reason: Added C syntax highlighting
  #2  
Old 09-Feb-2004, 21:45
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Your problem is with the line previous to that one and it is actually not needed.

Try to comment it out first as so:
CPP / C++ / C Code:
//int main()
or add a semicolon
CPP / C++ / C Code:
int main();

The reason that it is not needed is that it is a function definition, but main is always included in c so it does not need to be defined. However, if it is defined here it needs a semicolon after it.

Quote:
what exactly is "unsigned int creatnum (int *num)" meaning? I have a general idea about unsigned ints and that, but what exactly is this entire line saying - especially the code in the paranthesis?

This is fine. It is simply defining that the return value of the function is an unsigned int and that it needs a pointer to an integer passed to it.

BTW- where did you come up with your forum name? Thats alot of numbers!
  #3  
Old 09-Feb-2004, 22:39
tru504187211 tru504187211 is offline
New Member
 
Join Date: Feb 2004
Location: Milwaukee, WI
Posts: 2
tru504187211 is on a distinguished road
ah yes...THANKYOU SO MUCH. Got that BS message out of the way...and led to another...the inevitable "link error: undefined symbol: _main in maincrt.c"...aww nuts.

The numbers in my name? TRU is the name of my favorite rap group (master p, silkk the shocker and c-murder), 504 is the name of another favorite group (also composed partially of master p), 211 is the police code for armed robbery, and 187 is the code for homicide...more than you expected? lol, Thanx again...if you know what this link error is lemme know, but I've never actually solved a link error before!
  #4  
Old 10-Feb-2004, 07:11
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Okay, I think I understand your original post a little more. The code snippet that you downloaded is a function. It does absolutely nothing unless called from another function.

As I explained earlier, main is the entry function. This is basically where your program starts from and then it calls other functions as needed. C is basically just a grouping of functions that call each other. You can do everything in the main function if you want, but not only is this hard to track & read, it becomes impossible for bigger programs.

So basically, you need to write your own main function. For example:
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>

unsigned int creatnum (int *num)
{
        int i;
        unsigned int a=0;
        for(i=0;i<32;i++)
        {
        a= a|num[i];
        if(i!=31)      a <<=1;
        }
        return a;
}

int main(){
   unsigned int answer;
   int number;

   number = 500;
   answer = creatnum(&number);
   return 0;
}

Thats a pretty stupid main function, but it should compile and you can take it from there.
 
 

Recent GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:30.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.