为什么我的while循环从第二轮循环开始忽略了循环体的第一句话呢?
本帖最后由 antonieo 于 2013-06-28 11:11:42 编辑 本人最近正在入门C++,所以这可能是一个很低级的错误,先上我的代码再说问题:
#include <iostream>
#include <string>
using namespace std;
void PrintBigger(string inputLine)
{
if (inputLine != "")
{
cout<<"The bigger string is: "<<inputLine<<endl;
}
}
void PrintLonger(string inputLine)
{
if (inputLine != "")
{
cout<<"The longer string is: "<<inputLine<<endl;
}
}
string GetBiggerString(string in_line1, string in_line2)
{
string biggerString;
if (in_line1 == in_line2)
{
cout<<"Same string."<<endl;
biggerString = "";
}
else if (in_line1 > in_line2)
{
biggerString = in_line1;
}
else
{
biggerString = in_line2;
}
return biggerString;
}
string GetLongerString(string in_line1, string in_line2)
{
string longerString;
if (in_line1.size() == in_line2.size())
{
cout<<"Same length"<<endl;
longerString = "";
}
else if (in_line1.size() > in_line2.size())
{
longerString = in_line1;
}
else
{
longerString = in_line2;
}
return longerString;
}
int main()
{
string line1,line2;
cout<<endl<<"input a line of strings: "<<endl;
while(getline(cin, line1))
{
cout<<"input another line of strings"<<endl;
getline(cin, line2);
cout<<endl<<"Line1 is: "<<line1<<endl<<"Line2 is: "<<line2<<endl;
cout<<endl<<"Get bigger or longer one?[input B/L]"<<endl;
string modeChoise;
while(cin>>modeChoise)
{
if (modeChoise =="B")
{
string biggerString;
biggerString = GetBiggerString(line1, line2);
PrintBigger(biggerString);
break;
}
else if (modeChoise == "L")
{
string longerString;
longerString = GetLongerString(line1,line2);
PrintLonger(longerString);
break;
}
else
{
cout<<"please input B or L"<<endl;
}
}
cout<<endl<<"input a line of strings:"<<endl;
}
return 0;
}
while(cin>>modeChoise)
{
cin.ignore(0x99999, '\n');//跳过换行符
#include <iostream>
#include <string>
using namespace std;
void PrintBigger(string inputLine)
{
if (inputLine != "")
{
cout<<"The bigger string is: "<<inputLine<<endl;
}
}
void PrintLonger(string inputLine)
{
if (inputLine != "")
{
cout<<"The longer string is: "<<inputLine<<endl;
}
}
string GetBiggerString(string in_line1, string in_line2)
{
string biggerString;
if (in_line1 == in_line2)
{
cout<<"Same string."<<endl;
biggerString = "";
}
else if (in_line1 > in_line2)
{
biggerString = in_line1;
}
else
{
biggerString = in_line2;
}
return biggerString;
}
string GetLongerString(string in_line1, string in_line2)
{
string longerString;
if (in_line1.size() == in_line2.size())
{
cout<<"Same length"<<endl;
longerString = "";
}
else if (in_line1.size() > in_line2.size())
{
longerString = in_line1;
}
else
{
longerString = in_line2;
}
return longerString;
}
int main()
{
string line1,line2;
cout<<endl<<"input a line of strings: "<<endl;
while(getline(cin, line1))
{
cout<<"input another line of strings"<<endl;
getline(cin, line2);
cout<<endl<<"Line1 is: "<<line1<<endl<<"Line2 is: "<<line2<<endl;
cout<<endl<<"Get bigger or longer one?[input B/L]"<<endl;
string modeChoise;
while(cin>>modeChoise)
{
char ch;
cin.get(ch);//把换行符读掉。。
if (modeChoise =="B")
{
string biggerString;
biggerString = GetBiggerString(line1, line2);
PrintBigger(biggerString);
break;
}
else if (modeChoise == "L")
{
string longerString;
longerString = GetLongerString(line1,line2);
PrintLonger(longerString);
break;
}
else
{
cout<<"please input B or L"<<endl;
}
}
cout<<endl<<"input a line of strings:"<<endl;
}
return 0;
}