[C++] What's wrong with my quiz answer

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
55
Location
That Chaos Site
XP
483
Country
so I'm doing a test on chapter 6 of my c++ tutorial and one of the questions is this:
Write the following program: Create a struct that holds a student’s name and grade (on a scale of 0-100). Ask the user how many students they want to enter. Dynamically allocate an array to hold all of the students. Then prompt the user for each name and grade. Once the user has entered all the names and grades, sort the list by grade (highest first). Then print all the names and grades in sorted order.
What's wrong with my answer (I know I should have used std::vector but I rushed the lesson on it because I just want to get out of this hellish chapter as soon as possible :cry:)?
http://pastebin.com/amSCY7ja

output:
How many students do you wish to enter?: 2
Shrek
50
Hitler
75

50 got a grade of 0
75 got a grade of 0
 

Hikari06

Well-Known Member
Member
Joined
Nov 20, 2012
Messages
999
Trophies
0
XP
936
Country
Ecuador
Well I don't know c++ very well but I would write something like this instead to begin:

  1. for (int k; k<numberOfStudents; k++)
  2. {
  3. cin >> students[k].name;
  4. cin >> students[k].grade;
  5. }
 
  • Like
Reactions: Nyap

btoast777

Member
Newcomer
Joined
Dec 31, 2013
Messages
8
Trophies
0
Age
30
XP
561
Country
United States
Well, right off the bat I see that in your main's for loop:

  1. for (int student{}; student<numberOfStudents; ++student)
  2. {
  3. cin >> students[student].name;
  4. cin >> students[student].name;
  5. }

You set the student's name twice. That's why your students' names are always their grade number. Line 4 here should be "cin >> students[student].grade."
 
  • Like
Reactions: Nyap

Wellington2k

Well-Known Member
Member
Joined
Nov 1, 2010
Messages
763
Trophies
0
Location
Location
XP
1,271
Country
United States
Why do you use "int start{}" and "++start" in your for loop? I've taken two college level programming classes in C++ and we never used this.
For a for loop, I do this:
for (int i=0; i<count; i++) {
//stuff here
}

EDIT: Also, where is your swap function?
EDIT2: Okay, so swap is actually a built-in function. Learn something new everyday. :P
 
Last edited by Wellington2k,
  • Like
Reactions: codeluca

btoast777

Member
Newcomer
Joined
Dec 31, 2013
Messages
8
Trophies
0
Age
30
XP
561
Country
United States
Two things I have noticed in your sort method:

First, on line 21, is that supposed to be a ">" sign? I'm a bit unfamiliar with the "start{}" expression, but I assume it sets "start" to 0. If that's the case then your loop will never run, since 0 will never be greater than the number of students you have!

Also, you might have a problem when you up your list count to 3 or higher. On line 23 you declare the highestscore and higheststudent. Because this code comes after your first for loop, the next time it goes through the loop it's gonna set those back to zero. (This is known as an issue with "scope," a term you may or may not have not seen in your reading so far). You should instead move line 23 to before that first for loop.

EDIT: Edited to add a little more info.
 
Last edited by btoast777,

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
55
Location
That Chaos Site
XP
483
Country
Why do you use "int start{}" and "++start" in your for loop? I've taken two college level programming classes in C++ and we never used this.
For a for loop, I do this:
for (int i=0; i<count; i++) {
//stuff here
}

EDIT: Also, where is your swap function?
EDIT2: Okay, so swap is actually a built-in function. Learn something new everyday. :P
{} is uniform initialization which was introduced in C++11
my tutorial strongly reccomends using it because it help tell the difference between initialization and assignment, and a couple of other reasons aswell
and I'm too lazy to explain the difference between using ++ as a prefix and as a postfix

--------------------- MERGED ---------------------------

well I've made progress
Code:
void sortStudents(student *studentsToSort, int numberOfStudents)
{
  for (int start{}; start<(numberOfStudents-1); ++start)
  {
  int highestScore{studentsToSort[start].grade}, highestStudent{start};
  for (int current{start+1}; current<numberOfStudents; ++current)
  {
  if ((studentsToSort[current].grade)>highestScore)
  {
  highestScore=studentsToSort[current].grade;
  highestStudent=current;
  }
  }
  swap(studentsToSort[start].grade, highestScore);
  swap(studentsToSort[start].name, studentsToSort[highestStudent].name);
  }
}
How many students do you wish to enter?: 3
Shrek
75
Bloop
23
Hitler
45

Shrek got a grade of 75
Hitler got a grade of 45
Bloop got a grade of 45
 

Wellington2k

Well-Known Member
Member
Joined
Nov 1, 2010
Messages
763
Trophies
0
Location
Location
XP
1,271
Country
United States
{} is uniform initialization which was introduced in C++11
my tutorial strongly reccomends using it because it help tell the difference between initialization and assignment, and a couple of other reasons aswell
and I'm too lazy to explain the difference between using ++ as a prefix and as a postfix
Okay, interesting. I'm pretty sure we were purposefully not taught much C++ 11 to allow our code to work anywhere.
Oh, and I know the difference between prefix and postfix, but I've never had to use prefix in a for loop.
 

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
55
Location
That Chaos Site
XP
483
Country
Oh, and I know the difference between prefix and postfix, but I've never had to use prefix in a for loop.
I'm pretty sure it wouldn't make much of a difference used in a for loop like that

--------------------- MERGED ---------------------------
Two things I have noticed in your sort method:

First, on line 21, is that supposed to be a ">" sign? I'm a bit unfamiliar with the "start{}" expression, but I assume it sets "start" to 0. If that's the case then your loop will never run, since 0 will never be greater than the number of students you have!

Also, you might have a problem when you up your list count to 3 or higher. On line 23 you declare the highestscore and higheststudent. Because this code comes after your first for loop, the next time it goes through the loop it's gonna set those back to zero. (This is known as an issue with "scope," a term you may or may not have not seen in your reading so far). You should instead move line 23 to before that first for loop.

EDIT: Edited to add a little more info.

Yeah, I used the wrong relational operator and I'm fully aware of scope

--------------------- MERGED ---------------------------

I've fixed it! :D
Code:
void sortStudents(student *studentsToSort, int numberOfStudents)
{
  for (int start{}; start<(numberOfStudents-1); ++start)
  {
  int highestScore{start}, highestStudent{start};
  for (int current{start+1}; current<numberOfStudents; ++current)
  {
  if ((studentsToSort[current].grade)>highestScore)
  {
  highestScore=current;
  highestStudent=current;
  }
  }
  swap(studentsToSort[start].grade, studentsToSort[highestScore].grade);
  swap(studentsToSort[start].name, studentsToSort[highestStudent].name);
  }
}
thnx for helping
 
  • Like
Reactions: btoast777

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    SylverReZ @ SylverReZ: Sup