C++ program problem..

Fandango21

Junior Member
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.



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

Kirby

Lifer
Apr 10, 2006
12,028
2
0
For one thing, when you enter student number, it puts it at that particular place in the array. So when you loop, it starts at 0, even if there is nothing in that particular spot of the array, so you get garbage. You also need to initialize the variable that you pass in the functions.
 

veri745

Golden Member
Oct 11, 2007
1,163
4
81
If you have highestBalance() return an index to the student with the highest balance, it would be easy to get all the information associated with that student.

You have an off-by-one problem with the variable 'count' and your array indexing.

You set your output variable inside the for{} loops for highestBalance() and totalBalance(), that is pointless.
 
Last edited:

veri745

Golden Member
Oct 11, 2007
1,163
4
81
One last thing: You are passing count in as a reference, which means all of your methods that accept it as a parameter could modify it accidentally.

There's not a lot of reason to pass an int by reference if you don't want to modify it. If you really want to pass it by reference, I would make it const.
 

Fandango21

Junior Member
Feb 8, 2011
23
0
0
Ok. Yea I did change all &count to const int &count. I'm really just trying to get used to referencing variables so that's why I did that. I see now how you definately have to be careful or you can change the value. Thanks.
 

Cogman

Lifer
Sep 19, 2000
10,278
126
106
Ok. Yea I did change all &count to const int &count. I'm really just trying to get used to referencing variables so that's why I did that. I see now how you definately have to be careful or you can change the value. Thanks.

DON'T do that. Passing in an int as a const reference just makes your program slower. The only place to use constant references is when you are dealing with large objects (in which case a constant reference is faster than copying the full object). Using it on a basic data type is very wasteful (if the compiler doesn't optimize it back to being pass back to pass by value).

When you pass something by reference, what you are really doing is sending a pointer to the variable into the function. Pointers are relatively small, however, accessing something from them requires the cpu to interpret the pointer and then go fetch the data from memory. If you pass by value, on the other hand, the cpu doesn't have to reinterpret that pointer, it can load the value directly.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Why pass SIZE if it's a constant?

In your highestBalance function, you are assigning the balance to temp and then temp directly into high. You do something similar in totalBalance.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |