- Feb 8, 2011
- 23
- 0
- 0
Hey everyone. Got a question again that I've been racking my brain on trying to fix it and I haven't been able to yet. The program listed below has to do most of what the functions I have set say they do like get the highest balance, print the array, so on and so forth. I have a struct that has a double balance that seems to work fine when I use the function for highest balance but I get crazy math with the totalBalance function. For whatever reason I keep getting what i guess is a memory address or something. I have no idea why as everything else seems to work smoothly. If anyone could help me out I appreciate it. Any other advice on the program too is appreciate.
Oh. Also I have to print out something like student name, balance for the person with the highest balance. I know how to find the highest balance but really I'm lost on trying to use that to access the string name for that element. Thanks in advance.
Oh. Also I have to print out something like student name, balance for the person with the highest balance. I know how to find the highest balance but really I'm lost on trying to use that to access the string name for that element. Thanks in advance.
Code:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//set constant for array size
const int SIZE = 100;
//declaring struct and it's variables
struct studentInfo {
int studentAnumber;
string studentName;
double balance;
};
//Prototypes for functions
void getInfo(studentInfo student[], const int SIZE, int &count);
void searchString(studentInfo student[], int &count, int &leng);
void highestBalance(studentInfo student[], int &count, double &high);
void totalBalance(studentInfo student[], int &count, double &total);
void printArray(studentInfo student[], int &count, int &leng);
int main() {
//declare array for struct studentInfo and initialize to constant size
studentInfo students[SIZE];
cout << "Welcome to the student account manager!" << endl;
//declare and initialize counter variable and call function to get information input into array
int counter = 0;
getInfo(students, SIZE, counter);
//call function to determine the length of the longest studentName in the array
int length;
searchString(students, counter, length);
cout << "Length is now " << length << endl;
//call function to determine the highest balance
double highestBal;
highestBalance(students, counter, highestBal);
cout << "highest balance is " << fixed << showpoint << setprecision(2) << highestBal << endl;
//call function to determine the total balance
double totalBal;
totalBalance(students, counter, totalBal);
cout << "the total balance is " << fixed << showpoint << setprecision(2) << totalBal << endl;
cout << "Report" << endl;
cout << "------" << endl;
cout << "A-NUMBER" << " " << setw(length) << "NAME" << "BALANCE" << endl;
printArray(students, counter, length);
system("Pause");
return 0;
}
void getInfo(studentInfo student[], const int SIZE, int &count) {
for(int i = 1; i < SIZE + 1; i++) {
//Ask user for A number, check for valid input
cout << "Enter student " << i << " A number (or -999 to exit): ";
cin >> student[i].studentAnumber;
if (student[i].studentAnumber == -999) {
break;
}
count = count + 1;
while (cin.fail()) {
cin.clear();
cin.ignore(100,'\n');
cout << "Invalid input. Please enter a valid A number: ";
cin >> student[i].studentAnumber;
}
cin.ignore();
//Ask user for student name. Getline for string
cout << "Enter student " << i << " name: ";
// cin >> students[i].studentName;
getline(cin, student[i].studentName);
//Ask user for balance, check for valid input
cout << "Enter student " << i << " balance: ";
cin >> student[i].balance;
while (cin.fail()) {
cin.clear();
cin.ignore(100,'\n');
cout << "Invalid input. Please enter a valid balance: ";
cin >> student[i].balance;
}
cout << endl;
}
}
void searchString(studentInfo student[], int &count, int &leng) {
leng = student[0].studentName.length();
for(int i = 0; i < count; i++) {
if ( student[i].studentName.length() > leng ) {
leng = student[i].studentName.length();
}
}
}
void highestBalance(studentInfo student[], int &count, double &high) {
double temp = 0;
for(int i = 0; i < count; i++) {
if ( student[i].balance > temp ) {
temp = student[i].balance;
high = temp;
}
}
}
void totalBalance(studentInfo student[], int &count, double &total) {
double temp = 0;
for(int i = 0; i < count; i++) {
temp = temp + student[i].balance;
total = temp;
}
}
void printArray(studentInfo student[], int &count, int &leng) {
for(int i = count; i > 0; i--) {
cout << student[i].studentAnumber << " " << setw(leng) << student[i].studentName << student[i].balance << endl;
}
}