This question already has answers here:
Getline ignoring first character of input
(3 answers)
Closed 4 years ago.
I used the below code in C++ to read characters from the user into a string, including space characters:
cin.ignore();
string s;
getline(cin,s);
cout<<s<<endl;
After taking input, the output is not the same:
input: gee ks for gee ks
output: ee ks for gee ks
Why is this?
cin.ignore() is discarding the 1st character typed by the user, and then getline() reads the remaining characters until a line break is reached. That is why your output is missing the g character from gee.
Related
This question already has answers here:
How can I repeat a string a variable number of times in C++?
(10 answers)
Closed 2 years ago.
one is 2, and ans is "000000".
string ans = "000000";
ans += string("1", one);
cout<<ans<<endl;
The output is:
0000001�
But I want the output:
00000011
What am I doing wrong?
string("1", one) does not do what you think it does. It does not duplicate the "1" string one number of times. It instead copies the 1st one number of chars from "1", which in this case is the '1' character and the '\0' null-terminator that follows it, which is where the � is coming from in the output. That is not what you want.
Use string(one, '1') instead. That will duplicate the '1' character one number of times, like you want, eg:
ans = "000000";
ans += string(one, '1');
cout << ans << endl;
Just use c++ strings and use + operator to catenate strings.
This question already has answers here:
Change string by index
(3 answers)
Closed 7 years ago.
string l;
cin>>l[0];
cout<<l;
input:a
output:
According to me the code must print the value of l[0] but why is there no output?
You need to do
cin>>l;
In your current state of code, when you try to access l[0] you are trying to access a memory location which may or may not be there. cin >> l[0] doesn't changes the size of the string which remains 0.
So when you try to do cout << l you are effectively printing an empty string.
Another alternative is
string s;
s.resize(1);
You have an empty string. There are no characters in it, including 0th character, you trying to read into. You need to actually add characters in string:
std::string l;
l.push_back('\0'); //Or any other character
std::cin >> l[0];
This question already has answers here:
Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?
(5 answers)
Why is “while( !feof(file) )” always wrong?
(5 answers)
Closed 7 years ago.
I have the following C++ program:
ofstream output("scores.txt");
output<<"John"<<" "<<"T"<<" "<<"Smith"<<" "<<90<<endl;
output<<"Eric"<<" "<<"K"<<" "<<"Jones"<<" "<<103<<endl;
output.close();
ifstream input;
input.open("scores.txt");
string line;
while (!input.eof()) {
getline(input, line);
cout<<line<<endl;
}
input.close();
cout<<"Done";
The output is:
John T Smith 90
Eric K Jones 103
Done
Why is there a blank line between Eric K Jones 103 and Done?
Structure your loop like this:
while (getline(input, line)) {
cout<<line<<endl;
}
Your duplicate line is because the way your read loop is structured, once you read the last line the eof bit is not set yet because readline succeeded. Therefore, you iterate one more time, doing a readline that does set the eof bit, then you do cout<<line<<endl and that last endl is your extra blank line.
This question already has answers here:
How do you construct a std::string with an embedded null?
(11 answers)
Closed 8 years ago.
I'm trying to read a large buffer from a socket which uses \0 to delimit pieces of data and \n to delimit lines.
I thought getline() would be an easy way to get each line but it's behaving strangely.
I'm using \n as the delimiter in getline().
string line;
string test1 = "aaa,123\nbbb\nccc,456\n";
stringstream ss1(test1);
while(std::getline(ss1, line, '\n')) {
cout << line << endl;
}
// outputs:
// aaa,123
// bbb
// ccc,456
string test2 = "aaa\0123\0\nbbb\0\nccc\0456\0\n";
stringstream ss2(test2);
while(std::getline(ss2, line, '\n')) {
cout << line << endl;
}
// outputs:
// aaa
// 3
Why is this happening in test2? Where is the 3 coming from? Must I remove the \0 to make this work? Is there an easier/better way to mark strings in my buffer when I do a socket recv()?
\0 in a special symbol. It shows when the string ends.
For example, if you type in "a string", the compiler automatically adds a \0 on the end, which signifies the end of the string. However, it is legal to have a \0 in the middle of the string, it just means that everything after it is ignored.
So basically, any operation you do on the string, not just the getline, will treat the string as "aaa", ignoring everything after the first \0 that is found. But...
As #Fred Larson points out
Oh, I see where the 3 comes from. The first \0 isn't a null, it's the start of \012, which is a carriage return. Then the 3 follows.
So actually, the string is being treated as "aaa\n3". Which is why you get the output you do.
Edit: And thanks to Galik, I will also add that these rules I mention might only apply to a string literal / c-string. It may be a different case with std::strings, in which the length of the string is known ahead of time.
\0 is the standard string terminator symbol. As such, you may either read character by character or avoid \0 as delemeters
i am creating a program to read the .dxf file of autodesk.
i am encountering a problem while reading strings.
when i use :
string acad;
fstream f;
f.open(name);
f >> acad
if the string is "chamfer" it works perfect.
but if the string is "a & b" it is able to read only upto a.
since the file format follows a pattern i am using while loop.
example from a file:
9 //loop 1
$DWGCODEPAGE //loop 1
3 //looop 1
ANSI_1252 //looop 1
9 //loop 2
$LASTSAVEDBY //loop 2
1 //loop 2
sam & tom //loop 2
9 //loop 3
$INSBASE //loop 3
10 //loop 3
0.0 //loop 3
as you can see sometimes there may not be any space as in "ANSI_1252" & sometimes there may be spaces as in "sam & tom".
how can i generalise the code so that the whole string in a line is stored along with the spaces, if any.
please forget about the spaces in the beginning of each line, i am using ws for that.
thank you!
>> operator reads words delimited by space, when used with a string parameter. If you want to read lines of characters, you should use getline() instead.