-
Calculator Problems
Well second day of class and needless to say I am more than rusty after taking a year and a half off ne how I am getting two errors when I am compiling and I can't figure them out. This calc is supposed to generate two random numbers and the user picks what he wants to do w/them and then he answers it then the program tells him if he is right or wrong.
Thanks for the help in advance :-)
Code:
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<time.h>
double Add (double a, double b) //Addition
{
cout << a << " + " << b << " = ";
return (a+b);
}
double Multiply (double a, double b) //Multiplcation
{
cout << a << " * " << b << " = ";
return(a*b);
}
double Subtract (double a, double b) //Subtraction
{
cout << a << " - " << b << " = ";
return(a-b);
}
double Divide (double a, double b) //Dividing
{
cout << a << " / " << b << " = ";
return(a/b);
}
double main()
{
int num1;
int num2;
int choice;
int stop;
int ans;
int usans;
stop = 1;
while(stop < 2)
{
cout << "C++ Assigment Two - Random Calculator" << endl;
cout << "\n";
srand(time(0)); //this will produce a new number after each execution.
num1 = 1+rand()%20;
num2 = 1+rand()%20;
cout << "Do you wish to: (1)Add, (2)Subtract, (3)Multiply or (4)Divide ?" << endl;
cin >> choice;
switch (choice) //Calculator Function
{
case 1: cout << num1 << "+" << num2;
ans = num1 + num2;
break;
case 2: cout << num1 << "x" << num2;
ans = num1 x num2;
break;
case 3: cout << num1 << "-" << num2;
ans = num1 - num2;
break;
case 4: cout << num1 << "/" << num2;
ans = num1 / num2;
break;
}
cin >> usans;
If (ans == usans)
{
cout<< "/nYour answer is correct"
}
else
{
cout << "/nIncorrect Answer"
}
cout <<"\n\nDo you wish to (E)xit or (R)estart";
char ch;
cin >> ch;
//this if statment will keep looping untill they enter E or e...
if(ch == 'E' || ch == 'e') stop = 2;
clrscr();
}
}
-
You may want to post the errors.
1) int main( ). Only int main( ).
2) if, not If.
-
Warning 1 = Coversion may lose sinificant digits in function main()
Warning 2 = Function should return a value in function main()
-
Again, use int main( ). You don't need to worry about main( ) returning a value, because it's OK not to.
-
Tried int main() and I still get the same two warnings the weird thing is warning number one brings me to this line of code:
Code:
srand(time(0)); //this will produce a new number after each execution.
And the second warning brings me to this line:
-
these are the things that I am catching from your code:
Code:
ans = num1 x num2; // I'm guessing you want multiplication here so it should be
ans = (num1*num2);
the next one is mentioned in the reply above
Code:
If (ans == usans) // not If but if
cout<< "/nYour answer is correct" //you forgot a ';' at the end
cout << "/nIncorrect Answer" //here too!!!!
other then that it should compile and run, but I don't think the desired result will be shown,
axon
-
I have to post some more:
replace /n with \n to get newline.
prompt user for the answer because the prog is very unclear when all you get is two numbers with an operator...what then?
axon
-
Seed the random number generator at the beginning of main(), not within your loop.
-
srand(unsigned(time(0)));
-
just picking:
Code:
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h>
#include<time.h>
should be:
Code:
#include<iostream>
#include<iomanip>
#include<stdlib>
#include<time>
using namespace std;
of course that's only if the compiler your using follows newer standards...
-
Couple corrections, cstdlib not stdlib, and ctime, not time. ;)
-
haha... yeah... forgot to add the 'c' before i posted... sorry