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

Nyap

HTML Noob
OP
Banned
Joined
Jan 13, 2016
Messages
971
Trophies
0
Age
54
Location
That Chaos Site
XP
482
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
926
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
29
XP
551
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,261
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
29
XP
551
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
54
Location
That Chaos Site
XP
482
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,261
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
54
Location
That Chaos Site
XP
482
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
General chit-chat
Help Users
  • JuanMena @ JuanMena:
    Kissing random dudes choking in celery? Really? Need to study for that?
  • K3N1 @ K3N1:
    Yes it requires a degree
  • K3N1 @ K3N1:
    I could also yank out the rest of my teeth but theirs professionals for that
  • x65943 @ x65943:
    If your throat closes, putting oxygen in your mouth will not solve anything - as you will be introducing oxygen prior to the area of obstruction
  • JuanMena @ JuanMena:
    Just kiss me Kyle.
  • x65943 @ x65943:
    You either need to be intubated to bypass obstruction or create a stoma inferior to the the area of obstruction to survive
  • x65943 @ x65943:
    "Just kiss me Kyle." And I thought all the godreborn gay stuff was a smear campaign
  • JuanMena @ JuanMena:
    If I die, tell my momma I won't be carrying Baby Jesus this christmas :sad::cry:
  • K3N1 @ K3N1:
    Smear campaigns are in The political section now?
  • JuanMena @ JuanMena:
    Chary! Chary! Chary, Chary, Chary!
  • Sonic Angel Knight @ Sonic Angel Knight:
    Pork Provolone :P
  • Psionic Roshambo @ Psionic Roshambo:
    Sounds yummy
  • K3N1 @ K3N1:
    Sweet found my Wii u PSU right after I ordered a new one :tpi:
  • JuanMena @ JuanMena:
    It was waiting for you to order another one.
    Seems like, your PSU was waiting for a partner.
  • JuanMena @ JuanMena:
    Keep them both
    separated or you'll have more PSUs each year.
  • K3N1 @ K3N1:
    Well one you insert one PSU into the other one you get power
  • JuanMena @ JuanMena:
    It literally turns it on.
  • K3N1 @ K3N1:
    Yeah power supplies are filthy perverts
  • K3N1 @ K3N1:
    @Psionic Roshambo has a new friend
    +1
  • JuanMena @ JuanMena:
    It's Kyle, the guy that went to school to be a Certified man Kisser.
  • Psionic Roshambo @ Psionic Roshambo:
    Cartmans hand has taco flavored kisses
  • A @ abraarukuk:
    hi guys
  • Iron_Masuku @ Iron_Masuku:
    Hello
    Skelletonike @ Skelletonike: hmm