Hey so this is really getting on my nerves.
I'm trying to validate user input within a loop.
I need the user input to be between 0 and 60. I can validate it no problem but what I want it to do is re-ask the previous question if the input is incorrect, you know? like repeat the loop if that makes sense
int main()
{
//Constants
const int MAXROUNDS = 4;
const int NUMARCHERS = 3;
//Variables
int archerNum;
int roundNum;
double score;
double total;
//Start of outer loop, this loop displays each Archer
for (archerNum = 1; archerNum <= NUMARCHERS; archerNum++)
{
total = 0; //This clears the total for the archer
//Start of second loop, this loop displays each round
for (roundNum = 1; roundNum <= MAXROUNDS; roundNum++)
{
cout << "Enter Round " << roundNum << " score for Archer "
<< archerNum << ": ";
cin >> score;
if (score < 0 | score > 60)
{
cout << "ERROR! Number must be between 0 and 60!";
}
}
total = score + score + score + score; //This calculates the total score for the tournament
cout << "\nThe total score for archer " << archerNum << " is: "
<< total << "\n\n";
}
return 0;
}
This is my code ^
Now I have tried so many things. I've looked through my textbook and I've googled everything but I cant seem to find the answer.
I've tried putting the error message in a do-while loop
i've used if-else statements
i've used while loops
I think I've literally used every different type of loop there is and I still cant seem to figure it out and I'm becoming very frustrated.
Thanks in advance
Here is a simple version of just the number input and error message:
//#include "pch.h" if using Visual Studio 2017
#include <iostream>
using namespace std;
int main(){
cout << "Please enter a number between 0 and 60." << endl;
int input;
cin >> input;
while(input < 0 || input > 60 || cin.fail()){ //Using cin.fail() here in case the user enters a letter or word
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cerr << "Number must be between 0 and 60. Please try again." << endl;
cin >> input;
}
return 0;
}`
Alternatively, you could use a goto statement though, as I am sure many others on this site will tell you, that is not recommended as it leads to the infamous 'spaghetti code'.
Simply add roundNum -= 1; inside the validation If statement. It will make the counter decreased by 1, and re-ask the previous question
//Start of second loop, this loop displays each round
for (roundNum = 1; roundNum <= MAXROUNDS; roundNum++)
{
std::cout << "Enter Round " << roundNum << " score for Archer "
<< archerNum << ": ";
std::cin >> score;
if (score < 0 || score > 60)
{
std::cout << "ERROR! Number must be between 0 and 60!"<<endl;
roundNum -= 1; //or roundNum--
}
}
Try:
bool inputError;
do {
inputError = false;
std::out << "Enter Round "
<< roundNum
<< " score for Archer "
<< archerNum << ": ";
if (std::cin >> score) {
if (score < 0 || score > 60)
{
std::cout << "ERROR! Number must be between 0 and 60!";
inputError = true;
}
}
else {
std::cout << "ERROR! Your input must be a number";
inputError = true;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max());
}
} while(inputError == true);
Related
C++ newbie here. I'm not sure how to describe this but the task outside of the while-loop won't execute immediately. I need to enter the input value again to get it done.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int fourDigitInt, firstDigit, secondDigit, thirdDigit, fourthDigit, i = 0;
cout << "Enter a 4-digit integer : ";
cin >> fourDigitInt;
while (fourDigitInt > 9999 || !(cin >> fourDigitInt))
{
i++;
if (i >= 3)
{
cout << "You do not seem to understand the program instruction.\nPlease try again later." << endl;
return -1;
}
else
{
cout << "Error: Please make sure you are entering a 4-digit integer" << endl;
cin.clear();
cin.ignore(4, '\n');
cout << "Enter a 4-digit integer : ";
}
}
firstDigit = fourDigitInt / 1000 % 10;
secondDigit = fourDigitInt / 100 % 10;
thirdDigit = fourDigitInt / 10 % 10;
fourthDigit = fourDigitInt / 1 % 10;
cout << "1st digit : " << firstDigit << endl;
cout << "2nd digit : " << secondDigit << endl;
cout << "3rd digit : " << thirdDigit << endl;
cout << "4th digit : " << fourthDigit << endl;
}
Here are some problems I encountered:
1)If I enter a string first, it doesn't have any problem.
2)But if I enter any number less than 9999, it won't execute the calculation unless I
enter it again.
3)If I enter a 5-digit number the endl won't work. It will just display Enter a 4-digit integer : Error: Please make sure you are entering a 4-digit integer which suppose to be a different line.
Where exactly did I do wrong? Thank you in advance.
The main issue here is in the while loop condition. It should just check for the value of the fourDigitInt variable as that is what is important.
Looking closer, you will also be able to notice the fact that the if case inside of the loop would check for the second iteration instead of the third. I fixed that as well by moving the i++ inside of the else block.
while (fourDigitInt > 9999 || fourDigitInt < 1000) {
if (i >= 3) {
cout << "You do not seem to understand the program (ironic coming for OC) instruction.\nPlease try again later." << endl;
return -1;
}
else {
i++;
cout << "Error: Please make sure you are entering a 4-digit integer" << endl;
cout << "Enter a 4-digit integer : ";
cin >> fourDigitInt;
}
}
Any other issues with your code that may occur are not related to this question.
I am fairly new to C++, I am trying to write a code that allows input number of reviewers then allows number of reviewers to enter movie rating and display asterisks based on the number input. I am having difficulty incorporating an if statement that display "Movie ratings must be from 1 to 5." when the user input any number that's outside of 1 to 5. Another thing when it does work, it still continues the for loop of cout << "\nReviwer " << r << " rating: " ; instead of stopping and restarting. Any assistance is appreciated.
complied code
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
if (rating < 1 || rating > 5)
cout << "Movie ratings must be from 1 to 5." << endl;
else {
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end if
} //end for
Output example should be like this
You should have
#include <iostream>
#include <string>
using namespace std;
int main(){
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
while (rating < 1 || rating > 5){
cout << "Movie ratings must be from 1 to 5." << endl;
cout << "\nReviwer " << r << " rating: ";
cin >> rating;
}
if (rating >= 1 && rating <=5){
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end if
} //end for
}
};
It's not optimised but do the trick
if your mean is the reviewer must input the number between 1 and 5,
you can use a do while loop like this:
do
{
//enter rating;
}while (rating < 1||rating >5);
//print out rating;
I suppose you aim at something like this:
int reviewers;
int rating;
//input how many reviewers will be reviewing
cout << "How many reviewers? ";
cin >> reviewers;
cout << "Movie ratings must be from 1 to 5." << endl;
for (int r = 1; r <= reviewers; r++) {
while(true) {
cout << "\nReviewer " << r << " rating: ";
cin >> rating;
if (rating < 1 || rating > 5) {
cout << "Movie ratings must be from 1 to 5." << endl;
} else {
break;
}
}
for (int j = 1; j <= rating; j++) {
cout << "* ";
} //end for
cout << endl;
} //end for
Here is some code I quickly put together. I hope the comments are useful and if you have any questions just ask!
The main part is the recursive function get_ratings which will loop forever until it returns 1
// All this function does is returns the correct amount of stars
// E.G. make_stars(4) returns " * * * *"
string make_stars(int star_Count) {
string stars;
for (int i = 0 ; i < star_Count ; i++) {
stars += " *";
}
return stars;
}
// We get the ratings and returns 1 or 0 depending of it succeeded or failed
int get_ratings(int reviewer_count) {
// We initialise the ratings integer
int rating;
// We loop through all reviewers
for (int i = 0 ; i < reviewer_count ; i++) {
// We do i + 1 so it is more readable (usually in English we start at 1 not 0 like computers)
cout << "What is reviewer " << (i + 1) << "'s rating?" << endl;
//We get the user input
cin >> rating;
// We check to see if rating is withing the range. We could also do is NOT in the range and flip line 27 and 29
if (1 <= rating && rating <= 5) {
// If it is within range we will print the correct amount of stars
cout << make_stars(rating) << endl;
} else {
// We return 0 so we can determine the function "failed"
return 0;
}
}
// We return 1 so we can determine the function "succeeded"
return 1;
}
// This is a recursive function (it can run itself)
int get_ratings_rec(int reviewers) {
cout << "All ratings must be given between 1 and 5 (inclusive)" << endl;
// get_ratings_status is equal to 1/0 depending on if get_ratings() succeeded or failed
int get_ratings_status = get_ratings(reviewers);
if (get_ratings_status == 1) {
// If it was a success we print "Success!"
cout << "Success!" << endl;
} else {
// If it was a failure we tell the user and run get_ratings_loop() again until it succeeds
cout << "Failed, please try again\n" << endl;
get_ratings_loop(reviewers);
}
}
// Our main entry point to the program
int main() {
// We initialise the reviewers integer
int reviewers;
cout << "How many reviewers?\n>>> " << endl;
cin >> reviewers;
// We run get_ratings_loop() with the integer given
get_ratings_loop(reviewers);
}
You'd want to keep asking the number of stars if the user inputs a value outside the range, something like:
for (int r = 1; r <= reviewers; r++)
{
cout << "\nReviwer " << r << " rating: ";
do
{
cin >> rating;
if (rating < 1 || rating > 5) // print error message
cout << "Movie ratings must be from 1 to 5." << endl;
} while (rating < 1 || rating > 5); // repeat the loop if out of range
for (int j = 1; j <= rating; j++)
{
cout << "* ";
} //end for
cout << endl;
}
Note that you should be doing input validation also, e.g., if the input is an alphabetic character, your code will trigger an infinite loop, here an example of a possible solution:
#include <limits>
//...
do
{
if (!(cin >> rating))
{
std::cout << "Bad input, try again";
cin.clear(); //clear error flags
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // clear buffer
rating = 0;
}
if (rating < 1 || rating > 5)
cout << "Movie ratings must be from 1 to 5." << endl;
} while (rating < 1 || rating > 5);
I am having trouble with a homework assignment where I need to check if the ISBN code is valid. This is a beginner C++ class, and I am new to all of this. Right now, no matter what ISBN I put in, it tells me they are all valid. If someone could help point me in the right direction to figure out if my formula is correct when in my for() statement.
I have no problem with the outside loops, it's just the conversion I believe I am doing wrong.
being this is an intro c++ class, we are still only using basic functions, so the professor suggested reading in the ISBN and storing it under a char variable.
**#include <iostream>
using namespace std;
int main()*
{
int Counter;
int WeightedSum;
char ISBN[11] = { 0 };
char Choice;
char Again{};
int Sum = 0;
const char X = 10;
int IterationCounter = 0;
do // begininng of do/while loop
{
cout << "Would you like to check if your ISBN is valid or not? ";
cout << "Press Y/y for yes or N/n for no and to end program: ";
cin >> Choice; //choice input
cout << endl << endl; // blank line
while (Choice != 'Y' && Choice != 'y' && Choice != 'N' && Choice != 'n') // beginning of while loop. checks user input to see if valid y/n choice
{
cout << "Invalid choice! Enter either Y/y or N/n"; // displays when anything other than y/n is entered
cout << "Press Y/y for yes or N/n for no and to end program: "; //gives user another chance to enter y/n
cin >> Choice;
cout << endl << endl;
}
if (Choice == 'Y' || Choice == 'y')
{
cout << "Enter the ISBN you wish to convert: " << endl;
cin >> ISBN;
cout << ISBN;
cout << endl << endl;
for (Counter = 0; Counter < 10; Counter++)
{
WeightedSum = ISBN[10] * (10 - Counter);
Sum = WeightedSum + Sum;
}
cout << Sum;
if (Sum % 11 == 0)
{
cout << Sum;
cout << " Is a valid ISBN " << endl;
}
else
{
cout << Sum;
cout << " Is invalid " << endl;
}*
The problem is this line
WeightedSum = ISBN[10] * (10 - Counter);
You're always performing math against the character at position 11. Because you multiply it by 10 - Counter, which is 0 through 9, you're going to basically be multiplying it by the sum of 1 through 10, or 55. Since 55 is divisible by 11, you'll always end up with if (Sum % 11 == 0) evaluating to true. Worst case, you may even hit an access violation since you're not checking the length of ISBN before accessing it.
So I've been reading a C++ Primer that has all kinds of examples and test scenarios inside that are good to make sure each new chapter has been correctly learnt and that there's no other way to improve that actually coding.
The C++ Primer asked for this "Practice Problem" to be attempted and is as follows:
Write a program that provides the option of tallying up the results of a poll with 3 possible values. The first input to the program is the poll question; the next three inputs are the possible answers. The first answer is indicated by 1, the second by 2, the third by 3. The answers are tallied until a 0 is entered. The program should then show the results of the poll—try making a bar graph that shows the results properly scaled to fit on your screen no matter how many results were entered.
So I was curious as to how the author wanted me to make sure that the results fit the screen resolution regardless of "how many results were entered" and after entering the main syntax for the loops that input the data and display. I was curious what the best way to go about this and slapped together a very, VERY simple work around that'll divide by a 1:10 ratio dependant on the highest result input (up to 1000/100)
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Variable decleration
string Question;
int tally1 = 0, tally2 = 0, tally3 = 0;
int input = 1;
int resultRatio;
//Question input and text fluff
cout << "Please enter a poll that has 3 answers: ";
getline(cin, Question, '?');
cout << "Your question is: " << Question << "?" << endl;
cout << "When results are complete for specified poll answer, enter 0 to input results" << endl;
while (1) //Tally 1 answer input.
{
cout << "Please enter the tallies for answer A: ";
cin >> input;
tally1 = tally1 + input;
if (input != 0)
{
cout << "A's current tallies: " << tally1 << endl;
continue;
}
cout << endl;
break;
}
while (1) //Tally 2 answer input.
{
cout << "Please enter the tallies for answer B: ";
cin >> input;
tally2 = tally2 + input;
if (input != 0)
{
cout << "B's current tallies: " << tally2 << endl;
continue;
}
cout << endl;
break;
}
while (1) //Tally 3 answer input.
{
cout << "Please enter tallies for answer C: ";
cin >> input;
tally3 = tally3 + input;
if (input != 0)
{
cout << "C's current tallies: " << tally3 << endl;
continue;
}
cout << endl;
break;
}
// Ratio in which total tallies should be divded by before bar chart display
if (tally1 >= 10 || tally2 >= 10 || tally3 >= 10)
{
resultRatio = 10;
}
else if (tally1 >= 100 || tally2 >= 100 || tally3 >= 100)
{
resultRatio = 100;
}
else if (tally1 >= 1000 || tally2 >= 1000 || tally3 >= 1000)
{
resultRatio = 1000;
}
else
{
resultRatio = 1;
}
//Simple bar chart to display all the results in a ratio that'll fit the screen thanks to resultRatio division
cout << "All results have been entered, here is the barchart (Results displayed are divided by " << resultRatio <<"):";
cout << endl << "A:" << "(" << tally1 << "votes )";
for (int i = tally1 / resultRatio; i > 0; i--)
{
cout << "o";
}
cout << endl << "B:" << "(" << tally2 << "votes )";
for (int i = tally2 / resultRatio; i > 0; i--)
{
cout << "o";
}
cout << endl << "C:" << "(" << tally3 << "votes )";
for (int i = tally3 / resultRatio; i > 0; i--)
{
cout << "o";
}
cout << "\nHere is the full bar graph on input results";
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am new to programming and I need help with my term project. I have made a program that simulates a hotel booking, the problem is that whenever a non-whole number is entered for the first question, it goes into an infinite loop. If you get to the second question and enter a non-whole number it accepts it as a whole number by dropping off anything that comes after the decimal and then skips the next question and stops running the program.
#include <iostream>
#include <string>
using namespace std;
int stay_length (int stay)
{
int nights = stay;
int total = 0*nights;
return total;
}
int rooms_booking(int rooms)
{
int rooms_booked = rooms;
int total = 0;
if(rooms_booked > 0)
{
total = rooms_booked * 50;
}
else
{
total = 0;
}
return total;
}
int main(){
int x;
string repeat;
int nights;
int total = 0;
int rooms_avail = 10;
int rooms;
cout << "Hello! Welcome to Hotel COMP 150." << endl;
do {
if (rooms_avail > 0) {
cout << "How many nights will you be staying?" << endl;
}
else {
cout << "Sorry, but there are no rooms available. " << endl;
}
do {
cin >> nights;
if (nights > 0 && nights <= 28)
{
int total1 = stay_length(nights);
cout << "You are staying for " << nights << " nights" << endl;
cout << "That costs: $" << total1 << endl;
total = total + total1;
}
else
{
cout << "You cannot stay less than 1 or more than 28 nights" << endl;
}
} while (nights <= 0 || nights >28);
if (rooms_avail > 0)
{
cout << "How many rooms will you be booking?" << endl;
cout << "There are " << rooms_avail << " available." << endl;
cin >> rooms;
if (rooms > 0 && rooms <= rooms_avail)
{
int total2 = rooms_booking(rooms);
cout << "You are booking " << rooms << " rooms." << endl;
cout << "That costs : $" << total2 << endl;
total = total + total2;
rooms_avail = rooms_avail - rooms;
}
else if (rooms <= 0 || rooms > rooms_avail)
{
do{
cout << "You can only book a minimum of 1 room or a maximum of " << rooms_avail << endl;
cin >> rooms;
} while (rooms <= 0 || rooms > rooms_avail );
int total2 = rooms_booking(rooms);
cout << "You are booking " << rooms << " rooms." << endl;
cout << "That costs : $" << total2 << endl;
total = total + total2;
rooms_avail = rooms_avail - rooms;
}
else
{
cout << "You cannot book more than " << rooms_avail << endl;
}
}
else
{
cout << "Sorry, all rooms have been booked." << endl;
}
cout << "Your total so far is: $" << total << endl;
cout << "Would you like to make another booking? Enter 'Y' or 'y' to do so." << endl;
cin >> repeat;
}while(repeat == "Y" || repeat == "y");
return 0;
}
It's always better to use std::getline() instead of operator>> to read interactive input from std::cin.
operator>> is not for reading a single line of text from standard input, and storing it. That's what std::getline() is for.
If the wrong kind of input is entered, not what operator>> expects, it sets std::cin to a failed state, which makes all future attempts to read std::cin immediately fail, resulting in the infinite loop you are observing.
To do this correctly, it is going to be either:
1) Always check fail() after every operator>>, to see if the input failed, if so recover from the error with clear(), then ignore(). This gets real old, very quickly.
2) It's much easier to read a single line of text with std::getline(), then parse the input yourself. Construct a std::istringstream, if you wish, and use operator>> with that, if that makes it easier for you.
You can achieve basic user-input error checking via the console with something along these lines:
int nights = 0;
// error checking loop
while(1) {
std::cout << "How many nights will you be staying?" << endl;
std::cin >> nights;
// input valid
if(!std::cin.fail() && (std::cin.peek() == EOF || std::cin.peek() == '\n')
&& nights > 0 && nights <= 28) {
// do stuff
break; // break from while loop
}
// input invalid
else {
std::cin.clear();
std::cin.ignore(256, '\n');
std::cout << "An input error occurred." << std::endl;
}
}