about the code below, string doesn't light up anymore and when I entered "John Smith", only "John" appears, string was working fine for me weeks ago until i tried calling strings function today which didn't work so i tested for a simpler one.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string name;
// Get the user's name
cout << "Please enter your first name: ";
cin >> name;
// Print the greeting
cout << "Hello, " << name << "." << endl;
return 0;
}
string doesn't light up like int
I might be asking at the wrong place but I cant' tell what's the problem, please help :(
To get all the line, use getline(cin, name);
instead of cin >> name;
See http://www.cplusplus.com/reference/string/string/getline/
With std::string's, using std::cin >> someString will only read the first word off the buffer (it will stop at the first whitespace encountered).
Use getline(std::cin, someString) instead to read the entire line.
std::cin gets only characters to first 'white' character, like space, tab or enter.
If you want to read whole line use e.g. getline()
string line;
cin.clear(); //to make sure we have no pending characters in input buffer
getline(cin, line);
Related
I want to get input twice with one variable a string and the other character, but it always comes out compilation error, how do I solve this problem. I've tried cin.clear(); but it seems not working in this case.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
using std::getline;
int main(){
string name;
cout << "Please input a string." << endl;
getline (cin, name);
cout << "Hello, there, "<< name <<".\n";
char ccc;
cout << "Please input a character." << endl;
getline (cin, ccc);
cout << "This is a alphabet:" << ccc << endl;
return 0;
}
I expect the output will be like:
Please input a string.
John
Hello, there, John.
Please input a character.
c
This is a alphabet:c
I have used cin.ignore(); before. It should clear the buffer. Also char should use getchar instead of getline.
There is no version of getline() which accepts a char as its second argument. Instead, you probably want the version which accepts a char*. You will need to modify your code to read a string that contains one character.
Try getchar() instead of getline() on the second one.
I've been having issues in a program involving cin.
My problem is that the first word of everything I input appears to be skipped, possibly because of the way the buffer is handled. I have seen similar posts regarding this but trying to apply their fixes to my code have so far failed. What is supposed to happen is the user inputs a name and that name gets stored in a text file with other entered data. However, it always drops the first word.
#include "string"
#include "stdafx.h"
string _name;
int main()
{
cout << "Choose a name" << endl;
getline(cin, _name);
cout << _name;
ofstream dat;
dat.open("data.txt");
dat << _name;
dat.close();
return 0;
}
This code is where the problem appears to be. I just can't get it to take the first word.
cin >> _name;
This reads the first word on the first line of input into _name.
getline(cin, _name);
This will read the rest of the line into _name. This overwrites the existing contents of name.
Because this overwrites the existing contents of _name, which contains the first word read, this ends up reading all except the first word of the line, as you described.
If you just want to read the entire line into _name, the only thing that needs to be done is to remove the cin >> _name.
If you want to read a name from cin, then your code should look something like this:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string _name;
cout << "Choose a name : ";
getline(cin, _name);
cout << _name << endl;
// Do something with _name - write to file etc..
// ..
}
I can get the file to save right but I can't seem to get multiple words to write to .txt, like if I type "Hi purple" it just writes "Hi", here is code
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
system("color F0");
string name0;
cout << "Please enter a file name, no spaces/special characters" << endl;
cin >> name0;
name0 = name0+".txt";
system("cls");
cout << " FISHSOFT" << endl;
cout << "The best text editor in the world" << endl << endl;
string text;
cin >> text;
ofstream myfile;
myfile.open (name0.c_str() , ios::out | ios::trunc);
myfile << text;
myfile.close();
system("PAUSE");
return 0;
}
Use std::getline to read an entire line of input including spaces.
cin >> text; will read one whitespace delimited token from the input stream. One word in == one word out.
std::getline(cin, text); will read a whole line. Reading more than that is tricky, but typically a loop around multiple calls to getline.
Suggestion: Save yourself time and fire up your IDE's debugger to see what's happening in your code before posting a question. Almost always faster, and if not, you can make much better, tighter-focused questions.
std::cin is able to get several parameters at once.
That mean you may write:
std::cin >> name0 >> name1 >> name2;
// input: a1 a2 a3
// make: name0: a1, name1: a2, name3:a3
By default, the space is the separator between parameters.
To avoid this behavior, you could use getLine:
std::getline(std::cin, name0);
There are 2 ways to get the string with spaces and special character.
cin.getline(name);
gets(name);
Hope this will serve your purpose.
I don't think this question is not a duplicate of this. Even though it is caused by the same thing, it is a different manifestation of the problem.
The terminal output when I run this program looks like this (my input is 1 and Rock Lee followed by enter):
Enter a number: 1
Enter your name: Rock Lee
Name is blank.
However, the name shouldn't be blank. Why is the name variable ""? How do I fix this bug? Here is the code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main () {
int num;
string name;
cout << "Enter a number: ";
cin >> num;
cout << "Enter your name: ";
getline(cin, name, '\n');
cin.ignore (1000, '\n');
if (name == "")
{
cout << "Name is blank.";
return -1;
}
return 0;
}
Also, please note that I want to use Getline() in the solution, and I want to be able to read an entire line (so the name can be anything, not just first and last name).
When I tried commenting out cin.ignore(1000, '\n');, it gave this output which is also incorrect:
Enter a number: 1
Enter your name: Name is blank.
It doesn't even give me a chance to type in my name. How do I fix this?
cin >> num reads until it sees whitespace, but it doesn't discard the whitespace, so it leaves '\n' in the input. Your call to getline sees it and immediately returns, filling name with an empty string. You need to call cin.ignore before getline to ignore the '\n' that cin::operator>> left laying around.
I have following Simple program to print string in C++, But this program only reads characters before space, not reading full string.
#include<iostream>
using namespace std;
int main()
{
char str[90];
cout << "Enter a string:";
cin >> str;
cout << str;
system("pause");
}
This is by design: cin "breaks" lines on whitespace characters, such as spaces and tabs.
Moreover, you are limiting the input to 90 characters, which is not good either: typing more than 90 characters with no spaces in between would overflow the buffer.
Here is a way to fix it:
std::string str;
std::cout << "Enter a string: ";
std::getline(std::cin, str);
Unlike character arrays, std::string objects can grow dynamically, so they would accommodate any number of characters the user chooses to enter.
You need to add two headers in order for this to compile:
#include <string>
#include <iostream>
>> reads a single word. You want getline to read a whole line:
cin.getline(str, sizeof str);
Now the problem is that the line will be truncated if it's too long. To fix that, use a string rather than a fixed-size buffer:
string str;
getline(cin, str);