Computer Science Canada Problem with functions |
Author: | JR [ Thu Sep 30, 2004 4:10 pm ] | ||
Post subject: | Problem with functions | ||
Well i gotta do some prog which has 3 options subtraction,addition,multipication. Now it has to choose random numbers from 1-100 and have 3 tries to answers. then if after the last try it is not guesses it tells the answer... now i get some erros with the functions, my program is not completed yet. well check what i did wrong.
|
Author: | wtd [ Thu Sep 30, 2004 4:22 pm ] | ||||||||||
Post subject: | |||||||||||
Not sure what you're trying to accomplish here. Do you want to assign to a and b, or are you testing to see if they're equal to rand() % 100? I'm guessing it's the second, but you need an if ... else construct or those tests won't actually do anything. As for your other functions, there's no need to declare an intermediate "r" variable.
Simply becomes:
Tip for this post: Use spaces! Don't type:
Instead, type:
Much easier to read. |
Author: | JR [ Thu Sep 30, 2004 4:25 pm ] |
Post subject: | |
i'm trying to make the thing to make a and random number between 1-100 and b the same.. |
Author: | wtd [ Thu Sep 30, 2004 4:34 pm ] |
Post subject: | |
Sorry. Didn't quite understand that last bit. Do you want to create a random number (from 1 to 100) and see if it's the same as "a"? Or do you want to make "a" a random number between 1 and 100? |
Author: | JR [ Thu Sep 30, 2004 6:18 pm ] |
Post subject: | |
i want to make a & b random nums from 1-100 |
Author: | wtd [ Thu Sep 30, 2004 6:49 pm ] | ||||||
Post subject: | |||||||
JR wrote: i want to make a & b random nums from 1-100
In this case you want to be passing in references to a and b. Normally arguments to functions in C++ are passed by value. This means that the value of the variable gets passed in just fine. However, any changes to that value have no effect outside the function. Consider:
You still see 4 appear as the output. This is because the variable itself isn't given to the function, but just the value of that variable. By passing an argument by reference, we change that, so that the function actually gets the entire variable.
This actually prints 5. The little & symbol following the type specified that the argument was a reference to an int variable, rather than just an int value. Another way This may contradict the requirements of the assignment, but it's good to know. Of course, procedures like this, while important to understand, are often better replaced by functions. If all we want to do is assign a random value between 1 and 100 to an int variable, we can simply create a function which returns a random int between 1 and 100.
|
Author: | JR [ Thu Sep 30, 2004 7:57 pm ] | ||
Post subject: | |||
ok i changed the prog and the errors were things thx, now i got a different problem if i add the srand(time(0)) then i get random numbers but they are the same like 2+2, 89+89. now i removed it and then i only get one sec of numbers all the time... heres the code
|
Author: | wtd [ Thu Sep 30, 2004 8:16 pm ] |
Post subject: | |
A note on passing arguments by reference. You only need to do this if you want to change the variable you're passing in. If you're not changing the variables, you should pass them by value. As for srand()... you should seed your random number generator once. Do this at the beginning of your main() function. |
Author: | JR [ Thu Sep 30, 2004 8:31 pm ] | ||
Post subject: | |||
ok i changed the code so it can be eaiser to use. but now i get 10 errors most of them say that there are too few arguments in the add, subtract, multiply functions. As for the scores i'm gonna add them at the end.
|
Author: | wtd [ Thu Sep 30, 2004 8:54 pm ] |
Post subject: | |
Wht's the first error? Always solve the first error first. |
Author: | JR [ Thu Sep 30, 2004 8:55 pm ] |
Post subject: | |
c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp: In function `int main()': c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp:44: too few arguments to function `int add(int &, int &)' c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp:82: at this point in file c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp:83: `a' undeclared (first use this function) c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp:83: (Each undeclared identifier is reported only once c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp:83: for each function it appears in.) c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp:83: `b' undeclared (first use this function) c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp:51: too few arguments to function `int subtract(int &, int &)' c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp:87: at this point in file c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp:58: too few arguments to function `int multiply(int &, int &)' c:\docume~1\johnny~1\desktop\c__stu~1\a5\a5_ar.cpp:92: at this point in file |
Author: | wtd [ Thu Sep 30, 2004 9:06 pm ] | ||
Post subject: | |||
In order for me to help, I need to see the exact code you're trying to compile, as well as the errors. I tried to match it against the code you showed me before, but that didn't make any sense. I'll give you one hint, though. If you define a function as taking two arguments:
Then when you call it, you have to provide two arguments. |
Author: | JR [ Thu Sep 30, 2004 9:14 pm ] | ||
Post subject: | |||
heres the code:
and here are the errors 44- too few arguments to function `int add(int, int)' at this point in file 82- ma' undeclared (first use this function) 83- (Each undeclared identifier is reported only once for each function it appears in.) 83- b' undeclared (first use this function) 51-too few arguments to function `int subtract(int, int)' 87- at this point in file 58- too few arguments to function `int multiply(int, int)' 92- at this point in file 82 -at this point in file 83- `a' undeclared (first use this function) 83 (Each undeclared identifier is reported only once for each function it appears in.) 83 -`b' undeclared (first use this function) 51 -too few arguments to function `int subtract(int, int)' i know i need to put 2 vars in the functions cause it gives me 2. |
Author: | wtd [ Thu Sep 30, 2004 9:21 pm ] | ||
Post subject: | |||
You've defined add to take two ints. You're only giving it one here. |
Author: | Andy [ Thu Sep 30, 2004 10:18 pm ] |
Post subject: | |
wow... plz at least TRY to debug ur own code before posting it here... all of these errors are very obvious, you should be able to figure them out urself |
Author: | wtd [ Thu Sep 30, 2004 10:50 pm ] |
Post subject: | |
I was too nice to suggest that. |