急关于 cin 与 getline的有关问题
急求一个关于 cin 与 getline的问题
//请教下高手门如何解决输入命令有空格的问题
//这样编写解决了不少其他问题但用cin接收进来的choice不能接受空格 像输入f f就会重复输出2次tF
//如果用了getline又要2次回车才能完成输入 怎么样能一次回车就能解决f f这重错误
#include <string>
#include <iostream>
using namespace std;
int main ()
{
while(1) {
bool run=true;
string choice;
cout < < "You choice? " < <endl;
cin> > choice;
if(choice.size()> 1) {
cout < <endl < < "Errer: **Please enter correct option! ** " < <endl;
run=false;
}
if(run) {
switch (choice[0])
{
case 'F ': case 'f ':
cout < < "tF " < <endl;
break;
case 'N ' : case 'n ':
cout < < "tN " < <endl;
break;
case 'P ': case 'p ':
cout < < "tP " < <endl;
break;
case 'R ': case 'r ':
break;
case 'A ': case 'a ':
cout < < "tA " < <endl;
break;
case 'D ': case 'd ':
cout < < "tD " < <endl;
break;
case 'Q ': case 'q ':
cout < < "tQ " < <endl;
break;
case 'C ': case 'c ':
cout < < "tC " < <endl;
default:
cout < <endl < < "Errer: **Please enter correct option ! ** " < <endl;
}
}
}
return 0;
}
------解决方案--------------------
getline为啥要2次回车,你是VC6且没有打sp6补丁吧。
------解决方案--------------------
#include "stdafx.h "
#include <string>
using namespace std;
int main ()
{
bool run=true;
string choice;
while(1)
{
cout < < "You choice? " < <endl;
(cin> > choice).get();
if(choice.size() <1)
{
cout < <endl < < "Errer: **Please enter correct option! ** " < <endl;
run=false;
}
if(run)
{
switch (choice[0])
{
case 'F ': case 'f ':
cout < < "tF " < <endl;
break;
case 'N ' : case 'n ':
cout < < "tN " < <endl;
break;
case 'P ': case 'p ':
cout < < "tP " < <endl;
break;
case 'R ': case 'r ':
break;
case 'A ': case 'a ':
cout < < "tA " < <endl;
break;
case 'D ': case 'd ':
cout < < "tD " < <endl;
break;
case 'Q ': case 'q ':
cout < < "tQ " < <endl;
break;
case 'C ': case 'c ':
cout < < "tC " < <endl;
default:
cout < <endl < < "Errer: **Please enter correct option ! ** " < <endl;
//请教下高手门如何解决输入命令有空格的问题
//这样编写解决了不少其他问题但用cin接收进来的choice不能接受空格 像输入f f就会重复输出2次tF
//如果用了getline又要2次回车才能完成输入 怎么样能一次回车就能解决f f这重错误
#include <string>
#include <iostream>
using namespace std;
int main ()
{
while(1) {
bool run=true;
string choice;
cout < < "You choice? " < <endl;
cin> > choice;
if(choice.size()> 1) {
cout < <endl < < "Errer: **Please enter correct option! ** " < <endl;
run=false;
}
if(run) {
switch (choice[0])
{
case 'F ': case 'f ':
cout < < "tF " < <endl;
break;
case 'N ' : case 'n ':
cout < < "tN " < <endl;
break;
case 'P ': case 'p ':
cout < < "tP " < <endl;
break;
case 'R ': case 'r ':
break;
case 'A ': case 'a ':
cout < < "tA " < <endl;
break;
case 'D ': case 'd ':
cout < < "tD " < <endl;
break;
case 'Q ': case 'q ':
cout < < "tQ " < <endl;
break;
case 'C ': case 'c ':
cout < < "tC " < <endl;
default:
cout < <endl < < "Errer: **Please enter correct option ! ** " < <endl;
}
}
}
return 0;
}
------解决方案--------------------
getline为啥要2次回车,你是VC6且没有打sp6补丁吧。
------解决方案--------------------
#include "stdafx.h "
#include <string>
using namespace std;
int main ()
{
bool run=true;
string choice;
while(1)
{
cout < < "You choice? " < <endl;
(cin> > choice).get();
if(choice.size() <1)
{
cout < <endl < < "Errer: **Please enter correct option! ** " < <endl;
run=false;
}
if(run)
{
switch (choice[0])
{
case 'F ': case 'f ':
cout < < "tF " < <endl;
break;
case 'N ' : case 'n ':
cout < < "tN " < <endl;
break;
case 'P ': case 'p ':
cout < < "tP " < <endl;
break;
case 'R ': case 'r ':
break;
case 'A ': case 'a ':
cout < < "tA " < <endl;
break;
case 'D ': case 'd ':
cout < < "tD " < <endl;
break;
case 'Q ': case 'q ':
cout < < "tQ " < <endl;
break;
case 'C ': case 'c ':
cout < < "tC " < <endl;
default:
cout < <endl < < "Errer: **Please enter correct option ! ** " < <endl;