I have this code that asks for a number input and stores it in a variable. I'm trying to do validation on the input but it's behaving weirdly.
#include <iostream>
using namespace std;
float coursework_mark;
float exam_mark;
float module_mark;
int main() {
//COURSEWORK INPUT WITH VALIDATION
cout << "Please enter your coursework mark: ";
while(!(cin >> coursework_mark)){
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid data type, please enter valid coursework mark: ";
}
while (coursework_mark < 0 || coursework_mark > 100) {
cout << "Out of range, please enter valid coursework mark: ";
cin >> coursework_mark;
}
//EXAM MARK INPUT WITH VALIDATION
cout << "Please enter your exam mark: ";
while(!(cin >> exam_mark)) {
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid data type, please enter valid exam mark: ";
}
while (exam_mark < 0 || exam_mark > 100) {
cout << "Out of range, please enter valid exam mark: ";
cin >> exam_mark;
}
//Coursework capping
if (coursework_mark > exam_mark * 2) { coursework_mark = exam_mark * 2;}
//Calculate Module mark
module_mark = (coursework_mark* 0.4) + (exam_mark* 0.6);
//Output results
cout << coursework_mark << endl;
cout << "Your module mark is " << module_mark << endl;
if (module_mark >= 50) {
cout << "Congratulations you have passed!" << endl;
} else if (module_mark < 50) {
cout << "Unfortunately, you have not passed" << endl;
}
}
If user inputs '45kuefggf' the number 45 gets stored as the coursework mark and the code hits the line cout << "Out of range, please enter valid exam mark: ";. I have no idea why it's doing this, how do I make it so that it check if the user input a valid data type?
Instead of
while(!(cin >> coursework_mark)){
you should use std::getline
std::getline(std::cin, coursework_mark);
https://en.cppreference.com/w/cpp/string/basic_string/getline
bool has_only_digits(string s){
return s.find_first_not_of( "0123456789" ) == string::npos;}
This method is the simplest way I found to check if a string contains a number. So if it returns true the string has only digits else it contains more than just digits.
If the statement is false you can then clear the cin like you have done above.
Related
this is the code i wrote for simple grading exams (im still a very beginner) but when i do a wrong input in (Grades) it doesnt go to the function i made which is called (FalseInput) to make the user able to re-enter the (Grades) any suggestions to how to solve?
and how to improve in general ?
here is an example of whats the problem :
Please Type Your Name : rafeeq
Please Insert The Grade : as (which is an input error)
you failed
thanks.
#include <iostream>
#include <string>
using namespace std;
char Name[30];
int Grades;
const int MinGrade(50);
void FalseInput() {
cout << "pleae enter the number again : ";
cin >> Grades;
if (Grades >= MinGrade) {
cout << Name << " : " << "you passed\n";
cout << Grades;
} else if (Grades < MinGrade and cin.fail() == 0) {
cout << "you failed\n";
} else if (cin.fail() == 1) {
cout << "its not a valid number\n";
cin.clear();
cin.ignore(1000, '\n');
cout << endl;
FalseInput();
}
}
int main() {
cout << "Please Type Your Name : ";
cin.getline(Name, 30);
cout << "Please Insert The Grade : ";
cin >> Grades;
if (Grades >= MinGrade) {
cout << Name << " : " << "you passed\n";
cout << "The Grade Achieved : " << Grades << "%";
} else if (Grades < MinGrade) {
cout << "you failed\n";
} else if (cin.fail() == 1) {
cout << "its not a valid number\n";
cin.clear();
cin.ignore(1000, '\n');
cout << endl;
FalseInput();
}
return 0;
}
You don't check if the extraction of an int succeeds here:
cin >> Grades;
You can check the state of the input stream after extraction like this and it needs to be the first condition or else the program will make the comparisons with MinGrade first and will get a true on Grades < MinGrade.
if(!(cin >> Grades)) {
if(cin.eof()) {
// You can't recover the input steam from eof so here you need
// to handle that. Perhaps by terminating the program.
}
cin.clear();
cin.ignore(1000, '\n');
cout << endl;
FalseInput();
} else if(Grades >= MinGrade) {
cout << Name << " : " << "you passed\n";
cout << "The Grade Achieved : " << Grades << "%";
} else if(Grades < MinGrade) {
cout << "you failed\n";
}
You do have a lot of unnecessary code duplication and you also use an array of char to read the name - but you have included <string> so I assume you're familiar with std::string. I suggest using that.
Simplification:
#include <iostream>
#include <limits>
#include <string>
int main() {
const int MinGrade = 50;
std::string Name;
int Grades;
std::cout << "Please Type Your Name : ";
if(std::getline(std::cin, Name)) {
while(true) {
std::cout << "Please Insert The Grade : ";
if(!(std::cin >> Grades)) {
if(std::cin.eof()) {
std::cout << "Bye bye\n";
break;
}
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "That's not a valid number!\nPlease enter the "
"number again!\n";
} else if(Grades >= MinGrade) {
std::cout << Name << " : " << "you passed\n";
std::cout << "The Grade Achieved : " << Grades << "%\n";
break;
} else { // no need to check "Grades < MinGrade" here
std::cout << "you failed\n";
break;
}
}
}
}
What is happening: When that string "as" is attempted to write to the integer Grades, cin.fail() is set and Grades has the default of 0 written to it (I think that's right)
C++ cin reading string into int type returns 0
All-in-All: Input validation is needed BEFORE you check it's values.
Here is one approach, check if cin was able to successfully convert:
https://www.hackerearth.com/practice/notes/validating-user-input-in-c/
Another approach would be to read cin into a string instead of int, then you can control how to convert/cast it to whatever form you want (more work, but being that you are new - you will learn a lot doing this).
Secondary Note: Your string of 50 characters for name - imagine what would happen if you wrote 60 characters of input. There will be more data than the container can hold, thus writing past the bounds and into his neighbor (could cause a segment fault, or worse - crazy unexpected behavior)
Why does this work? Using cin to read to a char array smaller than given input
this is my first post here. I am new to c++ (just started last week) and have spent a few hours on this and am stumped.
I am aware that I am probably doing many things wrong in this program, but I assure you all I have tried my best. Validating inputs is beyond the scope of my homework, but I wanted to try it out since just getting inputs and returning them is boring.
Basically, the input validation works for the outer loop, but with the inner loops it will fall through even if invalid.
#include <iostream>
using namespace std;
//Global Variables
int cubeLength = 0;
int cubeWidth = 0;
int cubeHeight = 0;
int cubeSurfaceArea = 0;
int cubeVolume = 0;
bool valid = false;
int main() {
//Ask user for cubeLength and validate input for integer values
do {
cout << "Please enter a numerical value for the length of a cube" <<endl;
cin >> cubeLength;
if (cin.good()) {
valid = true;
//Ask user for cubeWidth and validate input for integer values
do {
cout << "Please enter a numerical value for the width of a cube" <<endl;
cin >> cubeWidth;
if (cin.good()) {
valid = true;
//Ask user for cubeHeight and validate input for integer values
do {
cout << "Please enter a numerical value for the height of a cube" <<endl;
cin >> cubeHeight;
if (cin.good()) {
valid = true;
}
else
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid cube height. Please try again" << endl;
}
}while (!valid);
}
else
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid cube width. Please try again" << endl;
}
}while (!valid);
}
else
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid cube length. Input is not an integer" << endl;
}
} while (!valid);
//Perform calculations for surface area and volume then assign them to their associated variables
if (cubeLength >= 1 && cubeWidth >= 1 && cubeHeight >= 1)
{
valid = true;
cubeSurfaceArea = ((2*(cubeWidth*cubeLength))+(2*(cubeLength*cubeHeight))+(2*(cubeWidth*cubeHeight)));
cubeVolume = (cubeWidth*cubeLength*cubeHeight);
}
else {
cout << "Sorry, one or more cube inputs is invalid. Ending program. Please restart and try again." << endl;
return 0;
}
//Output surface area and volume to user
cout << "Length = " << cubeLength << " Width = " << cubeWidth << " Height = " << cubeHeight << endl;
cout << "The surface area of your cube is " << cubeSurfaceArea << "." << endl;
cout << "The volume of your cube is " << cubeVolume << "." << endl;
//Pause system and end program
return 0;
}
I added the if statement for the calculations at the bottom to stop this from falling all of the way through the program and exit.
I have also checked a lot of similar questions on validating inputs for integers and loops on this site and others, but haven't been able to figure it out. My theory is that i'm either messing up the boolean logic for valid, or using the wrong looping method.
The main problem with the loops is that you are setting valid to true but never to false, so the statement while (!valid) will never evaluate to false.
Another general comment is that the layout of the code has too many nested loops. This can be simplified a lot.
I did not test the code below, but this type of structure is much easier to read - ie doing each input separately, rather than jumbling the all together! :-)
//Ask user for cubeLength and validate input for integer values
valid = true;
do {
cout << "Please enter a numerical value for the length of a cube" <<endl;
cin >> cubeLength;
if (!cin.good()) {
valid = false;
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid cube length. Input is not an integer" << endl;
}
} while (!valid);
//Ask user for cubeWidth and validate input for integer values
do {
cout << "Please enter a numerical value for the width of a cube" <<endl;
cin >> cubeWidth;
if (!cin.good()) {
valid = false;
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid cube width. Please try again" << endl;
}
} while (!valid);
//Ask user for cubeHeight and validate input for integer values
do {
cout << "Please enter a numerical value for the width of a cube" <<endl;
cin >> cubeWidth;
if (!cin.good()) {
valid = false;
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid cube height. Please try again" << endl;
}
}while (!valid);
After the first time you set valid = true, it stays true till the end. You should take it back to false before testing it again.
Thank you to everyone who provided help and feedback:
The issue is painfully obvious to me now, which is the boolean value not being reset at the beginning of the loop after a failed validation on input.
I redid the entire code per suggestions and have a stable program now!:)
#include <iostream>
using namespace std;
//Global Variables
int cubeLength = 0;
int cubeWidth = 0;
int cubeHeight = 0;
int cubeSurfaceArea = 0;
int cubeVolume = 0;
bool valid = true;
char choice;
int main() {
do {
//Ask user for cubeLength and validate input for integer values
do {
valid = true;
cout << "Please enter a numerical value for the length of a cube" <<endl;
cin >> cubeLength;
if (cin.fail()) {
valid = false;
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid cube length. Input is not an integer" << endl;
}
} while (!valid);
//Ask user for cubeWidth and validate input for integer values
do {
valid = true;
cout << "Please enter a numerical value for the width of a cube" <<endl;
cin >> cubeWidth;
if (cin.fail()) {
valid = false;
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid cube width. Input is not an integer" << endl;
}
} while (!valid);
//Ask user for cubeHeight and validate input for integer values
do {
valid = true;
cout << "Please enter a numerical value for the height of a cube" <<endl;
cin >> cubeHeight;
if (cin.fail()) {
valid = false;
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "Invalid cube height. Input is not an integer" << endl;
}
}while (!valid);
//Perform calculations for surface area and volume then assign them to their associated variables
if (cubeLength >= 1 && cubeWidth >= 1 && cubeHeight >= 1)
{
cubeSurfaceArea = ((2*(cubeWidth*cubeLength))+(2*(cubeLength*cubeHeight))+(2*(cubeWidth*cubeHeight)));
cubeVolume = (cubeWidth*cubeLength*cubeHeight);
}
else {
cout << "Sorry, one or more cube inputs is invalid. Ending program. Please restart and try again." << endl;
return 0;
}
//Output surface area and volume to user
cout << "Length = " << cubeLength << " Width = " << cubeWidth << " Height = " << cubeHeight << endl;
cout << "The surface area of your cube is " << cubeSurfaceArea << "." << endl;
cout << "The volume of your cube is " << cubeVolume << "." << endl;
cout << "Would you like to try again? (y/n)" << endl;
cin >> choice;
} while (choice != 'n');
//Pause system and end program
return 0;
}
I'm pretty new to c++ and am really close to the solution, but I still need some help. My loop works correctly the first time. After that when I enter the car number, it seems to be grabbing some input somewhere and just executes the invalid color on the second pass. Obviously, I'm missing something, but I'm at a loss. Any help would be appreciated.
This is just a small snippet of my program, but there lays the problem:
while (count < 3)
{
cout << endl << "Enter car color: blue, red or green in lower case. ";
getline(cin, carColor[count]);
if (!(carColor[count] == "blue" || carColor[count] == "red" || carColor[count] == "green"))
{
cout << "That is an invalid color"
<< "The program will exit";
cin.clear();
cin.ignore();
return 0;
}
cout << endl << "Enter car number between 1 and 99: ";
cin >> carNumber[count]; // Enter car number
if (carNumber[count] >99 || carNumber[count] < 1)
{
cout << "That is not a correct number"
<< " The program will exit";
return 0;
}
cout << "car no is:" << carNumber[count] << "color: " << carColor[count];
++count;
// int lapCount{ 1 };
cout << endl;
}
The '\n' character after you press enter in cin >> carNumber[count]; probably still remains so after you execute the second pass of getline(cin, carColor[count]); you get an empty string. One solution is to do this:
char c;
cin >> carNumber[count];
cin >> c;
But better solution would be just to change:
getline(cin, carColor[count]);
to:
cin >> carColor[count];
I am having some issues with my simple code of creating a dvd & software list to import into a csv file.
I have the output working fine but for some reason my program is skipping my first part of the code. If I take out the IF statement, that bit of code works so I am not understanding why.
my output looks like this:
Would you like to add new media? Enter M for Movie or S for software: m
Enter the name of the Movie (20 Chararters or less)Name: Enter a rating for
your movie 1-5:
I am not getting any errors in my compiler (Visual Studio 2013) and it does not allow me to input a name and skips right to rating.
Any explanations or suggestions would be appreciated as I want to fix this before I move on to adding more.
here is my code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
string typeM = "movie";
string typeS = "software";
char answer, mediainput;
int rating = 0;
string dir, type;
string moviename,softname;
do
{
cout << "Would you like to add new media? Enter M for Movie or S for software: ";
cin >> mediainput;
cout << endl;
if (mediainput == 'm' || mediainput == 'M')
{
cout << "Enter the name of the Movie (20 Chararters or less) \n Name: ";
getline(cin, moviename);
cout << endl;
cout << "Enter a rating for your movie " << moviename << " 1-5 ";
cin >> rating;
if (rating < 1 || rating > 5)
{
cout << "You must enter a number from 1 to 5. Enter a number rating: ";
cin >> rating;
cout << endl;
}
ofstream outdata("DVD_Software_inventory.csv", ios_base::app);
outdata << moviename << "," << typeM << "," << rating << "," << endl;
outdata.close();
}
if (mediainput == 's' || mediainput == 'S')
{
cout << "Enter the name of the software (20 Chararters or less) \n Software name: " << endl;
getline(cin, softname);
cout << "Enter the directory it is in \n Directory: ";
cin >> dir;
ofstream outdata("DVD_Software_inventory.csv", ios_base::app);
outdata << softname << "," << typeS << ",," << dir << "," << endl;
outdata.close();
}
cout << "\n\nWould you like to add more? Y/N ";
cin >> answer;
cout << endl;
if (answer == 'N' || answer == 'n')
{
cout << "** End of Program **" << endl;
break;
}
} while (answer == 'Y' || answer == 'y');
system("pause");
return(0);
}
The problem is that while your cin >> statement ignores "\n" (the newline character), the character is still in cin's buffer. getline(), however,
does not ignore the "\n" character.
The solution, therefore, is to explicitly tell cin to ignore the "\n" character:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, moviename);
(credit to http://www.cplusplus.com/forum/beginner/24470/)
#include <iostream>
#include <limits>
#include <math.h>
using namespace std;
int main()
{
float startTemperature;
float endTemperature;
float loopTemperature;
float stepSize;
float i;
float numberOne;
cout << "Please enter a start temperature: " << endl;;
cin >> startTemperature;
while(!(cin >> startTemperature)){
cin.clear();
cout << "Invalid input. Try again: ";
}
cout << "Please enter an end temperature: ";
cin >> endTemperature;
while(!(cin >> endTemperature)) {
cin.clear();
cin.ignore(256, '\n');
cout << "Invalid temperature. Please try again: ";
}
cout << "Please enter a step size: ";
cin >> stepSize;
while(!(cin >> stepSize)) {
cin.clear();
cin.ignore(256, '\n');
}
for(i = startTemperature; i < endTemperature; i += stepSize) {
if(i == startTemperature) {
cout << "Celsius" << endl;
cout << "-------" << endl;
cout << startTemperature << endl;
loopTemperature = startTemperature + stepSize;
}
loopTemperature += stepSize;
if(loopTemperature > 20) {
break;
}
cout << loopTemperature << endl;
}
}
Hi, The problem with this code is that I have to input the value of the temperature twice. I have looked at other answers and I think it is something to do with the cin buffer but I don't know exactly what is wrong.
In the line
cin >> startTemperature; // <---problem here
while(!(cin >> startTemperature)){
cin.clear();
cout << "Invalid input. Try again: ";
}
You are taking input once, then again in the loop. That's is why you had to give input twice.
Just remove first input line, same for endTemparature and stepSize.
You're asking for input before the while loop, then again in the loop condition statement. Change the condition in your while statement to
while(!cin){
//error handling you already have
cin>>startTemperature; //endTemperature respectively
}
It's not only for the temperature but rather for every input. Change your code to the one below:
cout << "Please enter a start temperature: " << endl;;
while (!(cin >> startTemperature)){
cin.clear();
cin.ignore(std::numeric_limits<int>::max(), '\n');
cout << "Invalid input. Try again: ";
}
cout << "Please enter an end temperature: ";
while (!(cin >> endTemperature)) {
cin.clear();
cin.ignore(std::numeric_limits<int>::max(), '\n');
cout << "Invalid temperature. Please try again: ";
}
cout << "Please enter a step size: ";
while (!(cin >> stepSize)) {
cin.clear();
cin.ignore(std::numeric_limits<int>::max(), '\n');
cout << "Invalid step size. Please try again: ";
}
Reason:
You had redundant cin calls. Also use std::cin.ignore(std::numeric_limits<int>::max(), '\n'); instead of arbitrary number 256.