gpt4 book ai didi

c++ - 字符显示

转载 作者:行者123 更新时间:2023-11-28 02:49:08 25 4
gpt4 key购买 nike

我知道这可能是一个很奇怪的问题,但几天前它一直困扰着我。我的任务是显示学生的信息并使用 STRUCT 类型更新它们。这是我的作品:

#include <iostream>

using namespace std;

struct DATE
{
int day;
int month;
int year;
};

struct STUDENT{
char ID[8];
char name[50];
DATE birthday;
char address[100];
float Math;
float English;
float CS;
};


void inputClass(STUDENT* &list, int &n)
{
cout << "Please enter the number of students: ";
cin >> n;
list = new STUDENT[n+1];
for(int i=1; i<=n; i++)
{
cout << "Please enter the info of student " << i << endl;

cout << "ID: ";
cin >> (&list[i]) -> ID; //the same with "list[i].ID"
fflush(stdin);

cout << "Name: ";
cin >> (&list[i]) -> name;
fflush(stdin);

cout << "Date of Birth\n";
cout << "Day: ";
cin >> (&list[i]) -> birthday.day;
fflush(stdin);
cout << "Month: ";
cin >> (&list[i]) -> birthday.month;
fflush(stdin);
cout << "Year: ";
cin >> (&list[i]) -> birthday.year;
fflush(stdin);

cout << "Address: ";
cin >> (&list[i]) -> address;
fflush(stdin);

cout << "Math result: ";
cin >> (&list[i]) -> Math;
fflush(stdin);

cout << "English result: ";
cin >> (&list[i]) -> English;
fflush(stdin);

cout << "CS result: ";
cin >> (&list[i]) -> CS;
fflush(stdin);

cout << "************* Next Student *************\n" ;
}
}

void updateScore(STUDENT* list, int n)
{
cout << "Who do you want to update?" << endl;
cout << "Ordinal Number(s): ";
cin >> n;
//Display outdated results
cout << "Student's Name: " << (&list[n])-> name << endl;
cout << "*********** Current Results ***********" << endl;
cout << "Math: " << (&list[n]) -> Math << endl;
cout << "English: " << (&list[n]) -> English << endl;
cout << "CS: " << (&list[n]) -> CS << endl;
//Update results
cout << "Please update the results" << endl;
cout << "Math result: ";
cin >> (&list[n]) -> Math;
fflush(stdin);

cout << "English result: ";
cin >> (&list[n]) -> English;
fflush(stdin);

cout << "CS result: ";
cin >> (&list[]) -> CS;
fflush(stdin);


}

void main()
{
STUDENT* list;
int n;
inputClass(list, n);

updateScore(list, n);
}

在“//显示过期结果”部分,我使用“cout”根据他/她的序号打印出相关学生的姓名。但是,假设我想获得全名,例如:“John Smith”。然而,我得到的只是“约翰”。有什么办法可以获得所有字符吗?

非常感谢您的帮助,抱歉我的英语不好,我是来自越南的学生。

最佳答案

使用 std::getline来自 <string> header ,带有 std::string变量,而不是 >>和原始字符数组。

  • >>读取输入的以空格分隔的

  • 原始字符数组未调整到所需的长度,您可能会在缓冲区溢出时出现未定义行为。


顺便说一句,许多/大多数程序员发现所有大写字母都令人讨厌;它伤害了眼睛。

此外,按照惯例(在 C 和 C++ 中),所有大写字母都保留给宏名称。

关于c++ - 字符显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23459158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com