帮忙看一下程序,谢谢

stardust20 2008-07-22 03:56:57
各位高手帮我看下为什么下面的程序会出现多余Next string (q to quit):输出谢谢了
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
char c;
string str;
cout<<"Enter a string (q to quit):\n";
getline(cin,str);
while(str!="q")
{
for(int i=0; i < str.length(); i++)
{
c = toupper(str[i]);
cout<<c;
}
cout<<endl;
cout<<"Next string (q to quit):\n";
getline(cin,str);
}
cout<<"Bye\n";
return 0;
}
...全文
103 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
stardust20 2008-07-22
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
char c;
string str;
cout<<"Enter a string (q to quit):\n";
getline(cin,str);
cin.get();
while(str!="q")
{
for(int i=0; i < str.length(); i++)
{
c = toupper(str[i]);
cout<<c;
}
cout<<endl;
cout<<"Next string (q to quit):\n";
getline(cin,str);
cin.get();
}
cout<<"Bye\n";
return 0;
}
这样就可以了
stardust20 2008-07-22
  • 打赏
  • 举报
回复
1楼的也谢谢了,我用的是vc6.0,可能不同吧,不过还是谢谢
stardust20 2008-07-22
  • 打赏
  • 举报
回复
可是如果换成cin>>str;的话如果输入的字符串中有空格如“lkdfg fsk”就会有问题了,不过还是谢谢了
flxmcx 2008-07-22
  • 打赏
  • 举报
回复
2 搂正解!
heimu2257 2008-07-22
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
char c;
string str;
cout << "Enter a string (q to quit):\n";
cin >> str;
while(str!="q")
{
for(int i=0; i < str.length(); i++)
{
c = toupper(str[i]);
cout << c;
}
cout << endl;
cout << "Next string (q to quit):\n";
cin >> str;
//getline(cin,str); 这个函数要等待2次回车起用
}
cout << "Bye\n";
return 0;
}

xingshi8000 2008-07-22
  • 打赏
  • 举报
回复
在我这里没有问题啊,VS2003

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧