将值输入到struct数组+打印出来 [英] input values into struct array + printing out
问题描述
我被赋予了问题域。输入结构数组C ++
I've been given a problem domain. Inputting into struct array C++
一个班级有5个学生。您需要编写一个程序来接受用户的以下信息。
A class has 5 students. You need to write a program to accept the following information from the user.
First Name
Last Name
Age
Major
GPA
所有这些信息必须从用户处获得并存储在数组中填充数组后,为每个学生打印所有这些信息。
All these information must be obtained from the user and stored in an array. Once the array has been populated, print all these information for each and every students.
对于执行此项目,您可能希望使用struct,数组和某些循环。确保使用正确的数据类型来存储信息。接受GPA时,您需要确保GPA大于或等于2且小于或等于4。如果学生的GPA超出此范围,请要求用户再次输入GPA,并给他限制。
For doing this project, you may like to user struct, arrays, and some loops. Make sure the proper datatypes are used to store the information. While accepting the GPA, you need to make sure that GPA is greater than or equal to 2 and less than or equal to 4. If the GPA of the student is beyond this range, ask the user to input the GPA again, giving him the limits.
我需要知道如何将值输入到struct数组中,然后将其打印出来。这是我到目前为止所拥有的。任何帮助,将不胜感激。
I need to know how to input values into the struct array, and then print them out. Here is what i have so far. Any help would be appreciated.
#include <iostream>
#include <string>
using namespace std;
typedef struct
{
string firstName;
string lastName;
int age;
string major;
float GPA;
} student;
int main ()
{
//Variable declaration
string fnInput;
string lnInput;
int ageInput;
string majorInput;
float GPAInput;
student students[4];
cout << "Enter the first name: " ;
cin >> fnInput ;
cout << "Enter the last name: " ;
cin >> lnInput ;
cout << "Enter the age: ";
cin >> ageInput ;
cout << "Enter the major: " ;
cin >> majorInput;
cout << "Enter the GPA: ";
cin >> GPAInput ;
cout << fnInput << lnInput << ageInput << majorInput << GPAInput ;
/*students[0].firstName = fnInput;*/
}
推荐答案
要将值输入到struct数组中,不需要临时变量,只需直接存储输入值即可:
To input values into the struct array, you don't need temporary variables, just store the input values directly:
std::cout << "Enter the first name: " ;
std::cin >> students[0].firstName;
std::cout << "Enter the age: ";
std::cin >> students[0].age;
输出类似:
std::cout << students[0].firstName;;
std::cout << students[0].age;
这篇关于将值输入到struct数组+打印出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!