Thread: Calculator Problems

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    52

    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();
    
      }
    }

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You may want to post the errors.

    1) int main( ). Only int main( ).
    2) if, not If.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    52
    Warning 1 = Coversion may lose sinificant digits in function main()
    Warning 2 = Function should return a value in function main()

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Again, use int main( ). You don't need to worry about main( ) returning a value, because it's OK not to.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    52
    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:

    Code:
    } // the last one

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    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

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Seed the random number generator at the beginning of main(), not within your loop.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    srand(unsigned(time(0)));
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    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...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Couple corrections, cstdlib not stdlib, and ctime, not time.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    haha... yeah... forgot to add the 'c' before i posted... sorry
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed