This question already has answers here:
std::cin.getline( ) vs. std::cin
(5 answers)
Closed 8 years ago.
#include<iostream.h>
#include<conio.h>
class String
{
char str[100];
public:
void input()
{
cout<<"Enter string :";
cin>>str;
}
void display()
{
cout<<str;
}
};
int main()
{
String s;
s.input();
s.display();
return 0;
}
I am working in Turbo C++ 4.5. The code is running fine but its not giving the desired output
for e.g if i give input as "steve hawking" only "steve" is being displayed. Can anyone please help?
Using >> on a stream reads one word at a time. To read a whole line into a char array:
cin.getline(str, sizeof str);
Of course, once you've learnt how to implement a string, you should use std::string and read it as
getline(cin, str);
It would also be a very good idea to get a compiler from this century; yours is over 15 years old, and C++ has changed significantly since then. Visual Studio Express is a good choice if you want a free compiler for Windows; other compilers are available.
cin>>str;
This only reads in the next token. In C++ iostreams, tokens are separated by whitespace, so you get the first word.
You probably want getline, which reads an entire line into a string:
getline(cin, str);
You can use :
cin.read( str, sizeof(str) );
But, this will fill up the buffer. Instead you should use cin.getLine() as MikeSeymour suggested
You could use cin.getline to read the whole line.
use this
cin.getline(cin, str);
Related
This question already has answers here:
C++: Why does space always terminate a string when read?
(4 answers)
Closed 2 years ago.
I wrote a code so that it removes everything(like spaces and other things) other than the alphabats using isalpha() function and converts it to lower case using tolower() function. It is working fine if i don't put a space in the string but if there is any space in the string then it go beyond the space. I dont understand why this is happening. This is the code i wrote.
#include<bits/stdc++.h>
#include<cstring>
#include<cctype>
using namespace std;
int main()
{
int i;
string A,b="";
cin>>A;
for(i=0;i<A.size();i++)
{
if(isalpha(A[i]))
b+= tolower(A[i]);
else
continue;
}
cout<<b;
}
Please help me.
Thankyou
The cin >> A; considers the space to terminate the input.
To get the whole line, use getline(cin, A);
cin reads the string till the first space it encounters, if your input string is "Hello World", then cin will only read "Hello".
You can use getline function to read a complete line.
This question already has answers here:
Using getline(cin, s) after cin [duplicate]
(13 answers)
Closed 7 years ago.
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
char text[200];
int input;
cin>>input;
if (input == 1)
{
cin.getline(text, 200);
cout<<text<<"\n";
}
else if(input == 0)
{
cout <<"You entered a 0";
}
return 0;
}
I am trying to make a small program where the user gives an input either a 1 or 0. if the user enters a 1 then he can enter a whole sentence and stores it in the char array of text. My problem is that when I put the cin.getline() inside an if statement it no longer works. why is that?
Thanks
It is not that cin.getline() doesn't work. It does exactly what has been asked of it: Read the line of text up to the next newline. It just so happens that cin >> input; has read some digits and then left the first non-digit input in the input buffer - which typically is a newline unless you typed something that wasn't a number.
You can work around this by calling cin.ignore(), which will "read everything up to the next newline and throw it away".
Ideally, you should decide whether you want to use cin >> or cin.getline(), and use one or the other, but that means then reading a string of text and in your code converting to a digit, and if you are a novice, that's probably a bit more complex than you actually want to make it.
This question already has answers here:
cin and getline skipping input [duplicate]
(4 answers)
Closed 8 years ago.
i have a problem with input char to my program
#include <iostream>
using namespace std;
int main()
{
int choise;
char word[81];
cin >> choise;
cout << "enter the word:" << endl;
cin.getline(word, 81);
return 0;
}
the visual studio open the input to "choise"
but skip on cin.getline (it the same if i replace it with gets_s).
i tried to write
cin.get(); before the "getline"...
but then the program not get's the first char
(if i put 'aa' it get 'a')
what can i do?
thanks
Its because you entered a newline for the program to accept the integer you entered for choice, the that newline is not extracted from the buffer, leaving it to be read in your next input operation. The getline call reads that left-over newline, and is happy with that.
There are a couple of ways to solve your problem. The first and most obvious is to use std::string for the word variable, and then use the normal input operator >> as that will skip leading whitespace (which includes newline).
Another solution is to tell the input stream to ignore until and including a newline. The linked reference has an example on how to do exactly that.
After entering the integer you press enter, and this enter input is left in the buffer area and used as the next input for getline and the computer assumes you're done.
add this line before getline statement and re- compile it.
cin.ignore().
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is this a bug with getline(), or am I doing something wrong. Right way to use getline()?
I tried to learn this topic on STL lists and Strings. So as an integration, i tried out this program:
#include<iostream>
#include<list>
#include<string>
using namespace std;
int main(){
list<string> obj1, obj2;
string obj;
int n;
cout<<"Enter the number of elements in string list 1:\t";
cin>>n;
cin.clear();
cout<<"Enter the string:\n";
for( int i=0; i<n; i++){
getline(cin, obj);
cout<<"The string is:\t"<<obj<<" and i is "<<i<<endl;
obj1.push_back(obj);
}
obj1.sort();
cout<<"The string in sorted order is:\n";
list<string>::reverse_iterator rit;
for( rit = obj1.rbegin(); rit != obj1.rend(); rit++)
cout<<*rit<<endl;
return 0;
}
I get the following output:
Enter the number of elements in string list 1: 4
Enter the string:
The string is: and i is 0
goat
The string is: goat and i is 1
boat
The string is: boat and i is 2
toad
The string is: toad and i is 3
The string in sorted order is:
toad
goat
boat
The error in the program is that the first string is a blank one that is automatically inserted into the list. In order to avoid this, i tried using cin.clear() but i am not able to overcome the error. Can anyone please identify the bug and help me with the answer.
One must take special care when using operator>> and getline in the same program. The operator>> leaves an end-of-line indicator in the input stream, which getline accepts.
Try adding std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') before the getline.
cin.clear() doesn't do what you think it does. Look it up. Then follow the advice that you got in Rob's answer.
It's because after you enter the number, the newline is still in the buffer, so the first getline gets that newline. The simplest solution is to use a dummy getline call before the loop.
I'm a beginner and I've been going through a book on C++, and I'm on a chapter on functions. I wrote one to reverse a string, return a copy of it to main and output it.
string reverseInput(string input);
int main()
{
string input="Test string";
//cin>>input;
cout<<reverseInput(input);
return 0;
}
string reverseInput(string input)
{
string reverse=input;
int count=input.length();
for(int i=input.length(), j=0; i>=0; i--, j++){
reverse[j]=input[i-1];
}
return reverse;
}
The above seems to work. The problem occurs when I change the following code:
string input="Test string";
to:
string input;
cin>>input;
After this change, the reverse function returns only the reverse of the first inputted word, instead of the entire string. I can't figure out where I am going wrong.
Lastly, is there a more elegant way of doing this by using references, without making a copy of the input, so that the input variable itself is modified?
The problem is with cin. It stops reading after the first space character is read.
See the "cin and strings" section of this tutorial: http://www.cplusplus.com/doc/tutorial/basic_io/
You can use getline(cin, input); to do what you want.
cin>>input; reads a word, not a line.
Use e.g. getline(cin, input); to read a line
cin >> input reads a word. To read an entire line you should use getline
getline(cin, input);
A debugger is very useful in this cases, you can just see the values of the variables stepping through the program.
A simple cout << input; would have helped you too but if you still don't have a good IDE with integrate debugger I would suggest you to use one. Eclipse is good and open source. Visual studio 2010 express is good and free if you are on windows.
Try this: istream& getline ( istream& is, string& str );
It takes an entire line from a stream you give, e.g. cin and saves it into a string variable. Example:
getline(cin, input);
cin.getline(...) would work on C-style character buffers.
Inplace reverse function was already answered in detail here:
How do you reverse a string in place in C or C++?
The problem with your code is that std::cin reads character till it encounters a character for which std::isspace(c) returns true. So spaces and newlines are all such characters which returns true when passing to std::isspace.
So what you need basically is, std::getline:
std::string input;
if ( std::getline(std::cin, input))
{
std::cout << reverseInput(input);
}
else
{
std::cout <<"error while reading from standard input stream";
}
As for your question about references and copying:
string& reverseInput(string& input)
{
for (i = 0, j = input.length()-1; i < j; i++, j--)
{
char c = input[i];
input[i] = input[j];
input[j] = c;
}
return input;
}
You pass your argument as reference, and you return a reference. No copying involved, and in a body, you don't define any new string, you are working on the same instance.
This is not an error in your reverse function, but the standard behaviour of istream::operator>>, which only reads until the first whitespace character.
You need to use cin.getline(), cin >> s will only read the first word (delimited by space)