-3

I am doing a project for school and we have to create a loop quote system. I am trying to get the code to prompt the user to ask if they want to do another quote.

If the user inputs 'N' the code does the end program statement as well as the incorrect response statement. Any help would be appreciated, the code is below and the output is below the code.

if (runQuote = 'N' || 'n') {
    cout <<"Thank you for using this program.  Goodbye. \n";
} else {
   " ";
}

if (runQuote != 'Y' || 'y' || 'N' || 'n') {
    cout << "Sorry but the response you answered is not valid, 
      Would you like to process another quote (Y/N)?\n";
    cin >> runQuote;
}

The output if I input 'N' after the prompt is:

  Thank you for using this program.  Goodbye.
  Sorry but the response you answered is not valid,
    Would you like to process another quote (Y/N)?
Steve E.
  • 9,003
  • 6
  • 39
  • 57
Garo
  • 1

2 Answers2

2

Your problem is in your if statement. Instead of

if (runQuote = 'N' || 'n')

You should have

if (runQuote == 'N' || runQuote == 'n')

'n' is evaluated to true in your original code

Michael Surette
  • 701
  • 1
  • 4
  • 12
1

You want the program to quit if the user inputs 'N' or 'n'. So your check should be like this:

if ((runQuote == 'N') || (runQuote == 'n'))

It is good to invest time in a good book rather than guessing the syntax of a language, especially one like C++.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • I made the changes suggested above and I am still receiving the same result. Also I have the C++ textbook for my college course but couldn't find a solution that worked. – Garo Feb 19 '19 at 05:25
  • You have to change this if statement also: `if (runQuote != 'Y' || 'y' || 'N' || 'n') ` accordingly. – P.W Feb 19 '19 at 05:27
  • I did that as well already. The new code is this now: if (runQuote != 'Y' || runQuote != 'y' || runQuote != 'N' || runQuote != 'n') – Garo Feb 19 '19 at 05:29
  • @Garo "The new code is this now: if (runQuote != 'Y' || runQuote != 'y' || runQuote != 'N' || runQuote != 'n')" - That's obviously wrong. It will always be `true`. *Any* character will satisfy at least one of the conditions, thus making the entire expression true. Think about 'n' for example; is 'n' different from 'Y'? Yes -> `true`. What about 'Y' then? Well, it's not different from the first term 'Y', but there's an OR (`||`) there, and it *is* different from 'y', so -> `true`. Same if you put in *any* other letter. – Jesper Juhl Feb 19 '19 at 06:59