This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Need help with getline()
In the following code, my getline is skipped entirely and doesn't prompt for input.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <istream>
using namespace std;
int main ()
{
int UserTicket[8];
int WinningNums[8];
char options;
string userName;
cout << "LITTLETON CITY LOTTO MODEL: " << endl;
cout << "---------------------------" << endl;
cout << "1) Play Lotto " << endl;
cout << "q) Quit Program " << endl;
cout << "Please make a selection: " << endl;
cin >> options;
switch (options)
{
case 'q':
return 0;
break;
case '1':
{
cout << "Please enter your name please: " << endl;
getline(cin, userName);
cout << userName;
}
cin.get();
return 0;
}
}
The problem is here:
cin >> options;
You can only extract (>>) from cin when the user hits enter. So the user types 1 Enter and that line executes. Since options is a char, it extracts a single character (1) from cin and stores it in options. The Enter is still in the stdin buffer, since nothing has consumed it yet. When you get to the getline call, the first thing it sees in the buffer is the Enter, which marks the end of input, so getline immediately returns an empty string.
There's lots of ways to fix it; probably the easiest way that fits with the model you're using in your program is to tell cin to ignore the next character in its buffer:
cin >> options;
cin.ignore();
Related
This question already has answers here:
cin and getline skipping input [duplicate]
(4 answers)
Closed 7 years ago.
Im doing exercices from c++ primer, and trying to do a program that recieves as input a word and a line. If when i ask for a word (with cin) I press enter, then the program just skips the next lines and doest ask for a line (with the getline)... and if i write an entire phrase in the cin (like "hello beautifull world") then the first word ("hello") is captured by the cin and the others two words ("beautifull world") by the getline.
i understand that in the cin, when i input a space, it cuts the input. what i dont uderstand are two things:
1.- why if i end the input (in the cin) with enter it skips all the rest of code? (is there a solution for that?)
2.- why if i write an entire phrase in the cin, it assign the other two word to the getline before to execute the
cout << "enter a line" << endl;
?
Thx! sorry for my english C:
#include <iostream>
#include <string>
using namespace std;
int main() {
string word, line;
cout << "enter a word" << endl;
cin >> word;
cout << "enter a line" << endl;
getline(cin, line);
cout << "your word is " << word << endl;
cout << "your line is " << line << endl;
return 0;
}
You need cin.ignore() between two inputs:because you need to flush the newline character out of the buffer in between.
#include <iostream>
using namespace std;
int main() {
string word, line;
cout << "enter a word" << endl;
cin >> word;
cout << "enter a line" << endl;
cin.ignore();
getline(cin,line);
cout << "your word is " << word << endl;
cout << "your line is " << line << endl;
return 0;
}
For your second answer, when you enter whole string in first cin, it takes only one word, and the rest is taken by getline and thus your program will execute without taking input from getline
Demo
This question already has answers here:
Why is the Console Closing after I've included cin.get()?
(4 answers)
Closed 8 years ago.
I'm fairly new to c++ and I was wondering why my cin.get() isn't stopping the program in cmd from instantly closing when it's done? I tried the cin.get() on my previous code and it worked, but for some reason it doesn't work on this code.
#include <iostream>
int main()
{
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots;
cout << "You have " << carrots << endl;
cin.get();
return 0;
}
Using cin.get(), you get only one of those characters, treated as char.
You would not able see the output as the command prompt will close as soon as the program finishes. Putting cin.get() forces the program to wait for the user to enter a key before it will close, and you can see now the output of your program.
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots;
cout << "You have " << carrots << endl;
cin.get();
cin.get();// add another one
return 0;
You have to add
cin.ignore();
Before than
cin.get();
To clean the input buffer from the previously return !
Complete code
#include <iostream>
int main()
{
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots;
cout << "You have " << carrots << endl;
cin.ignore();
cin.get();
return 0;
}
My code runs and works well the first time around, but I am having looping problems:
My code isn't counting characters that are in words
The second time around when you press "yes," it ends up printing everything out. I must have a loop in the wrong spot, but I can't find it for the life of me.
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
char character;
string sentence;
char answer;
int cCount;
while(1) {
cout << "Enter a character to count the number of times it is in a sentence: ";
cin >> character;
cout << "Enter a sentence and to search for a specified character: ";
cin >> sentence;
if(character == '\n' || sentence.empty())
{
cout << "Please enter a valid answer:\n";
break;
}
else {
cCount = count(sentence.begin(), sentence.end(), character);
cout << "Your sentence had" << " " << cCount << " " << character << " " << "character(s)" << '\n';
}
cout << "Do you wish to enter another sentence (y/n)?: \n";
cin >> answer;
if (answer == 'n'){
break;
}
}
return 0;
}
By just reading your code, it looks fine, except where you get the sentence. Using cin, it will only read until it sees a newline or a space, so if you're entering a sentence, it will read every word as a different input.
Try getline(cin, sentence) and see if that fixes the problem.
Edit: Forgot to add in: use cin.ignore() after the getline. cin reads up to, and including the line break (or space) while getline only reads up to the line break, so the line break is still in the buffer.
use
cin.ignore(); //dont forget to use cin.ignore() as it will clear all previous cin
getline(cin, sentence, '\n'); //take the sentence upto \n i.e entered is pressed
You don't have the loops wrong. What's wrong is that you are assuming that
cin >> sentence;
does something different from what it really does.
If you want to read a line of text, then do this
getline(cin, sentnence);
Your code reads a single word only.
use cin it will end with a newline or a space
eg:
when you input hello world it will get hello
and you can try
getline
it wiil end with a newline
This is working, try this.
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
char character;
string sentence;
char answer;
int cCount;
while(1) {
cout << "Enter a character to count the number of times it is in a sentence: ";
cin >> character;
cout << "Enter a sentence and to search for a specified character: ";
fflush(stdin);
getline(cin, sentence, '\n');
if(character == '\n' || sentence.empty())
{
cout << "Please enter a valid answer:\n";
break;
}
else {
cCount = count(sentence.begin(), sentence.end(), character);
cout << "Your sentence had" << " " << cCount << " " << character << " " << "character(s)" << '\n';
}
cout << "Do you wish to enter another sentence (y/n)?: \n";
cin >> answer;
if (answer == 'n'){
break;
}
}
return 0;
}
after you enter first input and enter that enter is considered as input in sentence
So, you need to flush that and after that you can scan that sentence.
Try:
cCount = count(sentence.c_str(), sentence.c_str()+sentence.length(), 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 ()
I have been working on this program for a while and I finally got rid of the compile errors. But when I tried it, the program basically skipped a line of code.
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main(){
string nameOfFile = "";
char index;
char title[100];
char name[100];
char copyright[100];
cout << "Welcome, and hello to the html templating software" << endl;
cout << "Is this your index page?\ny/n" << endl;
cin >> index;
if (index=='n'){
cout << "Enter the prefered name of this file" << endl;
getline(cin, nameOfFile, '\n');
}
cout << "What will the title of the page be?" << endl;
cin.getline(title, 100);
cout << "What is your name?" << endl;
cin.getline(name, 100);
cout << "What is the copyright?" << endl;
cin.getline(copyright, 100);
cin.get();
return 0;
}
You see how after asking if this is your index page it skips the next cin.getline function no matter the scenario.
When the user entered the index, they also typed a newline, but your cin didn't remove it from the input stream. So, your call to cin.getline returns immediately because of the leftover newline.
Add a call to cin.ignore before the cin.getline to flush it out.
replace getline(cin, nameOfFile, '\n')
with
cin >> nameOfFile