Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Help. Can someone tell me why my data validation loop is busted? Here's my code:
/* Sort Student ID
This program will accept values for student
id, name and final grade from a user.
It will print the values, sort them in the array and reprint the
output in ascending order by Student ID*//* By Jason Quattrone
5/17/08*/#include <iostream>
using namespace std;
struct Student{ // student structure
int id; // initialize int id;
string name; // intialize string name;
char finalGrade; // intialize char for Final Grade
};
int minimum_from(Student a[], int position, int length) //function searches for smallest array value
{
int min_index = position;for (int count = position + 1; count < length; count ++)
{
if (a[count].id < a[min_index].id)
min_index = count;
}return min_index;
}void swap(Student &first, Student &second) // Swap integers function
{
Student temp = first;
first = second;
second = temp;
}void selection_sort(Student a[], int length) // selection sort function
{
for (int count = 0 ; count < length - 1 ; count++)
swap(a[count], a[minimum_from(a,count,length)]);
}int main() // Main Function
{
const int MAX_STUDENTS = 5;
Student students[MAX_STUDENTS]; // Array of students// I added "+1" after every time variable "i" is printed, so it starts
// at 1 rather than 0cout<<endl;
cout<<"Welcome to the Student ID Sorter"<<endl;
for (int i = 0; i < MAX_STUDENTS; i++){ // Prompt user to enter ID of student
cout<<"Enter the ID of a student"<<i+1<<".";
cin>>students[i].id;
}
cout<<endl;
for (int i = 0; i < MAX_STUDENTS; i++){ // Prompt user to enter student name
cout<<"Enter the Name of a student"<<i+1<<".";
cin>>students[i].name;
}
cout<<endl;
for (int i = 0; i < MAX_STUDENTS; i++){
do {
if (students[i].finalGrade && "")
{
cout<<"Enter the Grade for a student"<<i+1<<"."; // Prompt user to enter final grade of student
cin>>students[i].finalGrade;
}
else if ((students[i].finalGrade != 'A') &&
(students[i].finalGrade != 'B') &&
(students[i].finalGrade != 'C') &&
(students[i].finalGrade != 'D') &&
(students[i].finalGrade != 'F'))
{
cout <<"Enter a valid student grade"<<i+1<<".";
cin>>students[i].finalGrade;
}
}
while ((students[i].finalGrade != 'A') &&
(students[i].finalGrade != 'B') &&
(students[i].finalGrade != 'C') &&
(students[i].finalGrade != 'D') &&
(students[i].finalGrade != 'F'));
}
cout<<"Name:\tStudent ID:\tFinal Grade:\n\n"; // Print output in tabular form
for(int i = 0; i < MAX_STUDENTS; i++){
cout<<students[i].name<<"\t\t"<<students[i].id<<"\t\t"<<students[i].finalGrade<<endl;
}cout << endl << "Sorting..." << endl;
selection_sort(students, MAX_STUDENTS); // Call selection_sort function
cout<<"Name:\tStudent ID:\tFinal Grade:\n\n"; // Reprint the results after sort in tabular form
for(int i = 0; i < MAX_STUDENTS; i++){
cout<<students[i].name<<"\t\t"<<students[i].id<<"\t\t"<<students[i].finalGrade<<endl;
}cout << endl << "Thanx for Stopping By :-)";
cout << endl;
return 0;
}

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |