第一次程序运行正常,但是第二次它没有从用户输入字符串,我不知道为什么?
我认为getline(cin,str);
中存在问题,但我无法解决问题
#include<iostream>
#include<string>
using namespace std;
int main(){
string str;
char check;
do{
system("cls");
cout<<"Enter the number (sample : +92121234567(Must contain 12 characters)) : ";
getline(cin,str);
cout<<endl<<str;
if(str.length() == 12){
string countrycode = str.substr(0,3);
if(countrycode == "+92"){
string citycode = str.substr(3,2);
if(citycode == "51"){
cout<<endl<<"This number belongs to Islamabad";
}else if(citycode == "42"){
cout<<endl<<"This number belongs to Lahore";
}else if(citycode == "21"){
cout<<endl<<"This number belongs to Karachi";
}else{
cout<<endl<<"Unknown Number";
cout<<endl<<"Remarks : ";
cout<<endl<<"Unknown city code";
}
}else{
if(str.substr(0,1) =="-"){
cout<<endl<<"Unknown Number";
cout<<endl<<"Remarks : ";
cout<<endl<<"Wrong country code format";
}
if(str.substr(1,2) !="92"){
cout<<endl<<"Unknown Number";
cout<<endl<<"Remarks : ";
cout<<endl<<"Unknown country code";
}
}
}else{
cout<<endl<<"Unknown Number";
cout<<endl<<"Remarks : ";
cout<<endl<<"Number is over 12 characters";
}
cout<<endl<<"Do you want to enter another number (y/n): ";
cin>>check;
//str.clear();
}while(check=='y'||check=='Y');
cout<<endl<<"End of Program. Bye!"<<endl;
system("pause");
return 0;
}
答案 0 :(得分:-1)
使用cin >> str; 不知道为什么,但是getline对我也不起作用。