when user inputs 'y' or 'Y' for "Do you wish to enter another year? (Y/N): ", loops "the month has 0 days" forever. Why?
I tried to look at the values and it seems like the values stored are being used again. Maybe I am not using cin.clear() correctly?
//variables
bool ucontinue = true; //answer to continue
int year = 0;
int month = 0;
int days = 0;
char answer = 'a';
//loop
while (ucontinue == true)
{
/*
Enter a year (Must be a positive integer): 2016
Enter a month (Must be a between 1 and 12): 2
The month has 29 days.
Do you wish to enter another year? (Y/N): y
*/
//year input
while (year <= 0)
{
cout << "Enter a year (Must be a positive integer): ";
cin >> year;
}
//month input
while (month <= 0)
{
cout << "Enter a month (Must be a between 1 and 12):";
cin >> month;
}
//# of days in the month
cout << "The month has " << days << " days." << endl << endl;
//continue?
while (answer != toupper('y') && answer != toupper('n'))
{
cout << "Do you wish to enter another year? (Y/N): ";
cin >> answer;
answer = toupper(answer);
if (answer == toupper('n'))
{
ucontinue = false;
}
}
cin.clear();
}
you code loops forever because of you while loops the the first time you run the program it works fine but the second time it goes around all the values are set for example the second time you go through the loop this while statement
while (year <= 0)
{
cout << "Enter a year (Must be a positive integer): ";
cin >> year;
}
won't run because year is already greater than 0 and this happens for all the while statements in your code. what would work is if you have do while statements instead of while statements because do while statements will run through the loop once before testing the condition it. like this:
do
{
cout << "Do you wish to enter another year? (Y/N): ";
cin >> answer;
answer = toupper(answer);
if (answer == toupper('n'))
{
ucontinue = false;
}
}while(answer != 'Y' && answer != 'N');
If I understand correctly you do the following:
Enter a year (Must be a positive integer): 2017
Enter a month (Must be a between 1 and 12):5
The month has 0 days.
Do you wish to enter another year? (Y/N): y <ENTER>
and your program loops. What happens it the following:
cin.clear();
is executed and your loop starts again. At this point year and month are still set. So when in the new loop iteration the line
while (year <= 0)
is encountered the condition is false and the loop continues to
while (month <= 0)
for this line the same is true. After that
cout << "The month has " << days << " days." << endl << endl;
is printed and the condition in
while (answer != toupper('y') && answer != toupper('n'))
is checked. As I have just entered y this condition is not true and
cin.clear();
is executed next, after this the loop restarts ad infinitum.
Your programs have many problems
you never updated or input something on the days variable so it will always be 0 as you set it in the beginning of the program
if (answer == toupper('n')) can be reduced to if (answer=='N')
Related
I'm needing to find how much tax $ any given number of taxpayers have to pay over any number of years. At the beginning of the program, # of taxpayers is entered, and # of years is entered. The while loop executes fine, and does what it's supposed to do the first time; however, it never loops back to the 'for' loop & asks for the next taxpayer's income. (I will note I have to do it this way as it's for a class)
for (int i = 1; i <= taxpayers; i++)
{
while (year <= years)
{
cout << "\n\nPlease enter payer " << i << "'s income for year " << year << ": $";
cin >> income;
if (income >= 0)
{
.......
year++;
}
else
{
cout << "\n *Error*"
cin.clear();
cin.ignore(INT_MAX, '\n');
continue;
}
}
}
it never loops back to the 'for' loop & asks for the next taxpayer's income
That is because after the while loop is finished the 1st time through, year has caught up to years, and so on subsequent iterations of the for loop, year <= years is always false.
You need to reset year back to its starting value on each iteration of the for loop, before entering the while loop:
for (int i = 1; i <= taxpayers; i++)
{
year = <your 1st year>; // <-- HERE
while (year <= years)
{
...
}
}
noob programmer here. Taking my first CS class in college and making first post on here so excuse me if the info i provide is not sufficient in advanced.
Still trying to figure out loops. Seem to get it but once there is loops within loops or if statements inside loops, I get thrown off and have no idea on how to proceed. For my assignment, I need the following to occur.
Would you like to process all the records in the file? (y/n) W
Please enter either y or n.
Would you like to process all the records in the file? (y/n) n
Enter number of records to process: two
XXXXXXXXXX Error-non numeric or negative value, try again
Enter number of records to process: 10
Here is my code:
char a = 0; //User chooses Y or N
int ProcessAmount = 0; //Amount of times to process if not all
cout << "Would you like to process all the records in the file? (y/n) ";
cin >> a;
do{
bool notDone = true;
if(a == 'n'){
while(notDone){
cout << "Enter records to process: ";
cin >> ProcessAmount;
if (cin.fail()){
cin.clear();
cin.ignore(40,'\n');
cout << "" << endl;
}
else{
notDone = false;
}
}
}else if(a != 'y' or a != 'n');
cout <<"Please enter either y or n." << endl;
}while( a != 'y');
Most problems are explained in comments, here is how I would fix it:
char a = 0; //User chooses Y or N
int ProcessAmount = 0; //Amount of times to process if not all
cout << "Would you like to process all the records in the file? (y/n) ";
cin >> a;
while (a != 'y') {
bool notDone = true;
if(a == 'n'){
while(notDone){
cout << "Enter records to process: ";
cin >> ProcessAmount;
if (cin.fail()){
cin.clear();
cin.ignore(40,'\n');
cout << "" << endl;
} else {
notDone = false;
}
}
} else if(a != 'y' or a != 'n') {
cout <<"Please enter either y or n." << endl;
cin >> a; // Need to get new input because old one is invalid.
}
};
Also I don't see how notDone is used. Also I would strongly advise of using proper indentation, spaces around keywords as while, for, if, else as it is good style.
You just put the y/n solicitation out of your loop then 'a' won't never change its value. Take a look of the change you may want:
do {
cout << "Would you like to process all the records in the file? (y/n/f) "; //f to break the loop
cin >> a;
bool notDone = true;
if (a == 'n') {
//. . .
} else if (a == 'y') {
//You may want to do something when yes
} else if (a != 'f')
cout <<"Please enter either y/n or f." << endl;
} while( a != 'f')
My homework is to write a program that finds the highest, lowest, and average of 5 numbers in an Array that the user inputs. Here is my problem, the user does not have to enter all 5 numbers. But has to enter at least 2 numbers minimum.
I have the whole program done already I am having a problem with the beginning, below is my code where I am having a problem:
// Ask for name and explain program
cout << "Please enter your name: ";
cin >> name;
cout << endl;
cout << "Hi " << name << ", please enter up to 5 whole numbers." << endl;
cout << "I will find the HIGHEST, LOWEST, and AVERAGE number." << endl;
// Loop through users input
for (int i = 0; i < SIZE; i++)
{
cout << "Enter number " << (i + 1) << " : ";
cin >> number[i];
// Validate that the user has entered atleast 2 numbers
if (i >= 1 && i < 4)
{
cout << "Do you wish to enter another number (Y/N)? : ";
cin >> continue_game;
// Validate that the user only enters Y/N
while (continue_game != 'Y' && continue_game != 'y' && continue_game != 'N' && continue_game != 'n')
{
cout << "Please type in (Y/N): ";
cin >> continue_game;
}
// What happens if user chooses NO
if (continue_game == 'N' || continue_game == 'n')
{
i = 5;
}
// What happens if user chooses YES
else if (continue_game == 'Y' || continue_game == 'y')
{
i = i;
}
}
}
PROBLEM: If the user presses no after the 2nd number the remaining elements get a number asigned to them like : -8251616. Is there any way to make sure that the elements get assigned a zero or stay blank please help its due tomorrow and I can not figure it out.
SIZE = 5
Don't set i = 5 when the user says no. Just end the loop with a break; statement.
Also, the i = i; statement in the yes case is useless.
When you're getting the highest, lowest, and average values, make sure you only look at the values from 0 to i-1, so you don't access the uninitialized elemends of the array.
If you really want zeros you need to fill array with zeros:
int number[5] = {};
or
int number[5];
for (int i = 0; i < 5; ++i) {
number[i] = 0;
}
However this will give wrong output if the user enter less than 5 numbers. What you should do is to count how many numbers user entered and then use values from 0 to count - 1.
Advice, use break; instead of i = 5;.
This probably requires only basic problem solving skills but I am trying to create a very short way to: ask the user for input and only say that the input is not legit after it did not pass the condition check at least once. A do while loop seemed to fit..
How I implemented it below will always create a problem in that 1 specific number creates a bug, namely if the user enters 0 you don't get the message while you should, I could change it to a very special number but still this front-end bug would remain. I come from a Java background in which null is the default value making this easy, but because of efficiency reasons this seems not to be the case in c++. I could declare a bool but that seems like too much of a workaround.
Here is the code I have now:
int birthYear = 0;
do {
if (birthYear != 0) cout << "Your input " << birthYear << " is not a legit birthday";
cout << "What is the year you were born?: "; cin >> birthYear;
} while (birthYear < 1900 || birthYear > 2100);
This is a classic example where neither the while nor the do/while loop is an exact fit, because you need to do something before and after checking the loop condition.
Rewrite your loop as a "forever" loop with a break in the middle:
for (;;) {
cout << "What is the year you were born?: ";
cin >> birthYear;
if (birthYear >= 1900 && birthYear <= 2100) {
break;
}
cout << "Your input " << birthYear << " is not a legit birthday";
}
You could try adding a flag:
int birthYear = 0;
bool birthYearInvalid = false;
do {
if (birthYearInvalid) cout << "Your input " << birthYear << " is not a legit birthday";
cout << "What is the year you were born?: "; cin >> birthYear;
birthYearInvalid = (birthYear < 1900 || birthYear > 2100);
} while (birthYearInvalid);
One extra variable.. could be good (more self documentation)
I think the question speaks for itself, I'm writing a program in c++ and there is a part where the console asks the user which type of input they want to use
while (loop == 5) {
cout << "\nWould you like to enter a depoist or a check? "; //asks for a choice
cin >> choice;
//determines whether or not to close the program
if(choice == 0 || depo == 0 || check == 0) {
return 0;
}//end close if
//choses which type of input to make
if( choice == 1) {
cout << "\nPlease enter check amount: ";
cin >> check;
check += check;
} else if(choice == 2) {
cout << "\nPlease enter deposit amount: ";
cin >> depo;
depo += depo;
}//end if
}
but how do i keep track of how many times the if statement was true?
You can add a counter and increment it every time you enter the if-statement's true block.
int true_counts = 0;
while (loop == 5){
...
if( choice == 1){
true_counts++;
...