Hello, I have been messing around and writing more and more intricate calculator programs on MVE C++. The issue I am having right now is my program is not running unless I give my add, sub, etc. a value. When I try to just leave them as int add; or int sub; It will not do the equation. Also, if I try to have my couts of cout<<"Your sum...<<add will not appear unless in the do-while loop. Lastly, if I hit q or Q it causes the program to output alot of random things, even though I made my while statement. I thank everyone who helps in advance, and my main concerns are the cout<< not displaying after while and Q messing up the program.

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int value;
    int sub=0;
    int add=0;
    int multp=1;
    double avg=0;
    int n=0;

    cout<<"Input your values (Press Q to quit)"<<endl;

    do
    {

    cout<<"Your sum is "<<add<<endl;
    cout<<"Your diff is "<<sub<<endl;
    cout<<"Your mult is "<<multp<<endl;
    cout<<"Your avg is "<<avg<<endl;

    cin>>value;

        add+=value;
        sub-=value;
        multp*=value;
        n++;
        avg=(add/n);

    }while(value!='q' && value!='Q');

    return(0);
}

Recommended Answers

All 7 Replies

The issue I am having right now is my program is not running unless I give my add, sub, etc. a value. When I try to just leave them as int add; or int sub; It will not do the equation.

Yes it will. You'll get some random value because you're using them before you set them to anything, but it still does it.

Also, if I try to have my couts of cout<<"Your sum...<<add will not appear unless in the do-while loop.

So, you're saying that anythign you don't put inside the do-while loop doesn't get done until you leave the do-while loop? That's how it's meant to work.

Lastly, if I hit q or Q it causes the program to output alot of random things, even though I made my while statement.

value is an int. 'q' and 'Q' are char. You are telling the compiler (through cin >> value ) to expect a number as input, and then you're giving it a letter as input. This is bad.

If you just write int sum or int sub, you'd be using those variables uninitialized in your loop, which would invoke undefined behavior. So don't do that.

Your while loop will run until value is either equal to 'q' or 'Q'. This will only happen if the user enters 113 or 81 as the input because value is an int, not a char. If the user entered q or Q, that would simply be discarded as invalid input and value would not change.

ah ty on the Q issue im still really new at this so will probably make these stupid mistakes. Lastly the thing I want the program to do, is for the final values of add sub multp... to be displayed after I hit Q. So instead of showing after each value input it inputs all the values and when q is entered it will display the couts. I will change the grammar on the first cout to reflect this with a 0 for right now

So instead of showing after each value input it inputs all the values and when q is entered it will display the couts.

So put them outside the while loop.

  }while(value!='q' && value!='Q');

  cout<<"Your sum is "<<add<<endl;
  cout<<"Your diff is "<<sub<<endl;
  cout<<"Your mult is "<<multp<<endl;
  cout<<"Your avg is "<<avg<<endl;

Even though I changed the value from Q and q to just 0 it does not display the cout after the while. After typing 0 it just leaves me with a blank line.

cin>>value;

        add+=value;
        sub-=value;
        multp*=value;
        n++;
        avg=(add/n);

    }while (value!='0');

    cout<<"Your sum is "<<add<<endl;
    cout<<"Your diff is "<<sub<<endl;
    cout<<"Your mult is "<<multp<<endl;
    cout<<"Your avg is "<<avg<<endl;
}

'0' is the character zero. It is NOT the number zero. If you want the loop to end when value is the number zero, try this:

}while (value!=0);

-_- gonna get a big headache with all these facepalm moments haha. Thx for that catch been mainly using whiles that deal with characters and did not even realize. Also thanks to everyone who helped

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.