This question already has an answer here:
这个问题在这里已有答案:
- std::cin.getline( ) vs. std::cin 3 answers
std :: cin.getline()vs。std :: cin 3 answers
#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?
我在Turbo C ++ 4.5中工作。代码运行正常,但它没有给出所需的输出,例如,如果我输入“steve hawking”只显示“史蒂夫”。有人可以帮忙吗?
5 个解决方案
#1
21
Using >>
on a stream reads one word at a time. To read a whole line into a char
array:
在流上使用>>一次读取一个单词。要将整行读入char数组:
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
当然,一旦你学会了如何实现一个字符串,你应该使用std :: string并将其读作
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.
从本世纪开始编译器也是一个非常好的主意;你的年龄超过15岁,从那时起C ++发生了重大变化。如果您想要一个免费的Windows编译器,Visual Studio Express是一个不错的选择;其他编译器可用。
#2
3
cin>>str;
This only reads in the next token. In C++ iostreams, tokens are separated by whitespace, so you get the first word.
这只会读入下一个标记。在C ++ iostreams中,令牌由空格分隔,因此您获得第一个单词。
You probably want getline, which reads an entire line into a string:
你可能想要getline,它将整行读入一个字符串:
getline(cin, str);
#3
2
You can use :
您可以使用 :
cin.read( str, sizeof(str) );
But, this will fill up the buffer. Instead you should use cin.getLine() as MikeSeymour suggested
但是,这将填补缓冲区。相反,你应该使用cin.getLine()作为MikeSeymour建议
#4
1
You could use cin.getline to read the whole line.
您可以使用cin.getline来读取整行。
#5
0
use this
cin.getline(cin, str);
#1
21
Using >>
on a stream reads one word at a time. To read a whole line into a char
array:
在流上使用>>一次读取一个单词。要将整行读入char数组:
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
当然,一旦你学会了如何实现一个字符串,你应该使用std :: string并将其读作
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.
从本世纪开始编译器也是一个非常好的主意;你的年龄超过15岁,从那时起C ++发生了重大变化。如果您想要一个免费的Windows编译器,Visual Studio Express是一个不错的选择;其他编译器可用。
#2
3
cin>>str;
This only reads in the next token. In C++ iostreams, tokens are separated by whitespace, so you get the first word.
这只会读入下一个标记。在C ++ iostreams中,令牌由空格分隔,因此您获得第一个单词。
You probably want getline, which reads an entire line into a string:
你可能想要getline,它将整行读入一个字符串:
getline(cin, str);
#3
2
You can use :
您可以使用 :
cin.read( str, sizeof(str) );
But, this will fill up the buffer. Instead you should use cin.getLine() as MikeSeymour suggested
但是,这将填补缓冲区。相反,你应该使用cin.getLine()作为MikeSeymour建议
#4
1
You could use cin.getline to read the whole line.
您可以使用cin.getline来读取整行。
#5
0
use this
cin.getline(cin, str);