Program doesn't work as it should.
Code:
#include <iostream>
#include <cctype>
using namespace std;
int main()
{
cout << "Enter whole numbers that you want me to total up:"
<< endl;
int value = 0; //value to be added
int total = 0; //total of all values
char yesno = 0; //check if user wants to continue
do
{
cout << "Enter a whole number: "; //let user enter number
cin >> value;
total += value; //add value to total
cout << "Do you want to add some more numbers? (y/n) "; //check if user wants to add more numbers
cin >> yesno;
while (tolower(yesno) != ('y'||'n')) //tell user that they have entered wrong value
{
cout << "You have entered an invalid answer. Please try again (y/n): ";
cin >> yesno;
}
}while (tolower(yesno) == 'y'); //loop stops if user doesn't enter 'y'
cout << endl //tell user the total
<< "The total is "
<< total
<< "."
<< endl
<< endl;
system("PAUSE");
return 0;
}
The part in red seems to be the problem. The loop goes on and on even if i enter 'y' or 'n' . What's wrong?
P.S one more questions: wat does header file <cstdlib> and <cstdio> do? The first book I read 'bout C++ seems to 'enforce' the two header files in all the example programs. But in the book <<Ivor Horton's Beginning C++ The complete language>> by the Wrox company , they only put <iostream> for the basic programs. I'm on to the chapter 'bout loops, but headers file they have used so far are: <iostream> <limits> <cctype> <iomanip> . Can someone explain?