I'm trying to use .empty() so pressing enter in the following code will not execute the if statement and the do-while loop will break. When I try this code, hitting enter does nothing: it just continues indenting until I enter more data. I looked up .empty() and I think I am using it correctly. Why isn't this code working?
void Student::read()
{
string g = " ";
cout << "Enter the students name: ";
getline(cin, new_name);
cout << endl;
do
{
cout << "Please enter a letter grade <E to quit>: ";
cin >> g;
if(!g.empty())
{
addGrade(g);
}
}while(!g.empty());
}
The problem is nothing to do with string.empty(), it is this line:
cin >> g;
That operation is whitespace delimited. That is to say, it skips all leading whitespace, and then once it has started consuming non-whitespace characters, it stops on the next whitespace if finds. So you can just press enter all day long, and it will be ignored, because pressing enter causes a newline character ('\n'), which is whitespace.
If you want line oriented input, use getline instead of operator>>.
getline(cin, g);
std:;string:empty() is working as expected in this. As Benjamin pointed out instead if you use getline(cin, g); in place of cin>>g you can achieve what you are expected:
int main()
{
string g = " ";
std::string new_name;
cout << "Enter the students name: ";
getline(cin, new_name);
cout << endl;
do
{
cout << "Please enter a letter grade <E to quit>: ";
getline(cin, g);
if(!g.empty())
{
std::cout<<"here";
//addGrade(g);
}
}while(!g.empty());
return 0;
}
Related
I have a switch in which one case asks the user for several inputs to use for constructing a class object. One of these inputs should be in the form of a number. If a number is not entered it breaks the switch and ends up terminating the program. I want to set up a while(){} condition so that if a non integer is entered it will prompt the user to enter an integer and then continue on with the program.
int main(){
int in_yob, ranking;
string in_first_name, in_last_name, in_genre, in_fact, last_name, in_composer;
char selection, choice;
do {
DisplayMenu();
cin >> selection;
cout << endl;
while (!cin || selection < 48 || selection > 53){
cin.clear();
cout << "Please make a valid selection" << endl;
DisplayMenu();
cin >> selection;
}
switch (selection) {
case 49 : {
cout << "First Name: ";
cin >> in_first_name;
cout << "Last Name: ";
cin >> in_last_name;
cout << "Genre: ";
cin >> in_genre;
cout << "Year of Birth: ";
cin >> in_yob;
cout << "Fact: ";
cin >> in_fact;
last_name = in_last_name;
transform(last_name.begin(), last_name.end(), last_name.begin(), ::tolower);
Composer& last_name = myDB.AddComposer(in_first_name, in_last_name,
in_genre, in_yob, in_fact);
cin.clear();
} break;
...
default:
cout << "Please make a valid selection" << endl;
}
} while (selection != 48);
}
I have tried inserting a while loop after cin >> in_yob; as:
while(!cin || in_yob > 1){
cin.clear();
cout << "Enter a positive enter for year of birth: ";
cin >> in_yob;
}
but the result is an infinite loop of "Enter a positive enter for year of birth: ". I know this construct for error checking works outside of a switch case so what is the reason that within a switch case i'm getting this result? Also how would you go about fixing this so that I can check for and prevent an input error? Thanks.
[To explain my self with more space and better formatting better than in a comment I post this as an answer instead as of a comment.]
When you enter input, like for example
1
the input buffer actually contains two characters, firs the digit '1' and then the newline '\n'.
When you read a single character, the input operation extracts the first character, the digit, and writes it to your variable.
If you then read another character, it will read the newline, and not whatever comes after.
There is a trick to "ignore" characters until, for example, a newline, and that is done by using the std::istream::ignore, and if you follow the link to the reference you will see a very good example on how to ignore anything up to and including the newline.
So in your case it's not enough to just call clear you need to call ignore as well in your input validation loop. And if you continue to read single characters, you need to call ignore before that as well.
You needed to clear the input stream.
#include <iostream>
#include <string>
int in_yob = 0;
int main()
{
while(std::cin.good() && in_yob < 1){
std::cin.clear();
std::cout << "Enter a positive enter for year of birth: ";
if( !(std::cin >> in_yob) ) {
std::cin.clear();
std::cin.ignore(10000,'\n');
}
}
std::cout << "YOB " << in_yob << std::endl;
}
So I am trying to write a function to check whether a word is in a sentence, by looping through a char array and checking for the same string of char's. The program works as long as the Sentence doesn't have any spaces. I googled around and they are all the same suggestions;
cin.getline
But however I implement it, it either doesn't run or skips the entire input and goes straight towards the output.
How can I account for spaces?
#include <iostream>
using namespace std;
bool isPartOf(char *, char *);
int main()
{
char* Word= new char[40];
char* Sentence= new char[200];
cout << "Please enter a word: ";
cin >> Word;
cout << endl << "Please enter a sentence: ";
//After Word is input, the below input is skipped and a final output is given.
cin.getline(Sentence, 190);
cout << endl;
if (isPartOf(Word, Sentence)==true)
{
cout << endl << "It is part of it.";
}
else
{
cout << endl << "It is not part of it.";
}
}
bool isPartOf(char* a, char* b) //This is the function that does the comparison.
{
int i,j,k;
for(i = 0; b[i] != '\0'; i++)
{
j = 0;
if (a[j] == b[i])
{
k = i;
while (a[j] == b[k])
{
j++;
k++;
return 1;
if (a[j]=='\0')
{
break;
}
}
}
}
return 0;
}
And I am not allowed to use strstr for the comparison.
Ok, I'll try to explain your problem:
Let's assume this is your input:
thisisaword
this is a sentence
When you use cin and give it any input, it stops at the newline character which in my example follows character 'd' in 'thisisaword'.
Now, your getline function will read every character until it stops newline character.
Problem is, the first character getline encounters is already a newline so it stops immediately.
How is this happening?
I'll try to explain it like this:
If this is your input given to a program (note \n characters, treat it like a single character):
thisisaword\n
this is a sentence\n
What your cin function will take and leave:
\n
this is a sentence\n
Now getline sees this input and is instructed to get every character until it meets a newline character which is "\n"
\n <- Uh oh, thats the first character it encounters!
this is a sentence\n
cin reads input and leaves "\n", where getline includes "\n".
To overcome this:
\n <- we need to get rid of this so getline can work
this is a sentence\n
As said, we cannot use cin again because it will do nothing.
We can either use cin.ignore() without any parameters and let it delete first character from input or use 2x getline(first will take remaining \n, second will take the sentence with \n)
You can also avoid this kind of problem switching your cin >> Word; to a getline function.
Since this is tagged as C++ I changed Char*[] to Strings for this example:
string Word, Sentence;
cout << "Please enter a word: "; cin >> Word;
cout << endl << Word;
cin.ignore();
cout << "\nPlease enter a sentence: "; getline(cin, Sentence);
cout << endl << Sentence;
OR
string Word, Sentence;
cout << "Please enter a word: "; getline(cin, Word);
cout << endl << Word;
cout << "\nPlease enter a sentence: "; getline(cin, Sentence);
cout << endl << Sentence;
How about using this:
std::cin >> std::noskipws >> a >> b >> c;
cin by default utilizes something like this:
std::cin >> std::skipws >> a >> b >> c;
And you can combine flags:
std::cin >> std::skipws >> a >> std::noskipws >> b;
Tell me if it works for you : )
By default operator>> skips whitespaces. You can modify that behavior.
is.unsetf(ios_base::skipws)
will cause is's >> operator to treat whitespace characters as ordinary characters.
Case 3 is an option to add a book to the structure. As long as books with titles without spaces are added, they are ok, whenever I try to put a name that has a space in it, the compiler goes crazy, sorta like what it would do if you execute an infinite loop. Why and what is the solution?
struct bookStruct
{
string bookTitle;
int bookPageN;
int bookReview;
float bookPrice;
};
const int MAX_BOOKS=10;
case 3:
{
for(int i=0;i<MAX_BOOKS;i++)
{
if(books[i].bookTitle=="\0")
{
cout << "\nPlease Enter the Title: ";
cin >> books[i].bookTitle ;
cout << "\nPlease Enter Total Number of Pages: ";
cin >> books[i].bookPageN ;
cout << "\nPlease Enter Rating (stars): ";
cin >> books[i].bookReview ;
cout << "\nPlease Enter Price: ";
cin >> books[i].bookPrice;
cout << "\n\nBook Added.\n\n";
break;
}
}break;
}
The input operator >> stops at space when reading strings.
What you want to use is std::getline.
cout << "\nPlease Enter the Title: ";
std::getline(std::cin, books[i].bookTitle);
The input operator >> when reading a number will also stop at a space or newline (leaving them on the input stream). Thus when you wrap around to the next book there is still a '\n' character on the input stream. So for numbers you also need to use std::getline(). But in this case you need to convert the value to an integer.
cout << "\nPlease Enter Total Number of Pages: ";
std::string line;
std::getline(std::cin, line);
std::stringstream linestream(line);
linestream >> books[i].bookPageN ;
Its been a while since i have coded c++ and i have forgot an annoying thing that happens when you gather string input. Basically if this loops back through, say if you use negative numbers then it skips the cin from the employee name line the second go round. I remember having this issue before and having to clear or do something of that sort before or after the string is input. Please help!
PS Also for extra help can anyone help me with a correct loop below. How can i check for a value in the string input to make sure they input a value?
#include <string>
#include <iostream>
#include "employee.h"
using namespace std;
int main(){
string name;
int number;
int hiredate;
do{
cout << "Please enter employee name: ";
getline(cin, name);
cout << "Please enter employee number: ";
cin >> number;
cout << "Please enter hire date: ";
cin >> hiredate;
}while( number <= 0 && hiredate <= 0 && name != "");
cout << name << "\n";
cout << number << "\n";
cout << hiredate << "\n";
system("pause");
return 0;
}
You want to change your loop condition to be whether or not any of the below are not set. The logical AND will only trigger if all three are unset.
do {
...
} while( number <= 0 || hiredate <= 0 || name == "");
Next, use cin.ignore() as prescribed by #vidit to get rid of issues with reading in newline characters.
Lastly, and importantly, your program will run an infinite loop if one enters an alphabetic character for an integer instead of...an integer. To mitigate that, use isdigit(ch) from the <cctype> library.
cout << "Please enter employee number: ";
cin >> number;
if(!isdigit(number)) {
break; // Or handle this issue another way. This gets out of the loop entirely.
}
cin.ignore();
cin leaves a newline character(\n) in the stream, which causes the next cin to consume it. There are many ways of getting around that. This is one way.. using ignore()
cout << "Please enter employee name: ";
getline(cin, name);
cout << "Please enter employee number: ";
cin >> number;
cin.ignore(); //Ignores a newline character
cout << "Please enter hire date: ";
cin >> hiredate;
cin.ignore() //Ignores a newline character
#include <iostream>
#include <string>
struct Car{
std::string model;
unsigned int year;
};
int main(){
using namespace std;
int carNum;
cout << "How many cars do you wish you catalog? ";
cin >> carNum;
Car * cars = new Car[carNum];
for (int i=0;i<carNum;i++){
cout << "Car #" << i << endl;
cout << "Please enter the make: ";
getline(cin, cars[i].model);
cout << "Please enter the year made: ";
cars[i].year = cin.get();
}
cout << "Here's your collection" << endl;
for (int i=0;i<carNum;i++){
cout << cars[i].model << " " << cars[i].year << endl;
}
delete [] cars;
return 0;
}
When i execute the program, the getline(cin, car[i].model) just get skipped over. Why is this?
like this:
Car #2
Please enter the make: Please enter the year made:
Simple reason.
When you do cin >> whatever, a \n is left behind (it was added when you pressed Enter). By default, getline reads until the next \n, so the next read will simply read an empty string.
The solution is to discard that \n. You can do it by putting this:
cin.ignore(numeric_limits<streamsize>::max(),'\n');
Just after the cin >> carNum.
Don't forget to include limits in order to use numeric_limits.
After every cin call using insertion operator. You must call cin.ignore() if you want cin.getline () to work.
As cin>> leaves behind a '\n' character when you press enter and because of that when you use getline () it picks up the \n
and takes no input as it finds \n in the input stream which is the default delimiter.
So you can either do cin.ignore () after every cin>> or simply set a delimiter character cin.getline ()