【发布时间】:2018-06-03 10:48:44
【问题描述】:
我一直在使用 struct 一段时间,但我总是有问题。请参阅此示例:- `
#include <iostream>
struct Employee
{
short id;
int age;
double wage;
};
void printInformation(Employee employee)
{
std::cout << "ID: " << employee.id << "\n";
std::cout << "Age: " << employee.age << "\n";
std::cout << "Wage: " << employee.wage << "\n";
}
int main()
{
Employee joe = { 14, 32, 24.15 };
Employee frank = { 15, 28, 18.27 };
// Print Joe's information
printInformation(joe);
std::cout << "\n";
// Print Frank's information
printInformation(frank);
return 0;
}`
这段代码工作得很好,但是什么时候我将使用字符串而不是“Joe”和“Frank”。我试过但失败了。 这是我正在处理的代码。
'#include <bits/stdc++.h>
using namespace std;
struct People{
string name;
int id;
int age;
int wage;
};
int main(){
string iname;
int iid = 0;
int iage;
int iwage;
while(1){
iid++;
cout << "ID No." << iid << endl <<"Enter Name";
std::getline(std::cin,iname);
cout << "Enter your Age:-";
cin >> iage;
cout << "Enter your Wage :-";
cin >> iwage;
cout << "See your details."<<endl <<"Name"<<iname<< endl<< "ID."<< iid <<
endl << "Age" << iage << endl<< "Wage" << iwage << endl;
People a = static_cast<People>(iid);
People a={iname,iid,iage,iwage};
std::cout << "Name:" << a.name << "\n";
std::cout << "ID: " << a.id << "\n";
std::cout << "Age: " << a.age << "\n";
std::cout << "Wage: " << a.wage << "\n";
}
return 0;
}
用户在此处输入其数据。我想以 struct 的形式保存太多数据,所以我使用了“a”。据我说,它必须计算 1.age=(bla..),3.age=(bla..) 请帮帮我。
【问题讨论】:
-
你说的是变量名吗?发布一个您想要工作但没有工作的代码示例。