CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: New to C++, code help.

  1. #1
    Join Date
    Nov 2014
    Posts
    6

    New to C++, code help.

    I have this simple calculator code, but when I run it, I put two numbers as input and also if I want to add, subtract, multiply, ect. The output does the opposite. Can anyone help please?





    Name:  Program Calculator!.jpg
Views: 665
Size:  23.7 KBName:  Program Calculator!.jpg
Views: 665
Size:  23.7 KBName:  Program Calculator!.jpg
Views: 665
Size:  23.7 KBName:  Program Calculator!.jpg
Views: 665
Size:  23.7 KBName:  Program Calculator!.jpg
Views: 665
Size:  23.7 KBName:  Program Calculator!.jpg
Views: 665
Size:  23.7 KBName:  Program Calculator!.jpg
Views: 665
Size:  23.7 KB

  2. #2
    Join Date
    Nov 2014
    Posts
    6

    Re: New to C++, code help.

    Sorry, what a fail thread, ignore the top pictures. this is my code

    #include <iostream>

    using namespace std;

    main()
    {
    double x, y;
    int add, subtract, multiply, divide;

    cout << "Enter a number: ";
    cin >> x;
    cout << "Enter a number: ";
    cin >> y;

    cout << "Do you want to add, subtract, multiply, or divide?" "\n";
    cin >> add || subtract || multiply || divide;

    if (add)
    {
    cout << "These two numbers add to, " << x + y << endl;
    }
    else if (subtract)
    {
    cout << "These two numbers add to, " << x - y << endl;
    }
    else if (multiply)
    {
    cout << "These two numbers multiply to, " << x * y << endl;
    }
    else
    {
    cout << "These two numbers divide to, " << x / y << endl;
    }
    }

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: New to C++, code help.

    When posting code please use code tags. Go Advanced, select the code and click '#'.

    Code:
    cin >> add || subtract || multiply || divide;
    Whatever you think this statement is supposed to do, it doesn't!

    The easiest way would be to ask the user to enter a for add, s for subtract, m for multiply and d for divide (as a char). Then check the entered char.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  4. #4
    Join Date
    Nov 2014
    Posts
    6

    Re: New to C++, code help.

    Okay thanks.
    Also, the program runs but it just doesn't add, subtract, ect. correctly, when you say replace subtract for s and whatnot, do you want the 3 || signs gone? I don't know what it should look like.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: New to C++, code help.

    Code:
    char ch;
    cin >> ch;
    
    if (ch == 'a')
    ....
    else if (ch == 's')
    ...
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  6. #6
    Join Date
    Nov 2014
    Posts
    6

    Re: New to C++, code help.

    What you say does make sense and I replaced it into my program, but I ran it and I said 10 + 5 and the output was, "These two numbers divide to, 2." What??? Here is what the code now looks like


    #include <iostream>

    using namespace std;

    main()
    {
    double x, y;
    int ch, a, s, m;

    cout << "Enter a number: ";
    cin >> x;
    cout << "Enter a number: ";
    cin >> y;

    cout << "Do you want to add, subtract, multiply, or divide?" "\n";
    cin >> ch;
    if (ch == a)
    {
    cout << "These two numbers add to, " << x + y << endl;
    }
    else if (ch == s)
    {
    cout << "These two numbers add to, " << x - y << endl;
    }
    else if (ch == m)
    {
    cout << "These two numbers multiply to, " << x * y << endl;
    }
    else
    {
    cout << "These two numbers divide to, " << x / y << endl;
    }
    }

    Should I show you a screenshot of it, but I'm guessing you have a complier too. I don't know why it thinks I'm dividing when I inputed 'a' and it checked to see if 'ch' equals 'a' and it does so why didn't it add them together?????? Thanks.
    Last edited by PastaChef; November 30th, 2014 at 12:56 PM.

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: New to C++, code help.

    You missed the quotes off the a, m etc as per my post #5.
    Code:
    if (ch == 'a')
    Also you don't define a s m as these are not being used as variables but as constants.

    Also I defined ch as a char, not as an int!
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  8. #8
    Join Date
    Nov 2014
    Posts
    6

    Re: New to C++, code help.

    Thank you 2kaud! you were super helpful and understanding, now it works. One question, what does the 'a' quotes mean? Because it changed the color when I inserted them and it must mean something I don't know what? Than you.

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: New to C++, code help.

    'a' means the character a as opposed to a without ' which means the variable a and "a" in double quotes means the string containing the character a and "ab" would mean a string containing the characters a b

    How are you learning c++?

    You might want to have a look at
    http://www.learncpp.com/
    http://www.cplusplus.com/doc/tutorial/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  10. #10
    Join Date
    Nov 2014
    Posts
    6

    Re: New to C++, code help.

    Okay, thanks 2kaud. I bought a book called Jumping Into C++ by Alex Allain. Here is a link to show you.

    http://www.amazon.com/Jumping-into-C.../dp/0988927802

    This got pretty good reviews for beginners.

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,117

    Re: New to C++, code help.

    That's not a book I've seen. The Amazon reviews are mostly positive with just a few negatives. How far have you progressed through it and what are your views?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.6.0)

  12. #12
    Join Date
    Oct 2013
    Posts
    10

    Re: New to C++, code help.

    You can improve your program by seeing other sample codes
    here is a simple example is discussed here hope it will be helpful.
    Its function based and some restrictions to user not to enter wrong option e.g divide by zero..
    C++ simple calculator example

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)