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;
		}
}

Recommended Answers

All 2 Replies

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;
		}
}

oh sorry i forgot to mention that it suppose to print out the names and scores in ascending order.

oh sorry i forgot to mention that it suppose to print out the names and scores in ascending order.

You did? Here's your first post. Read the first line:

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.

Also, you don't need to quote your entire message to add a single line.

Lastly, your code is a mess. See this to clean up your code so it's readable. Pay attention to the indentation section. I for one don't want to decipher the code.

All I can suggest is look at your sort function and print out values during the sorting to see what is happening. Or, reformat and post the sort function and explain in full what you expect and what is actually happening.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.