Computing.Net > Forums > Programming > C++ Data Validation

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ Data Validation

Reply to Message Icon

Name: fender0327
Date: May 24, 2008 at 16:47:31 Pacific
OS: Ubuntu
CPU/Ram: 768
Product: HP
Comment:

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 0

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



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: May 25, 2008 at 01:01:39 Pacific
Reply:

It seems to only allow A, B, C, D, or F. What's the problem?


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ Data Validation

JS data validation...Opinions? www.computing.net/answers/programming/js-data-validationopinions/9043.html

Java - validating user input www.computing.net/answers/programming/java-validating-user-input/12223.html

help help!!!..C...data file..please www.computing.net/answers/programming/help-helpcdata-fileplease/8092.html