Beginner needs help with If Statement

This is a discussion on Beginner needs help with If Statement within the C++ Programming forums, part of the General Programming Boards category; Hi, i recently developed a desire to learn how to program. After asking around, I was told to pick up ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    2

    Question Beginner needs help with If Statement

    Hi, i recently developed a desire to learn how to program. After asking around, I was told to pick up C++ as the programing language to learn. I downloaded the free Microsoft Visuall C++ Express and whipped out the cprograming.com tutorial. In the first tutorial it gives you a basic program

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int thisisanumber;
    
      cout<<"Please enter a number: ";
      cin>> thisisanumber;
      cin.ignore();
      cout<<"You entered: "<< thisisanumber <<"\n";
      cin.get();
    }

    After working this threw a couple times i decided to toy around and wrote a simple program using the base code, but adding an extra line for a second number and then subtracted the two and then gave you an answer. This too worked out fine. After reading the next chapter on If statements, i decided to go back and get more indepth. Setting up a couple different variables and then asking the user to imput what type of math, be it addition subtraction multiplication etc and then execute a series of commands to provide them with a simple answer. So far the code i have writen is such

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int firstnum;
    	int secondnum;
    	int equals;
    	char sub;
    	char add;
    	char multiply;
    	char choice;
    	
    cout<<"Please choose one:Add Multiply Or Subtract";
    	cin>>choice;
    	cin.ignore ();
    	If (choice == sub); {
    	  cout<<"Please enter a number: ";
          cin>> firstnum;
          cin.ignore();
          cout<<"please enter a number to subtract: ";
          cin>> secondnum;
          cin.ignore ();
    	  equals = firstnum - secondnum;
          cout<<"Your answer is: "<< equals <<"\n";
          cin.get(); }
    	Else (choice == add) {
    		cout<<"Please enter a number:";
    		cin>> firstnum;
    		cin.ignore();
    		cout<<"Please enter a number to add: "; 
    		cin>> secondnum;
    		cin.ignore ():
    		equals = firstnum + secondnum;
    		cout<<"You answer is:" <<equals <<"/n";
    		cin.get(); }
    }
    Since i am new, i stopped after the first two functions and debugged. I keep getting two identical errors when i debug the program. "Error C3861 If identifier not found and Else identifier not found. I tried using the help section of Visual C++ plus provide insight on the error code and it explains how to fix it, in a way that I have not learned yet. So I am hoping somebody out there can spot my mistake and help me correct it.

    Thanks.
    ps i hope the code tag worked, first time posting.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Ok. Firstly I'm going to say that I appreciate that learning programming is hard. So stick at it!!

    Secondly, both C and C++ are both case sensitive, so if isn't the same as If, nor is else the same as Else. It's only the lower case versions which should work (it's likely that they're typedeffec of defined somewhere, but ... )

    The best way to do this is to do something like:

    Code:
    char option;
    int firstnum, secondnum, answer;
    
    cout<< "Enter your first number: ";
    cin >> firstnum;
    
    cout<< "Enter your second number: ";
    cin >> secondnum;
    
    cout<< "Do you want to (m)ultiply, (d)ivide, (s)ubtract or (a)dd those two numbers?";
    cin >> option;
    
    if ( option == 'm' )
    {
      // Do multiplication stuff here
    }
    else if ( option == 'd' )
    {
      // do division stuff here
    }
    else if ( option == 's' )
    {
      // do subtraction
    }
    else if ( option == 'a' )
    {
      // do addition stuff
    }
    else 
    {
      cout<< "That option is not available here";
    }
    Now. Look at that for and see if you can see why it doesn't work as you would like

    In your code you made characters called add, subtract etc. These aren't initialised in your code (not given initial values), so saying if something == something else will have indefined effects. You could have said:

    Code:
    char add='a';
    char subtract = 's';
    char multiply = 'm';
    char divide = 'd';
    and then added a

    Code:
    if ( option == add ) 
    {
      [..]
    }
    thing, but there's no need.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You'd probably want these variables to be constant, unless you plan on changing them.
    Code:
    char add='a';
    char subtract = 's';
    char multiply = 'm';
    char divide = 'd';
    ->
    Code:
    const char add='a';
    const char subtract = 's';
    const char multiply = 'm';
    const char divide = 'd';
    If you're using constant values, it looks like a sure case for a switch statement.

    You could always use an array of function pointers.

    BTW, you could always use '+' instead of 'a' etc.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Posts
    2
    I changed the uppercase If and Else statements, and the program debugged completely. I ran the program and it Asked the opening statement, Please choose oneA)dd (M)ultiply Or (S)ubtract:"

    After you enter your choice, the window just closes. Not really sure what to do now, since im just starting to learn, i cant really troubleshoot anything without the debugger telling me where an error is, since i dont know what is right and what is wrong.

    If anybody wants to take the time to look it over, ill post the whole code. Its not very long

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      int firstnum;
    	int secondnum;
    	int equals;
    	int sub =' s ';
    	int add = ' a ';
    	int multiply = ' m ';
    	int choice = ' c ';
    	
    	cout<<"Please choose one:(A)dd (M)ultiply Or (S)ubtract:";
    	cin>>choice;
    	cin.ignore ();
    	if (choice == sub) {
    	  cout<<"Please enter a number: ";
          cin>> firstnum;
          cin.ignore();
          cout<<"please enter a number to subtract: ";
          cin>> secondnum;
          cin.ignore ();
    	  equals = firstnum - secondnum;
          cout<<"Your answer is: "<< equals <<"\n";
          cin.get(); }
    	else if (choice == add) {
    		cout<<"Please enter a number:";
    		cin>> firstnum;
    		cin.ignore();
    		cout<<"Please enter a number to add: "; 
    		cin>> secondnum;
    		cin.ignore ();
    		equals = firstnum + secondnum;
    		cout<<"You answer is:" <<equals <<"\n";
    		cin.get(); }
    	else if (choice == multiply); {
    		cout<<"Please enter first number:";
    		cin>> firstnum;
    		cin.ignore ();
    		cout<<"Please enter Second number to Multiply";
    		cin>> secondnum;
    		cin.ignore ();
    		equals = firstnum * secondnum;
    		cout<<"Your answer is:" <<equals <<"\n"; }
    
    
    	}

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    int sub =' s ';
    Your compiler should give you a warning for that, something about "multicharacter constants". It should be
    Code:
    int sub = 's';
    or maybe
    Code:
    char sub = 's';
    or maybe
    Code:
    const char sub = 's';
    This semicolon really messes up your code. Get rid of it.
    Code:
    else if (choice == multiply); {
    For code like this,
    Code:
    if(x); {
        // ...
    }
    the compiler sees
    Code:
    if(x) {}
    
    {  /* always executes */
        // ...
    }
    You're writing it much longer than it needs to be. Prompt for the numbers outside the if statements. The user input could look something like this
    Code:
    Enter the operator: +
    Enter the first number: 3.14159265359
    Enter the second number: 1.0
    3.141593 + 1.000000 = 4.141593
    and your pseudocode
    Code:
    get operator
    get number 1
    get number 2
    if operator is '+'
        result = number1 + number2
    else if operator is '-'
        result = number1 - number2
    etc
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    I prefer to use enum for declaring such consts, it will group and show all possibilities at once
    Code:
    enum operations
    {
       add = 'a',
       sub = 's',
    etc
    };
    So anyone wondering what are the possible operations supported - has one place to look for
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I particularily like enums for values that increment each time, because the compiler does the calculations for you. You make less mistakes that way.

    There are arguments against using what some call the "enum-hack", though. One thread comes to mind: #define vs const

    Basically, you can make constants in three ways (AFAIK).
    1. With enums.
    2. With constant variables.
    3. With #defines.


    The third is usually a bad idea in C++, and the first two have been mentioned in this thread.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    >With #defines.

    Hmm takes me back to the old C language sytle of the mid 90s
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 05:51 AM
  2. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  3. Beginner needing help in C language
    By chauncey005 in forum C Programming
    Replies: 2
    Last Post: 09-26-2003, 02:04 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21