Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Problem with functions
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
JR




PostPosted: 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.

code:

//Alex R
//A5
//September 30th


#include <iostream>
#include <stdlib.h>
#include <time.h>

//function for addition
int add (int a,int b)
{
int r;
srand(time(0));
a== rand() %  100;
b== rand () % 100;
r=a+b;
return(r);
}

//function for subtraction
int subtract (int a,int b)
{
int r;
r=a-b;
return (r);
}

//Function for multiplication
int multiply (int a,int b)
{
int r;
r=a*b;
return (r);
}




int main ()
{
int choise;
std::cout<<"Enter your choise"<<endl<<endl;
std::cout<<"1. Addition"<<std::endl<<"2. Subtraction"<<std::endl<<"3. Multiplication"<<std::endl<<endl;
std::cin>>choise;

int a1,s,m;
a1= add (a,b);
s= subtract(a,b);
m= multiply(a,b);

return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Sep 30, 2004 4:22 pm   Post subject: (No subject)

code:
a== rand() %  100;
b== rand () % 100;


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.

code:
int multiply(int a, int b)
{
   int r;
   r = a * b;
   return(r);
}


Simply becomes:

code:
int multiply(int a, int b)
{
   return a * b;
}


Tip for this post: Use spaces!

Don't type:

code:
r=a*b;


Instead, type:

code:
r = a * b;


Much easier to read.
JR




PostPosted: Thu Sep 30, 2004 4:25 pm   Post subject: (No subject)

i'm trying to make the thing to make a and random number between 1-100 and b the same..
wtd




PostPosted: Thu Sep 30, 2004 4:34 pm   Post subject: (No 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?
JR




PostPosted: Thu Sep 30, 2004 6:18 pm   Post subject: (No subject)

i want to make a & b random nums from 1-100
wtd




PostPosted: Thu Sep 30, 2004 6:49 pm   Post subject: (No 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:

code:
void foo(int bar)
{
   bar += 1;
}

int main()
{
   int baz = 4;
   foo(baz);
   std::cout << baz << std::endl;
}


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.

code:
void foo(int& bar)
{
   bar += 1;
}

int main()
{
   int baz = 4;
   foo(baz);
   std::cout << baz << std::endl;
}


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.

code:
int randInt1To100()
{
   return rand() % 100;
}

int main()
{
   int a = randInt1To100();
   int b = randInt1To100();
}
JR




PostPosted: Thu Sep 30, 2004 7:57 pm   Post subject: (No 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


code:

#include <iostream>
#include <stdlib.h>
#include <time.h>

//random function
static int random()
{

return rand() % 100;
}

//function for addition
static int add (int& a,int& b)
{

return(a + b);
}

//function for subtraction
static int subtract (int& a,int& b)
{

return (a - b);
}

//Function for multiplication
static int multiply (int& a,int& b)
{

return (a * b);
}




int main ()
{
int choise;
int answer;
int a=random();
int b=random();
int z;
std::cout<<"Enter your choise"<<endl<<endl;
std::cout<<"1. Addition"<<std::endl<<"2. Subtraction"<<std::endl<<"3. Multiplication"<<std::endl<<endl;
std::cin>>choise;

//if statments
if (choise==1)
{
z=add(a,b);
std::cout<<a<<" + "<<b<<"=";
std::cin>>answer;

//second if statment
if (answer==(z))
{
std::cout<<"Correct Answer"<<std::endl;
}
else
{
std::cout<<"Wrong answer, You got 2 tries left."<<std::endl;
}
}

cin.get();
cin.get();

return 0;
}
wtd




PostPosted: Thu Sep 30, 2004 8:16 pm   Post subject: (No 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.
Sponsor
Sponsor
Sponsor
sponsor
JR




PostPosted: Thu Sep 30, 2004 8:31 pm   Post subject: (No 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.


code:

#include <iostream>
#include <stdlib.h>
#include <time.h>

//random function
static int random()
{

return rand() % 100;
}


//Answer calculation function
static void answers(int& a,int& b, int& total)
{
int answer;
int ctr=0;

if (ctr<3){

std::cout<<a<<" * "<<b<<" = ";
std::cin>>answer;

if (answer!=total){
std::cout<<"Wrong Try agian "<<std::endl;
ctr++;
}
else if (answer==total){
std::cout<<"You Are correct "<<std::endl;
}
}
else{
std::cout<<"You have missed 3 times. The answer is "<<total<<std::endl;
}
}

//function for addition
static int add (int& a,int& b)
{
return(a + b);
}


//function for subtraction
static int subtract (int& a,int& b)
{

return (a - b);
}

//Function for multiplication
static int multiply (int& a,int& b)
{

return (a * b);
}




int main ()
{
//seeding the random number
srand(time(0));

int total,choice;

//Menu
std::cout<<"Enter your choice" <<std::endl<<std::endl;
std::cout<<"1. Addition "<<std::endl;
std::cout<<"1. Subtraction "<<std::endl;
std::cout<<"1. Multiplication "<<std::endl;
std::cin>>choice;
//swtich case start
switch (choice){
//if choice is 1, first case
case 1: add(total);
answers(a,b,total);
break;

//if choice is 2, second case
case 2:subtract(total);
answers(a,b,total);
break;

//if choice is 3, third case
case 3:multiply(total);
answers(a,b,total);
break;
default: std::cout<<"You need to make a choice of 1, 2 or 3. "<<std::endl;
break;
}



cin.get();
cin.get();

return 0;
}

wtd




PostPosted: Thu Sep 30, 2004 8:54 pm   Post subject: (No subject)

Wht's the first error?

Always solve the first error first.
JR




PostPosted: Thu Sep 30, 2004 8:55 pm   Post subject: (No 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
wtd




PostPosted: Thu Sep 30, 2004 9:06 pm   Post subject: (No 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:

code:
void foo(int bar, int baz) { ... }


Then when you call it, you have to provide two arguments.
JR




PostPosted: Thu Sep 30, 2004 9:14 pm   Post subject: (No subject)

heres the code:

code:




#include <iostream>
#include <stdlib.h>
#include <time.h>

//random function
static int random()
{

return rand() % 100;
}


//Answer calculation function
static void answers(int a,int b, int total)
{
int answer;
int ctr=0;

if (ctr<3){

std::cout<<a<<" * "<<b<<" = ";
std::cin>>answer;

if (answer!=total){
std::cout<<"Wrong Try agian "<<std::endl;
ctr++;
}
else if (answer==total){
std::cout<<"You Are correct "<<std::endl;
}
}
else{
std::cout<<"You have missed 3 times. The answer is "<<total<<std::endl;
}
}

//function for addition
static int add (int a,int b)
{
return(a + b);
}


//function for subtraction
static int subtract (int a,int b)
{

return (a - b);
}

//Function for multiplication
static int multiply (int a,int b)
{

return (a * b);
}




int main ()
{
//seeding the random number
srand(time(0));

int total,choice;

//Menu
std::cout<<"Enter your choice" <<std::endl<<std::endl;
std::cout<<"1. Addition "<<std::endl;
std::cout<<"1. Subtraction "<<std::endl;
std::cout<<"1. Multiplication "<<std::endl;
std::cin>>choice;
//swtich case start
switch (choice){
//if choice is 1, first case
case 1: add(total);
answers(a,b,total);
break;

//if choice is 2, second case
case 2:subtract(total);
answers(a,b,total);
break;

//if choice is 3, third case
case 3:multiply(total);
answers(a,b,total);
break;
default: std::cout<<"You need to make a choice of 1, 2 or 3. "<<std::endl;
break;
}



cin.get();
cin.get();

return 0;
}




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.
wtd




PostPosted: Thu Sep 30, 2004 9:21 pm   Post subject: (No subject)

code:
case 1: add(total);


You've defined add to take two ints. You're only giving it one here.
Andy




PostPosted: Thu Sep 30, 2004 10:18 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: