When it prints it does not print the numbers and names in ascending order like its suppose to do and i have no clue why it doesnt. This is for school and this is the book we are using in class: sixth edition-starting out with C++ early objects. We are as far as chapter 10-pointers this would be program challenge problem #2 in the book.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct StudentInfo
{
string name;
int score;
StudentInfo(string n = "guest", int s = 0.0)
{ name = n;
score = s;
}
};
void sortArray(StudentInfo *,int);
void showArray(StudentInfo *,int);
//void CalculateArray(double [] , int);
int main()
{
double total = 0.0;
int numScores,
count;
StudentInfo *student;
cout <<"How many test scores are you inputting today?"<<endl;
cin >> numScores;
student = new StudentInfo[numScores];
cout << "Please enter the name of the student press enter then " << endl;
cout << "Please input each test score that you need to be calculated"<<endl;
for (count = 0; count < numScores; count++)
{
cout << "Student " << (count + 1) <<" name is : ";
cin >> (student + count)->name;
cout << "Test Score : " << (count + 1) <<" is " ;
cin >> (student + count)->score;
}
sortArray(student , numScores);
showArray(student , numScores);
return 0;
}
void sortArray(StudentInfo *student,int numScores)
{
StudentInfo *temp = student;
bool swap;
do
{
swap = false;
for (int counter = 0; counter < (numScores - 1); counter++)
{
if ((student + counter)->score > (student + counter + 1)->score)
{
temp->name = (student + counter)->name;
(student + counter)->name = (student + (counter + 1))->name;
(student + (counter + 1))->name = temp->name;
temp->score = (student + counter)->score;
(student + counter)->score = (student + (counter + 1))->score;
(student + (counter + 1))->score = temp->score;
swap = true;
}
}
} while (swap);
}
void showArray(StudentInfo *student,int numScores)
{
for (int count = 0; count < numScores; count++)
{
cout << (student + count)->name;
cout << (student + count)->score;
cout << endl;
}
}