Thread: what's wrong?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    31

    what's wrong?

    Okay well I am going to try to make a big calculator, but I am starting off here since this is something I don't understand. Sorry for most likely a very stupid question, but could use your help. Anyways I have this code:

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main()
    {
      int choice;
      cout<<"Enter what you want to do.Do so by either entering in add or subtract."<<endl;
      cin>> choice;
      
      if (choice == 'add')
      {
       int first;
       int second;
       cout<<"Enter in your first number."<<endl;
       cin>> first;
       cout<<"Enter in your second number."<<endl;
       cin>> second;
       cout<<"The answer is: "<<(first + second)<<endl;
      }
      else if (choice == 'subtract')
      {
       int first1;
       int second1;
       cout<<"Enter in your first number."<<endl;
       cin>> first1;
       cout<<"Enter in your second number."<<endl;
       cin>> second1;
       cout<<"The answer is: "<<(first1 - second1)<<endl;
      }
      
      system("PAUSE");	
      return 0;
    }
    It will let me compile and run. It will ask what I would like to do and when I enter in add or subtract it just says Press any key to continue... and I do and it exits. I don't know what I am doing wrong. I don't know if I need to declare add and subtract or what. I am confused so please a little help here. Thank you...



    Emotions

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    13
    you declared choice ast Int and comparing to a string ... fix that first
    Ok...

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    Should I change it to a char or a string? I tried char and it compiled the exact same way and did the exact same thing. I tried putting it as string choice; and it didn't compile.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    48

    Problem...

    When I made a calculator program I made it menu style. For example,
    Code:
    cout<<"Please choose an option below.\n1. Addition\n2. Subtraction\nWhat is your choice?  ";
    cin>>choice
    and you can probably figure out the rest. I am really new to this so it may not be a good way to do it but it worked perfectly for me

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    13
    It should work,copy and paste, but if you are taking a C++ course you should know all this .. v important...

    I don't recommend declearation of too many ints..


    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
      string choice (" ");
      cout<<"Enter what you want to do.Do so by either entering in add or subtract."<<endl;
      cin >> choice;
      
      if ( choice == "add")
    	{
    		int first;
    		int second;
    		cout<<"Enter in your first number."<<endl;
    		cin>> first;
    		cout<<"Enter in your second number."<<endl;
    		cin>> second;
    		cout<<"The answer is: "<<(first + second)<<endl;
    	}
      else  if (choice == "subtract")
    	{
    		int first1;
    		int second1;
    		cout<<"Enter in your first number."<<endl;
    		cin>> first1;
    		cout<<"Enter in your second number."<<endl;
    		cin>> second1;
    		cout<<"The answer is: "<<(first1 - second1)<<endl;
    	}
      
      system("PAUSE");	
      return 0;
    }
    Last edited by Shahram_z; 12-27-2004 at 10:43 PM.
    Ok...

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    13
    Oh i shouldn't forget declare your int in main not within the f statement, plus remove that system pause thingi it pauses if you are using a .net complier...
    Ok...

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    Not taking any class. I am just trying to learn on my own from tutorials and books. I'm sorry but what do you mean by declaring my int in the main. I copied and pasted the code and I found out what I was missing which was the #include <string> header. The code worked, Thanks. I am going to expand and make it where you can do other things rather than just add or subtract.

    Should I use the if statements or use switch? Which would be best if I could even use switch. Also how would I use the switch if I wanted to have the user enter in words rather than numbers. I know:

    Code:
    int choice;
    cout<<"Enter in either a 1, 2, or 3."<<endl;
    cin>> choice;
    
    switch (choice)
    {
           case 1:
                //code to do if 1 was chosen
           break;
           
           case 2:
                //code to do if 2 was chosen 
           break;
         
           case 3:
                //code to do if 3 was chosen
           break;
    
           default:
           cout<<"Your choice was invalid."<<endl;
    }
    But if I wanted to use words instead of number would I change case 1 to like case add or what? Sorry for the ignorance.

    *edit*- Sorry I thought about it again and realized what you meant by declare my ints in the main and not in the if statements. So it is best to declare right after int main() {?
    Last edited by Emotions; 12-27-2004 at 11:11 PM.

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Switch statement are much easier to read than multiple if statements, BUT unfortunately you need case statements that evaluate to integers. You could do this:
    Code:
    char choice;
    cout<<"Would you like to add (A) or subtract (S)?"<<endl;
    cin>>choice;
    switch (choice)
    {
           case 'A':
                //add
           break;
           
           case 'S':
                //subtract 
           break;
    
           default:
           cout<<"Your choice was invalid."<<endl;
    }
    but unfortunately you cannot do this:
    Code:
    string choice;
    cout<<"Would you like to add or subtract?"<<endl;
    cin>>choice;
    switch (choice)
    {
           case "add":
                //add
           break;
           
           case "subtract":
                //subtract 
           break;
    
           default:
           cout<<"Your choice was invalid."<<endl;
    }
    I'm not sure if there is an easy way around this with strings, I can't think of anything off the top of my head.
    Last edited by PJYelton; 12-27-2004 at 11:20 PM.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    thanks for the help. I did it with a addition, subtraction, multiply, division, and square option. I just went ahead and did it using if statements and I added in numbers. Like I used 1)Add 2)Subtract..... And changed it so if (choice == "2").. It works fine. Thanks for all of the help. I'm sure I'll be back soon with another question that I don't understand, but let's hope not.

  10. #10
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Switch statement are much easier to read than multiple if statements, BUT unfortunately you need case statements that evaluate to integers.
    I was recently writing a text parser and needed to check many times if one of the many acceptable strings had been entered. I made an array of strings, and simply made a loop that would cycle through this array, strcmp()ing the current string to each string in the table. If one came up, I executed the necessary code and then broke the loop. This would solve the above problem with only one if statement.

  11. #11
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    If you are using numbers or chars then, I would definately suggest using a switch statement. Its much easier to read and maintain, although to each their own

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    Yeah I could just do the switch the same way by using numbers for each operation. I think I will rewrite it using switch statements to see if I can do it without any problems. Will help me learn. Thanks.

  13. #13
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Quote Originally Posted by sean_mackrory
    I was recently writing a text parser and needed to check many times if one of the many acceptable strings had been entered. I made an array of strings, and simply made a loop that would cycle through this array, strcmp()ing the current string to each string in the table. If one came up, I executed the necessary code and then broke the loop. This would solve the above problem with only one if statement.
    Do you mean only one if statement AND a switch statement? Because otherwise wouldn't you still need to have some if statements to see which word was found in the array and execute the code appropriate for that command?

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    No. I had a table of acceptable strings, and the string I was testing at that time (a loop was running feeding in new strings to be tested all the time). I would loop through every string in the table of acceptable strings, strcmp() with the string being tested, and if the results was 0, I would execute the code. If not, the loop moved onto the next acceptable string in the table. Once it was done with that table, it read in a new string.

    edit: I was able to tell which string it had matched with because it was the same string that had been used in the strcmp function immediately beforehand.
    Last edited by sean; 12-27-2004 at 11:46 PM.

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    31
    I just remade the calculator with the switch statement. It went smoothly except for the string choice; I had declared. A message came up saying that switch declares quantity not an integer. So I just changed the string choice; to int choice; and it compiled fine. Thanks again..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM