Cin >> reading input - c++

When I input "M12 9UB" to this code:
std::string postal_code;
cout << "Enter postal code: ";
cin >> postal_code;
cout << "Your postal code is: " << postal_code << endl;
The output is "Your postal code is: M12".
So how do I get cin to read the whole line?

Use std::getline to read a whole line from a stream:
std::getline(std::cin, postal_code);
Live example.

You may use the C++ getline function like so:
#include <iostream>
using namespace std;
int main()
{
std::string postal_code;
cout << "Enter postal code: ";
getline(cin,postal_code);
cout << "Your postal code is: " << postal_code << endl;
}

Related

getLine() does't seem to work after first input - C++

I'm currently new to C++ and learning the basic syntax.
I'm exploring how getLine() works and I'm trying to compare the standard input and a getline().
#include <iostream>
#include <string>
using namespace std;
int main(){
string name1;
string name2;
cout << "Type your name please: ";
cin >> name1;
cout << "Your name is: " << name1 << endl;
cout << endl;
cout << "Type your name again please: ";
cin.ignore();
getline(cin, name2);
cout << "Your name is: " << name2 << endl;
return 0;
}
Expected Output:
Type your name please: John Doe
Your name is: John
Type your name again please: John Doe
Your name is: John Doe
Current Output:
Type your name please: John Doe
Your name is: John
Type your name again please: Your name is: Doe
What may be the issue causing this? Any help would be greatly appreciated.
As you can see cin >> name1; reads only up to the first whitespace. What remains in the input buffer is Doe\n. (Notice the first character is a space).
Now cin.ignore(); will ignore 1 character (the white space in this case). The input buffer now contains Doe\n. See here for more details: https://en.cppreference.com/w/cpp/io/basic_istream/ignore
Next getline(cin, name2); will read all the data up to the next new line. That is Doe, which you get as second output.
I think you wanted to discard the complete input, instead of just one character. This should do it:
cin.ignore(std::numeric_limits<std::streamsize>::max());
You should clear the input stream and ignore not only one character but all in the stream:
#include <iostream>
#include <limits>
#include <string>
int main() {
std::string name1;
std::string name2;
std::cout << "Type your name please: ";
std::cin >> name1;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Your name is: " << name1 << std::endl;
std::cout << std::endl;
std::cout << "Type your name again please: ";
std::getline(std::cin, name2);
std::cout << "Your name is: " << name2 << std::endl;
return 0;
}

Using cin.ignore AFTER cin.get to ignore extra inputs

The user is prompted to "enter a middle initial". What happens if they enter a space, full name, or maybe a letter followed by a period '.' ?
How can we modify the program to handle this using cin.ignore?
This is the code I currently have:
I commented out the area I'm having trouble with.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string fname, lname;
char MI;
cout << "Please enter your first name: ";
cin >> fname;
cout << "Please enter your middle initial: ";
cin.ignore(1, '\n');
cin.get(MI);
cout << "Please enter your last name: ";
//cin.ignore('\n')
cin >> lname;
cout << "Your name is " << fname << " " << MI << " " << lname << endl;
return 0;
}
When I have this other cin.ignore in it still doesn't do anything and the last name reads the extra inputs. I've tried adding a number of characters to read and it still doesn't fix the problem. When I run it it just skips the input for last name. I also tried changing the last name input to getline but if still didn't do anything.
You can just use std::getline and std::istringstream:
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::string fname, lname;
std::string MI;
std::cout << "Please enter your first name: ";
std::getline(std::cin, fname);
std::istringstream iss(fname);
iss >> fname;
do
{
std::cout << "Please enter your middle initial: ";
std::getline(std::cin, MI);
} while (MI.size() != 1);
std::cout << "Please enter your last name: ";
std::cin >> lname;
std::cout << "Your name is " << fname << " " << MI << " " << lname << std::endl;
return 0;
}
Here for fname I have used std::getline to get user input and then I've used std::istringstream to get only one word of the input.
For MI I have made it a string and until and unless the user doesn't provide a single character, the program doesn't continue.
And the lname part is the same.
You should change:
cin.ignore(1, '\n');
cin.get(MI);
To simply:
cin >> MI;
Let operator>> ignore any white space, including line breaks, between the first name and the middle initial.
After reading MI, you can then use the following to ignore everything up to the next input:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
Try this:
#include <iostream>
#include <string>
#include <limits>
using namespace std;
int main ()
{
string fname, lname;
char MI;
cout << "Please enter your first name: ";
cin >> fname;
cout << "Please enter your middle initial: ";
cin >> MI;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Please enter your last name: ";
cin >> lname;
cout << "Your name is " << fname << " " << MI << " " << lname << endl;
return 0;
}

Identifier "friends_name" is undefined & the Identifier "input " is undefined? C++

I am trying to prompt the user to enter his or her first name.
I wrote "hello,first_name", where first_name is the name entered by the user.
Then I modified the code as follows: change the prompt to "enter the name of the person you want to write to" and change the output to "Hello, first_name,"following do you like where you are right now in life (y/n)?";
this Is right before I wrote an if statement, letting the user make the decisions on what code to execute, but the input in my a given "conditions" isn't working because its undefined despite me putting assigning them to 'y' and 'n'.
Long story short I don't understand entirely to how to define the first_name and the input, I'm trying to have the user input those values to be assigned to those variables.
The Error list
#include "stdafx.h"
#include "iostream"
#include"string"
using namespace std;
int main()
{
string first_name;
cout << " Please enter your first name (followed by 'enter'):\n";
cin >> first_name;
cout << "Hello," << first_name << " do you like where you are right now in life (y/n)?:\n";
cin >> first_name;
if (input =='y') {
cout << "what do you like about it over,the people?(y/n)";//?:\n"
}
else if (input == 'n') {
cout << " what do you hate about it,the expenses?(y/n)"//?:\n"
}
else cout << "invalid choice";
cout << " what was your friends name again ?" << endl;
string = friends_name;
cin >> friends_name;
}
return 0;
}
}
In line 12 of your program it looks like you've used the variable first_name instead of input. Replace "first_name" with "input".
Eg:
cin>>input
Also as mentioned in the comments, make sure you declare all the variables that you use
Eg:
string first_name;
string input;
string friends_name;
Edited working code looks like:
#include "stdafx.h"
#include "iostream"
#include"string"
using namespace std;
int main(){
string first_name;
string friends_name;
string input;
cout << " Please enter your first name (followed by 'enter'):\n";
cin >> first_name;
cout << "Hello," << first_name << " do you like where you are right now in life (y/n)?:\n";
cin >> input;
if (input =="y")
cout << "what do you like about it over,the people?(y/n)";//?:\n"
else if (input == "n")
cout << " what do you hate about it,the expenses?(y/n)";//?:\n"
else cout << "invalid choice";
cout << " what was your friends name again ?" << endl;
cin >> friends_name;
return 0;
}

String phrase ends at space?

I am wringing a simple code to learn more about string. When I ran my code it would not print my last name. Can someone explain why? I used string phrase to store it and it only appears to have stored my first name. Here is the code.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting;
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
cin >> phrase;
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
I used string phrase to store it and it only appears to have stored my first name.
That makes sense.
cin >> phrase;
will stop reading when it encounters a whitespace character in the input.
To read the full name you can use one of the following approaches.
Use two calls to cin >>.
std::string first_name;
std::string last_name;
cin >> first_name >> last_name;
Use getline to read the entire line. getline will read everything in a a line, including whitespace characters.
getline(cin, phrase);
When you call cin >> phrase;, it only reads the string up to the first non-space character. If you want to include spaces in your name, best goes with getline(cin,phrase);.
IMPORTANT: getline() will reads whatever it is in the stream buffer up to the first \n. It means that when you enter cin >> greeting;, if you hit ENTER, getline() will read everything before that \n that is not already read, which is NOTHING into your phrase variable, making it an empty string. An easy way out is to call getline() twice. E.g.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
cout << "Exercise 3B" << endl;
cout << "Kaitlin Stevers" << endl;
cout << "String arrays" << endl;
cout << endl;
cout << endl;
char greeting[26];
cout << "Please enter a greeting: " << endl;
cin >> greeting; //IMPORTANT: THIS ASSUME THAT GREETING IS A SINGLE WORD (NO SPACES)
cout << "The greeting you entered was: " << greeting << endl;
string phrase;
cout << "Enter your full name " << endl;
string rubbish_to_be_ignored;
getline(cin,rubbish_to_be_ignored); //this is going to read nothing
getline(cin, phrase); // read the actual name (first name and all)
cout << greeting << ", how are you today " << phrase << "?" << endl;
return 0;
}
Assuming you store that code in the file stackoverflow.cpp. Sample run:
Chip Chip#04:26:00:~ >>> g++ stackoverflow.cpp -o a.out
Chip Chip#04:26:33:~ >>> ./a.out
Exercise 3B
Kaitlin Stevers
String arrays
Please enter a greeting:
Hello
The greeting you entered was: Hello
Enter your full name
Kaitlin Stevers
Hello, how are you today Kaitlin Stevers?
Tested on ubuntu 14.04

Extract a value from an input of type string and assign to a variable

I have a very basic question. It's about extracting a value from a string input and then assigning this value to an int and then copying out this integer to screen.
Here is my code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
string mystr;
float price;
int quantity;
cout << "What is your name? ";
getline (cin,mystr);
cout << "Hello Mr. " << mystr << endl;
cout << "Enter price: ";
getline (cin,mystr);
stringstream(mystr) >> price;
cout << "Enter quantity: ";
getline (cin,mystr);
stringstream (mystr) >> quantity;
cout << "Total price: " << quantity*price << endl;
cout << "Thank you for purchasing our product!";
return 0;
}
So the question is: when asked to enter price. Can I type "Price is 16" for example and the program is supposed to extract the 16 from the input and assign it to price?
If you allow that type of input, you would have to strip off the Price is portion before you can then read the 16 portion. Easiest way to do that is to simply put the input into a stringstream and call its >> operator in a loop until you reach a number or the end of the stream, eg:
cout << "Enter price: ";
getline(cin, mystr);
stringstream ss(mystr);
do
{
if (ss >> price)
break;
}
while (!ss.eof());
if (!ss)
{
// no price provided, do something...
}