0

My if statement runs through as if the conditions have been met even when they haven't. I have tried moving bits of code about and even rewriting the if statement differently but it has not changed the outcome. Does anyone know what I'm doing wrong?

#include <iostream>
#include <string>
using namespace std;

double num, num2, num3, num4, num5, num6, sum;
char input;
bool continueBool = true;
string bob;

void math()
{


    cout << "Please enter your first number" << endl;
    cin >> num;

    cout << "Please enter your second number?" << endl;
    cin >> num2;

    cout << "Please enter your third number?" << endl;
    cin >> num3;

    cout << "Please enter your fourth number" << endl;
    cin >> num4;

    cout << "Please enter your fith number?" << endl;
    cin >> num5;

    cout << "Please enter your sixth number?" << endl;
    cin >> num6;

    sum = num + num2 + num3 + num4 + num5 + num6;


}

void ifStatement() {

    if (bob == "no", "No", "NO", "nO") {

        continueBool = false;

        cout << "Good bye!" << endl;

    }
}


int main()
{
    while (continueBool = true) {


        math();

        cout << "The sum of your numbers is: " << sum << endl;

        cout << "Would you like to add any more numbers together?" << endl;

        cin >> bob;

        ifStatement();

        return 0;


    }



}
  • 1
    warning: `while (continueBool = true)` --> `while (continueBool) `..... Better than `while (continueBool == false == false)`. Bottom line: checking whether a boolean `==true` never makes sense, just check the boolean itself. – A.S.H Jan 09 '17 at 02:57
  • Always `return`ing from inside a loop makes the loop: _not-a-loop_. Though when you have an infinite loop bug, perhaps that's not a bad thing. – Disillusioned Jan 09 '17 at 04:17
  • See also: http://stackoverflow.com/q/16475032/224704 – Disillusioned Jan 09 '17 at 06:53

4 Answers4

2

This is really bogus

if (bob == "no", "No", "NO", "nO")

You need to break it out with logical OR instead

if (bob == "no" || bob == "No" || bob == "NO" || bob == "nO")

As it stand, this if (bob == "no", "No", "NO", "nO") would be equivalent to if("nO") as the effect of the comma operator.

artm
  • 17,291
  • 6
  • 38
  • 54
  • That has fixed it running through but now it does not continue the while loop? – Scott Mcgonigle Jan 09 '17 at 02:52
  • 1
    @ScottMcgonigle But it solved the problem you asked about? It seems somewhat unappreciative to not upvote an answer just because there are still more mistakes in your code over and above the first one you encountered. – Disillusioned Jan 09 '17 at 04:16
0
bob == "no", "No", "NO", "nO"

is not doing what you think it is doing. You mean to do:

bob == "no" ||
bob == "No" ||
bob == "NO" ||
bob == "nO"
druckermanly
  • 2,694
  • 15
  • 27
0

This is a bit of a side note to your question, but in this context, you may want to consider converting the answer to lowercase (or uppercase) before comparing it.

That way, you can just use if (tolower(bob) == "no")


Here's an example of how to do use the tolower function

http://www.cplusplus.com/reference/cctype/tolower/

/* tolower example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
  {
    c=str[i];
    putchar (tolower(c));
    i++;
  }
  return 0;
}
Meercat9
  • 1
  • 1
0

Your loop problem can be explained:

while (continueBool = true)

should read

while (continueBool == true).

As your code currently stands, you are setting it to true instead of checking for a value, so it will never exit.

Mikel F
  • 3,567
  • 1
  • 21
  • 33
  • 1
    Even better: `while (continueBool)`. – Pete Becker Jan 09 '17 at 03:37
  • @PeteBecker I consider it a matter of preference. I suspect modern compilers will output the same results with both forms. – Mikel F Jan 09 '17 at 04:08
  • Well, yes, they'll produce the same code. But an `if` statement takes a boolean value, so converting `continueBool` to boolean with a comparison is pointless and, with a simple typo as in this example, wrong. – Pete Becker Jan 09 '17 at 11:53