HELP with Basic else if statment
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: HELP with Basic else if statment

  1. #1
    Join Date
    Jul 2010
    Posts
    2

    Post HELP with Basic else if statment

    HI,
    Im sure this will be easy for you guys, however it's being annoying me for quite a while now. I have only studyed a couple of hours in C++ in a induction sesision.
    ok here is my code:


    #include <iostream>

    using namespace std;

    int main()

    {
    char op;
    int num1;
    int num2;
    int ans;

    cout <<"Enter your first number: ";
    cin >> num1;
    cout <<"Enter your second number: ";
    cin >> num2;
    cout << "" << endl;

    if (op == '+');
    cout << "Add" <<endl;

    {
    ans = num1 + num2;
    cout << "Answer is " << ans << endl;
    cout << "" << endl;
    }
    else if (op == '-');
    cout << "Subtract" <<endl;

    {
    ans = num1 - num2;
    cout << "Answer is " << ans << endl;
    cout << "" << endl;
    }
    else if (op == '*');
    cout << "Times" <<endl;
    {
    ans = num1 * num2;
    cout << "Answer is " << ans << endl;
    cout << "" << endl;
    }
    else (op == '/');
    cout << "Divide" <<endl;

    {
    ans = num1 / num2;
    cout << "Answer is " << ans << endl;
    cout << "" << endl;
    }

    system("PAUSE");
    }

    Right the problems, i carnt see anthing wrong with it however when i try to run it in Dev-C++ is says there is somthing wrong with my else if statments, however if i replace them with if startments it runs just not the way i want it to.

    hope you can help i am very new to C++ so any help will be appreciated.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,426

    Re: HELP with Basic else if statment

    First please use code tags when posting code. The code you posted is unformatted and very hard to read.
    when i try to run it in Dev-C++
    This is the Visual C++ forum. Next time, post your question in the non-Visual C++ forum. "Dev-C++" is an IDE that runs the gcc compiler underneath the hood -- none of these deals with Microsoft Visual C++, which is the topic of this forum.
    Im sure this will be easy for you guys, however it's being annoying me for quite a while now. I have only studyed a couple of hours in C++ in a induction sesision.
    Explain what this code does, because it doesn't look right:
    Code:
    if (op == '+'); // <-- what's this semicolon doing here?
        cout << "Add" <<endl;
    
    // What's this supposed to be?
    {
        ans = num1 + num2;
        cout << "Answer is " << ans << endl;
        cout << "" << endl;
    }
    Please read up on how to properly construct an if/else in C++, because whatever that is, it's incorrect. You do it several times in your code.
    i carnt see anthing wrong with it
    If you take a look at the C++ book you're using, you can't see the difference between what you posted and what you see in your book?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 17th, 2010 at 04:59 AM.

  3. #3
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: HELP with Basic else if statment

    Since your code isn't formatted (with code tags), I didn't look at it in detail, but for one thing, you need to remove the ; at the end of your if and your else if lines.
    Code:
    if (op == '+');    // lose the semicolon
    ...
    else if (op == '-');    // lose the semicolon
    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  4. #4
    Join Date
    Apr 2010
    Posts
    166

    Talking Re: HELP with Basic else if statment

    Hello this should help you it is a reconstructed version of your project hope it helps

    how do you put in code tags :L:L

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    char op;
    int num1;
    int num2;
    int ans;
    
    cout <<"Enter your first number: "; 
    cin >> num1;
    cout <<"Enter your second number: ";
    cin >> num2;
    cout <<"Pick your operation: ";
    cin >> op;
    cout << "" << endl;
    
    
    if (op == '+'){
    cout << "Add" <<endl;
    
    ans = num1 + num2;
    cout << "Answer is " << ans << endl;
    cout << "" << endl;
    }
    else if (op == '-'){
    cout << "Subtract" <<endl;
    
    
    ans = num1 - num2;
    cout << "Answer is " << ans << endl;
    cout << "" << endl;
    }
    else if (op == '*'){
    cout << "Times" <<endl;
    
    ans = num1 * num2;
    cout << "Answer is " << ans << endl;
    cout << "" << endl;
    }
    
    else if (op == '/')
    {
    cout << "Divide" <<endl;
    
    ans = num1 / num2;
    cout << "Answer is " << ans << endl;
    cout << "" << endl;
    }
    
    system("pause");
    return 0;
    
    }
    Last edited by gaar321; July 19th, 2010 at 04:43 PM.

  5. #5
    Join Date
    Apr 2010
    Posts
    166

    Re: HELP with Basic else if statment

    oh yeah dont use this for new line cout <<""<<;

    it should be \n at the end

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center