Visual C++

Moderators: Lundin
Number of threads: 379
Number of posts: 695

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
sorted names and scores Posted by juanpi on 30 Nov 2012 at 3:15 PM
I am a new in programming and I try to compiler this code but I have problem with sorted part. yo want to get this out out put:
josh 89
Jeff 98
but I got
89
Jeff 98

this is my code

#include <iostream>
#include <string>

using namespace std;

struct StudentGrades
{
string name;
double grade;
};

void sort (StudentGrades *score, int size);


int main ()
{
StudentGrades *sGrade;
int mass;

cout << "Please enter the amount of student grades you wish to enter \n";
cin >> mass;
sGrade = new StudentGrades[mass];

for (int index = 0; index < mass; index ++)
{
cout << "Please enter the [NAME] of student #" << (index + 1) << ": ";
cin >> (sGrade + index)->name;

cout << "Please enter the [SCORE] of student #" << (index + 1) << ": ";
cin >> (sGrade + index)->grade;



while (sGrade[index].grade < 0)
{
cout << "Negative numbers are not accepted, please try again with positive values \n";
cin >> (sGrade + index)->grade;
}
}

cout << "The sorted names and scores are:";
cout << endl;
sort (sGrade, mass);

//cout << fixed << showpoint << setprecision(2);


delete []sGrade;
sGrade = 0;
system ("pause");
return 0;
}

void sort (StudentGrades *score, int size)
{
int minIdx;
double minGrade;
string id;

for (int scan = 0; scan < (size - 1); scan ++)
{
minIdx = scan;
minGrade = (score + scan)->grade;

for (int count = scan + 1; count < size; count ++)
{
if ((score + count)->grade < minGrade)
{
minGrade = (score + count)->grade;
minIdx = count;
id = (score + count)->name;
}
}

(score + minIdx)->grade = (score + scan)->grade;
(score + scan)->grade = minGrade;
(score + minIdx)->name = (score + scan)->name;
(score + scan)->name = id;

}

for (int count = 0; count < size; count ++)
{
cout << (score + count)->name << " ";
cout << (score + count)->grade << " ";
cout << endl;
}
}


thanks for your help.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.