0

I know this maybe such a wierd question but it stucks me for a couple of days ago. My assignemnt is to display the students' information and update them using STRUCT type. Here is my works:

#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);
}

In the "//Display outdated result" section, I used "cout" to print out the Name of the regarding student based on his/her ordinal numbers. However, let's say I want to get the whole name like: "John Smith". What I have got, however, is only "John". Is there a way I can get all of the characters?

Many thanks for your help and sorry for my bad English, I am a student from Vietnam.

apxcode
  • 7,696
  • 7
  • 30
  • 41
Ben
  • 215
  • 1
  • 2
  • 8

2 Answers2

1

Use std::getline from the <string> header, with a std::string variable, instead of >> and raw character array.

  • The >> reads whitespace-separated words of input.

  • The raw character array doesn't adjust to the needed length, and you risk Undefined Behavior on buffer overflow.


In passing, many/most programmers find all UPPERCASE to be an eyesore; it hurts the eyes.

Also, all uppercase is by convention (in C and C++) reserved for macro names.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • I think this is a good point to encourage the use of `std::vector` instead of raw dynamic arrays too. I prefer people getting good knowledge of the C++ language first, and then going to the low-level subset to learn how that magic features actually work. – Manu343726 May 04 '14 at 17:25
  • 1
    can you demonstrate how should I correct my code? Since I'm really new to the programming languagues like C++ :(... Many thanks for your support – Ben May 04 '14 at 17:29
  • well i could "demonstrate", but i chose not to when i wrote this answer. i think the ability to google is a skill that beginners benefit from honing. there is even a saying about this, "give a man a fish, and you feed him for one day. teach him how to fish, and you feed him for life". or, as we once said, "give a man a grilled fish, and you feed him for a day. set him on fire, and you've dispensed with the problem forever". – Cheers and hth. - Alf May 04 '14 at 17:55
  • Thank you for all Cheers and hth, I did it. I did change cin >> (&list[i]) -> name; to getline(cin,list[n].name); and it works! – Ben May 04 '14 at 17:55
  • Congrats! :-) Now make sure you're using `std::string`, not raw character arrays. – Cheers and hth. - Alf May 04 '14 at 17:57
1

As it's already been answered previously, you should use std::getline (refer to this question).

I'm assuming you're using a IDE, and it usually fixes a lot of things for us, users, but this may turn your code non-compilable in other compilers, so there are some things you should fix to be able to compile your code everywhere:

Always pay attention if you're adding the necessary includes. There's lack of an include statement for stdin and fflush. You should add:

#include <cstdio>

Also, main should return an int, so, it should have been something like

int main(int argc, char* argv[]){ /*Although you can usually omit the parameters*/
  // Code

  return 0;
}

By the way, just as a side comment, you forgot the subscript at:

cout << "CS result: ";
cin >> (&list[]) -> CS;
Daniel Carvalho
  • 473
  • 1
  • 5
  • 17
  • well, i'm using VS2010 but when I added #include as you said and it says "No such file or directory". Maybe my code complier is missing something and I should re-install it someday. ...anyway, thanks for your help. – Ben May 04 '14 at 18:03
  • You're welcome. Don't forget to mark Cheers and hth's answer as accepted, if it has answered your question. – Daniel Carvalho May 04 '14 at 18:07