This question already has answers here:
Why does std::getline() skip input after a formatted extraction?
(5 answers)
Closed 1 year ago.
Hi I am solving a question of the book C++ Primer by Stanely. The questions is as follows :-
Write a program to read two strings and report whether the
strings are equal. If not, report which of the two is larger. Now, change
the program to report whether the strings have the same length, and if
not, report which is longer.
I have used a variable choice to switch between the program i.e whether to check strings are equal or not. Or to check whether the strings have same length or not.
#include<iostream>
using namespace std;
int main(){
char choice;
cout<<"Please enter choice"<<endl<<"For Larger press (L) and for longer press (l) "<<endl;
cin>>choice;
string s1, s2 ;
getline(cin,s1);
getline(cin,s2);
if(choice=='L'){
if(s1!=s2){
if(s1>s2) {
cout << "string which is larger is : " <<s1<<endl;
}
else{
cout<<"string which is larger is : " <<s2<<endl;
}
}
else{
cout<<"Both strings are equal "<<endl ;
}
}
else if (choice == 'l'){
if(s1.size() != s2.size()){
if(s2.size()> s1.size()){
cout<<"Longer string : "<<s2<<endl;
}
else {
cout<<"Longer string : " << s1<<endl;
}
}
else {
cout<<"Both strings have same length" <<endl;
}
}
else{
cerr<<"wrong input!! "<<endl;
return -1;
}
return 0;
}
but when I'm compiling the program, It is only taking input of string s1 and not taking input of string s2.
The output is as follows :-
Apparently using cin>> leaves out the '\n' from the input that is absorbed by the first getline() that, as #Scheff'sCat said, reads everything until '\n'. This means that the first getline() exits right away and shows only the second one.
You can try to use cin.ignore('\n').
Related
I am trying to make a magic 8 ball that provides a random preset answer to any input except "bye". Each time the void function magic8ball() is called, it generates a random number from 0 - 19 and prints a corresponding response to the console.
int main()
{
string question;
cin >> question;
while (question != "bye") {
magic8ball();
cout << "~Ask another question.\n";
cin >> question;
}
return 0;
}
For some reason, if the input for question has more than one word / has whitespace between characters, the loop repeats for each word before asking for input again. I stuck a "cout << question << endl;" in there and it showed each word in the string (as well as a call of magic8ball and "ask another").
e.g
>hi frend
... Ask again later
hi
~Ask another question.
... Don't count on it
frend
~Ask another question.
How do I prevent the while loop from treating the string as a series of words? Why is cin not triggering along with magic8ball() and cout ?
std::cin stops reading when it sees whitespace. Space also counts as a whitespace.
If you want your string to have space, use std::getline()
int main()
{
string question;
std::getline(std::cin, question);
while (question != "bye") {
magic8ball();
cout << "~Ask another question.\n";
std::getline(std::cin, question);
}
return 0;
}
This question already has answers here:
Integer validation for input
(3 answers)
Closed 6 years ago.
For example I have the code:
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "Please input a number:";
while(!(cin >> test))
{
cout << "Invalid input";
}
if(test == 1)
{
cout << "Test is 1";
}
else
{
// Do something
}
return 0;
}
If I input 1abc to the test variable it still continues to process the if statement even though the input is wrong. How could I ignore all the input made and just accept pure numbers in it?
You can use getline, find_if_not, and isdigit to check if an entire line is a valid integer or not. This will loop until it reads an integer:
std::string number;
while (std::getline(std::cin, number) && number.end() !=
std::find_if_not(number.begin(), number.end(), &isdigit))
std::cout << "gitgud!";
getline will read the input up to the newline, and put it in the string. find_if_not and isdigit will find the first non-digit character. If there are none (meaning it is a valid integer), it will return the end iterator.
This question already has answers here:
Integer validation for input
(3 answers)
Closed 6 years ago.
For example I have the code:
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "Please input a number:";
while(!(cin >> test))
{
cout << "Invalid input";
}
if(test == 1)
{
cout << "Test is 1";
}
else
{
// Do something
}
return 0;
}
If I input 1abc to the test variable it still continues to process the if statement even though the input is wrong. How could I ignore all the input made and just accept pure numbers in it?
You can use getline, find_if_not, and isdigit to check if an entire line is a valid integer or not. This will loop until it reads an integer:
std::string number;
while (std::getline(std::cin, number) && number.end() !=
std::find_if_not(number.begin(), number.end(), &isdigit))
std::cout << "gitgud!";
getline will read the input up to the newline, and put it in the string. find_if_not and isdigit will find the first non-digit character. If there are none (meaning it is a valid integer), it will return the end iterator.
This question already has answers here:
Integer validation for input
(3 answers)
Closed 6 years ago.
For example I have the code:
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "Please input a number:";
while(!(cin >> test))
{
cout << "Invalid input";
}
if(test == 1)
{
cout << "Test is 1";
}
else
{
// Do something
}
return 0;
}
If I input 1abc to the test variable it still continues to process the if statement even though the input is wrong. How could I ignore all the input made and just accept pure numbers in it?
You can use getline, find_if_not, and isdigit to check if an entire line is a valid integer or not. This will loop until it reads an integer:
std::string number;
while (std::getline(std::cin, number) && number.end() !=
std::find_if_not(number.begin(), number.end(), &isdigit))
std::cout << "gitgud!";
getline will read the input up to the newline, and put it in the string. find_if_not and isdigit will find the first non-digit character. If there are none (meaning it is a valid integer), it will return the end iterator.
This question already has answers here:
Integer validation for input
(3 answers)
Closed 6 years ago.
For example I have the code:
#include <iostream>
using namespace std;
int main()
{
int test = 0;
cout << "Please input a number:";
while(!(cin >> test))
{
cout << "Invalid input";
}
if(test == 1)
{
cout << "Test is 1";
}
else
{
// Do something
}
return 0;
}
If I input 1abc to the test variable it still continues to process the if statement even though the input is wrong. How could I ignore all the input made and just accept pure numbers in it?
You can use getline, find_if_not, and isdigit to check if an entire line is a valid integer or not. This will loop until it reads an integer:
std::string number;
while (std::getline(std::cin, number) && number.end() !=
std::find_if_not(number.begin(), number.end(), &isdigit))
std::cout << "gitgud!";
getline will read the input up to the newline, and put it in the string. find_if_not and isdigit will find the first non-digit character. If there are none (meaning it is a valid integer), it will return the end iterator.