【发布时间】:2019-10-18 02:17:25
【问题描述】:
我正在尝试学习 C++ 中的结构。当我像这样运行我的代码时:
#include<iostream>
using namespace std;
struct person{
char firstName[50];
int age;
float gradYear;
};
int main(){
person p1;
cout << "Whats your FIRST name?";
cin.get(p1.firstName, 50);
cout << "Whats your AGE?";
cin >> p1.age;
cout << "Whats your GRADUATION YEAR?";
cin >> p1.gradYear;
cout << "Displaying Your Information . . . " << endl;
cout << "First Name: " << p1.firstName << endl;
cout << "Age: " << p1.age << endl;
cout << "Graduation Year: " << p1.age << endl;
return 0;
}
代码按我的意愿工作,但是当我像这样运行代码时:
#include<iostream>
using namespace std;
struct person{
char firstName[50];
char lastName[50];
int age;
char branch[50];
float gradYear;
};
int main(){
person p1;
cout << "Whats your FIRST name?";
cin.get(p1.firstName, 50);
cout << "Whats your LAST name?";
cin.get(p1.lastName, 50);
cout << "Whats your AGE?";
cin >> p1.age;
cout << "Are you Corps or Civilian?";
cin >> p1.branch;
cout << "Whats your GRADUATION YEAR?";
cin >> p1.gradYear;
cout << "Displaying Your Information . . . " << endl;
cout << "First Name: " << p1.firstName << endl;
cout << "Last Name: " << p1.lastName << endl;
cout << "Age: " << p1.age << endl;
cout << "Branch: " << p1.branch << endl;
cout << "Graduation Year: " << p1.age << endl;
return 0;
}
在我输入名字后,代码会输出信息。为什么会这样做,我怎样才能让它以第二种方式工作?
【问题讨论】: