Code Help

0 Nikolai Illyich · April 10, 2015
So I've created this little calculator program that asks the user first of the operation (add, sub, mult, div), then lets the user input two numbers and those will be processed based on the input operation.

I  made it so that it can keep calculating an indefinite amount of times.


#include
#include


using namespace std;


int addition(int x, int y);
int multiplication(int x, int y);
int division(int x, int y);
int subtraction(int x, int y);

int main()
{
    do{
    int x;
    int y;
    string op;
    //Prompt message:
    cout y;
            addition(x, y);
        }
        if (op == "sub")
        {
            cout x; cin >> y;
            subtraction(x, y);
        }
        if (op == "mult")
        {
            cout x; cin >> y;
            multiplication(x, y);
        }
        if (op == "div") {
            cout x; cin >> y;
            division(x, y);
        }
        if ( ! (op == "add") && (op == "sub") && (op == "mult") && (op == "div") ) {

            cout

Thing is, every iteration, the prompt message appears twice, and I do not know why. Anyone knows why this happens? 
Thanks a lot! :D

And any helpful tips regarding how the code is written is highly appreciated.

Post a Reply

Replies

Oldest  Newest  Rating
0 Yazer Cha · April 10, 2015
One thing that would help is to use else if because it makes code much more readable.  I would and the "while" in your do while loop you have set up.  Posting the prompt message would also help determine what you problem is ;).
0 Nikolai Illyich · April 12, 2015
My apologies, looks like the code in the first post isn't complete ( I do not know why, it justs shortens itself when I Submit)

Edit: Turns out it does with this post too.

But anyways, how could the last statement not work? Doesn't the '!' make sure that if either operators are entered, it would print a warning message?
0 Clark Alaan · April 13, 2015
The reason is because the last if statement doesn't have a good scope. Right now, it would only check it in this way:

((!(op=="add"))&&...//other conditions)

Notice the '!' being only applied to your first condition, i.e. the op=="add" condition. Now seeing as that can be true when op is not equal to "add", you are also expecting op to be equal to "sub","mult", and "div" all at the same time which will never happen. So it will never be true.

Next time, when you make long && conditions like this, it'd be better to have it in this format IMO.

((condition1&&condition2)&&(condition3&&condition4))

The compiler might check the conditions differently otherwise, and it may end up having weird issues. I may be wrong about this though.

However, in my opinion using an else if would be much of a greater help since you'd be able to turn the last conditional to an else instead, removing any need for a condition that looks like that in this case at least.

As for the question in the first post,
Posting the code of your prompt message should enable us to help you determine your problem with it. (It's just a comment saying that there's some form of prompt message there atm) Is it a nested loop? or is it simply a cout and a cin?

Hope that this helps and that we can help you further if you are still experiencing this problem.



Edit: I noticed that you redeclare your variables every time the loop loops. I would suggest putting the declaration outside of the loop, and instead only reinitializing them inside the loop. As for why, the issue is scope, and in this case. You're better off just declaring them outside of the do-while loop.
  • 1

C++

107,155 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator