2
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{

double addition;
double subtraction;
double top, bottom;
double multiplication, multiplication2;
char variable;
double total = 0.0;



    cout << "Type in:\n'A' For Addition\n"
        << "'S' For subtraction\n" << "'D' For division\n"
        << "'M' For multiplication\n";
    cin >> variable;

    switch (variable)
    {
    case 'A':
    {
        cout << "Enter 0 for input\n";
        cin >> addition;
        while(addition != 'Q' || addition != 'q')
        {

            cout << "Enter numbers for adding\nThen type in"
                << "Q or q to quit\n";
            cin >> addition;
            total += addition;

        }
        cout << "Your total is " << total << endl;

    }

It loops infinitely starting out at the first cout statement in the while loop. I will type in numbers, then as soon as I type in q or Q and hit enter it will immediately loop infinitely. Thanks!

6
  • 1
    Look at the logical condition for you while loop. What's wrong with it? Commented Jan 21, 2019 at 18:14
  • 1
    Your loop could only ends if addition is both Q and q simultaneously. So it never ends. Commented Jan 21, 2019 at 18:16
  • 3
    ... and comparing a double value with a character literal like 'Q' rarely makes sense. Commented Jan 21, 2019 at 18:17
  • while(addition != 'Q' || addition != 'q') better use && instead of || addition can't be both at the same time. Commented Jan 21, 2019 at 18:17
  • 1
    nothing wrong with my logical operators No they are wrong. addition can't be both q and Q at the same time so no matter what your input is the condition is always true. Commented Jan 21, 2019 at 19:01

2 Answers 2

3

Your condition for the while loop uses a logical OR.

Let's say you try to quit the loop and enter the input 'Q'. The first part of the condition will be FALSE, but the second part of the condition will be TRUE. Since it is a logical OR, then the whole condition will be TRUE and the loop will execute. The converse is also true if you input 'q'.

So no matter what you enter, your loop will run.

Sign up to request clarification or add additional context in comments.

4 Comments

@StephanLechner also brings up a good point. This won't work with a double variable.
It doesn't have anything to do with logical operators, but you're right a double can't receive input from a char. I think that's it. Thanks!
@Nathan they are 2 independent bugs.
Yeah you're right it is logical operators as well! Haha my bad.
3

There are two main problems in your program.

First, condition addition != 'Q' || addition != 'q' is always true, because for any value of addition, either addition != 'Q' or addition != 'q' is true (i.e. addition can never be both Q and q at the same time). You probably meant addition != 'Q' && addition != 'q'

Second, when you do cin >> addition with variable of type double, then you will either receive a valid number or - if somebody enters - 'Q', for example, "nothing" and an error flag is set. "Nothing" means that the value of addition remains unchanged.

To accomplish the "either a number or 'Q'"-thing, you need to read in a string and compare it to "Q" (or "q") and otherwise try to convert the string into a double.

The code fragment could look as follows:

int main() {

    double sum = 0;
    double toAdd;
    std::string input;
    bool end = false;

    while (!end) {
        cout << "enter a value to add (type Q or q to quit)" << endl;
        cin >> input;
        if (input == "Q" || input == "q") {
            end = true;
        }
        else {
            try {
                toAdd = stod(input);
                sum += toAdd;
            } catch (out_of_range &e) {
                cout << "input " << input << " is out of range." << endl;
            } catch (invalid_argument &i) {
                cout << "input " << input << " is not a valid number." << endl;
            }
        }
    }
    cout << "sum: " << sum << endl;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.