Thread: What would be wrong with this little code...

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

    What would be wrong with this little code...

    Hello.
    I'm new here and this is my first post ^_^. I started C++..a few hours ago, I had some skills over some other programming languages so it was easy for me to learn the first few steps.
    I made up my own little code to try and learn and maybe learn more from it.
    Here is my code:

    Code:
    #include <iostream>	
    
    using namespace std;
    
    int main()
    {
        char m;
        int sub;
        int sub2;
        
       cout<<"If you type s subtraction will start: ";
       cin>>m;
        if (m == s) {
            cout<<"Please enter your first number: ";
            cin>>sub;
            cout<<"Please enter your second number now: ";
            cin>>sub2;
            cout<<"The results are "<<sub - sub2 <<"\n;
            cin.ignore();
        }
        else {
            cout<<"You Messed Up";
        }    
        cin.get();
    }
    I'd appriciate your help and just one more question.
    I use Dev C++ and I was wondering if anyone can tell me what is the best compiler to use?.
    `Thank You

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    cout<<"If you type s subtraction will start: ";
       cin>>m;
        if (m == s) {
    Did you try compiling this code? Did you pay attention to the error your compiler gives you? You know the one, something about s being undeclared and being used here first?

    s is a variable name, and you have no variable declared which is named s.
    's' is a character constant for the letter s.
    "s" is a string literal for a string containing the letter s (and the null following it).

    If you mean to compare a single character, you need to use single quotes. If you mean to compare a string, you need to use double quotes and the appropriate comparison function / string class.

    In this case, since you're using a char, you'd be using single quotes:
    Code:
    if( foo == 's' )
        ...do whatever...
    It's generally a good idea to pay attention to all warnings and errors your compiler spits out at you.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    5
    I changed a few things in it.
    Code:
    #include <iostream>	
    
    using namespace std;
    
    int main()
    {
        int m;
        int sub;
        int sub2;
        
       cout<<"If you type s subtraction will start: ";
       cin>>m;
        if (m == 1) {
            cout<<"Please enter your first number: ";
            cin>>sub;
            cout<<"Please enter your second number now: ";
            cin>>sub2;
            cout<<"The results are "<<sub - sub2 <<"\n;
            cin.ignore();
        }
        else {
            cout<<"You Messed Up";
        }    
        cin.get();
    }
    it stilll doesn't work and here is the error msg I got
    Code:
    syntax  error before `.' token
    In function `int main()':
    missing terminating " character

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    The hint's in the error: missing terminating " character
    Look for one of those!

    Also read Quzah's post so you can fix the initial input.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your code:
    Code:
    cout<<"The results are "<<sub - sub2 <<"\n;
    Correct code:
    Code:
    cout<<"The results are "<<sub - sub2 <<"\n";
    Sing it with me now!

    One of these things!
    Is not like the other!

    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    5
    Thank you very much Prelude. Lol I better watch for this little errors from now on.
    Anyways I made a little changes to it again.
    Code:
    #include <iostream>	
    
    using namespace std;
    
    int main()
    {
        char m;
        int math = 0;
        int math2 = 0;
        
       cout<<"Type (1) for Subtraction\n";
       cout<<"Type (2) for Addition\n";
       cout<<"Type (3) for Division\n";
       cout<<"Type (4) for Multiplication\n";
       cout<<"Please type your desired number: ";
       cin>>m;
       cin.ignore();
        if (m == 1) {
            cout<<"Please enter your first number: ";
            cin>>math;
            cout<<"Please enter your second number now: ";
            cin>>math2;
            cout<<"The results are "<<math + math2 <<"\n";
            cin.ignore();
        }
        if (m == 2) {
            cout<<"Please enter your first number: ";
            cin>>math;
            cout<<"Please enter your second number now: ";
            cin>>math2;
            cout<<"The Results are "<<math - math2 <<"\n";
            cin.ignore();
        }
        if (m == 3) {
            cout<<"Please enter your first number: ";
            cin>>math;
            cout<<"Please enter your second number now: ";
            cin>>math2;
            cout<<"The Results are "<<math / math2 <<"\n";
            cin.ignore();
        }
        if (m == 4) {
            cout<<"Please enter your first number: ";
            cin>>math;
            cout<<"Please enter your second number now: ";
            cin>>math2;
            cout<<"The Results are "<<math * math2 <<"\n";
            cin.ignore();
        }          
        else if (m >= 5) {
            cout<<"You Messed Up";
        }    
        cin.get();
    }
    This time it works fine but instead when its compiled. It says to pick a number between 1-4 and when I type any number its runs the
    Code:
        else {
            cout<<"You Messed Up";
        }
    You guys know the problem, I'll be looking for it myself as well.
    Last edited by DiGiT; 12-24-2004 at 09:19 PM.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    First, m is a char, so when you type 2, the value placed in m will be '2', which is not the same as the integer literal 2 (confused yet?). What you want to do is either change m to int, or change your tests to surround the number with single quotes to make them character literals.

    Second, the else part of the test only applies to the if test for 4, so if m is not equal to 4 then you'll get the "You messed up" message. To fix that you use else if:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char m;
      int math = 0;
      int math2 = 0;
    
      cout<<"Type (1) for Subtraction";
      cout<<"Type (2) for Addition";
      cout<<"Type (3) for Division";
      cout<<"Type (4) for Multiplication";
      cout<<"Please type your desired number: ";
      cin>>m;
      cin.get(); // Eat the newline
      if (m == '1') {
        cout<<"Please enter your first number: ";
        cin>>math;
        cout<<"Please enter your second number now: ";
        cin>>math2;
        cout<<"The results are "<<math + math2 <<"\n";
        cin.ignore();
      }
      else if (m == '2') {
        cout<<"Please enter your first number: ";
        cin>>math;
        cout<<"Please enter your second number now: ";
        cin>>math2;
        cout<<"The Results are "<<math - math2 <<"\n";
        cin.ignore();
      }
      else if (m == '3') {
        cout<<"Please enter your first number: ";
        cin>>math;
        cout<<"Please enter your second number now: ";
        cin>>math2;
        cout<<"The Results are "<<math / math2 <<"\n";
        cin.ignore();
      }
      else if (m == '4') {
        cout<<"Please enter your first number: ";
        cin>>math;
        cout<<"Please enter your second number now: ";
        cin>>math2;
        cout<<"The Results are "<<math * math2 <<"\n";
        cin.ignore();
      }          
      else {
        cout<<"You Messed Up";
      }    
      cin.get();
    }
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    5
    Alright Prelude I fixed it
    thank you for your time and help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM