求帮助,这题是用结构数组建立并初始化一个工资表,然后输入一个人得姓名,查询其工资
#include<iostream>
#include<string>
using namespace std;
struct people
{
string name;
int namey;
} ;
int main()
{
people s[3];
int i,j,n;
cout<<"输入工人的信息:"<<endl;
for(i=0;i<3;i++)
{
cin>>s[i].name>>s[i].namey;
}
string str;
cin>>str;
for(i=0;i<3;i++)
{
if(str==s[i].name)
n=1;
cout<<s[i].namey;
break;
}
return 0;
}
我的问题是,如果我输入的名字不是名单上的,想重新输入该如何编写代码。
}
[解决办法]
设计一个Find函数,找到你先前输入的那个,然后修改!
[解决办法]
帮你改了下!
- C/C++ code
#include<iostream>#include<string>using namespace std;struct people{ string name; int namey;};int Find(string name,people s[],int n){ int namey; for (int i=0; i<n; ++i) { if (s[i].name == name) { namey = s[i].namey; return namey; } } return -1;}int main(){ people s[3]; int i,j,n; cout<<"输入工人的信息:"<<endl; for(i=0;i<3;i++) { cin>>s[i].name>>s[i].namey; } string name; while(1) { cout<<"name:"; cin>>name; if (name == "q") { break; } else { int namey = Find(name,s,3); if (-1 == namey) { cout<<"No information of this people name="<<name<<endl; } else { cout<<namey<<endl; } } } return 0;// string str;// cin>>str;// for(i=0;i<3;i++)// {// if(str==s[i].name)// n=1;// cout<<s[i].namey;// break;// } // // return 0;}
[解决办法]
或者按1楼说的,另外写个函数
- C/C++ code
cin>>str;for(i=0;i<3;i++) {if(str==s[i].name)n=1;cout<<s[i].namey;break;
我的异常网推荐解决方案:软件开发者薪资,http://www.myexception.cn/other/1391128.html