[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
  • K3Nv2 @ K3Nv2:
    Sorry I sat on them when they were open
  • AncientBoi @ AncientBoi:
    eeewww
  • K3Nv2 @ K3Nv2:
    I thought it was the toilet
  • AncientBoi @ AncientBoi:
    okies. Time to go watch YT paranormal ghost things. L8er my luvs :D
    +1
  • K3Nv2 @ K3Nv2:
    I got a massive clue
  • BakerMan @ BakerMan:
    this mf def ain't watching ghost shit, he boutta beat his meat fr
    +1
  • K3Nv2 @ K3Nv2:
    Nah he's about to be the ghost in your bedroom
    +1
  • Xdqwerty @ Xdqwerty:
    @K3Nv2, and leave ectoplasm all over the place
  • BakerMan @ BakerMan:

    this is him being described
    +2
  • Xdqwerty @ Xdqwerty:
    Sigh
  • Xdqwerty @ Xdqwerty:
    Yawn
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, I dislike the kind of drm where you have to play single player games online all the time bc of some verification bs
    +1
  • SylverReZ @ SylverReZ:
    @Xdqwerty, Don't use games that have Easy Anti-Cheat as its been exploited many times.
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, my PC can't run most AAA games so i wont
    +1
  • Xdqwerty @ Xdqwerty:
    Most of the modern AAA games
    +1
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, I also heard one of the Prince of Persia games was so unfinished that it required the "24/7 online" drm so a puzzle could be done and the game could be finished. And that when the Ubisoft servers were closed the (cracked) game was impossible to finish or something like that
  • SylverReZ @ SylverReZ:
    @Xdqwerty, That's extra scummy. Ubisoft nowadays ship out incomplete games like Skull and Bones which was being worked on for nearly a decade now.
    +1
  • Xdqwerty @ Xdqwerty:
    @SylverReZ, i think they have been doing that since late 2000s
    +1
  • Xdqwerty @ Xdqwerty:
    Either that or their old games were unfinished aswell but we can't notice it
  • Psionic Roshambo @ Psionic Roshambo:
    I like that games can be fixed after the fact, hate that it's being abused via beta tests... And DLC... I was a 7800 owner back in the day and loved Impossible Mission, turns out I couldn't beat it because it was actually impossible lol
  • Psionic Roshambo @ Psionic Roshambo:
    I never knew about it at the time but a fixed version was available but you had to mail in your broken copy lol
  • Psionic Roshambo @ Psionic Roshambo:
    So that version is semi rare
    Psionic Roshambo @ Psionic Roshambo: So that version is semi rare