I think my issue is with how I'm passing the variable from main to openFile but I'm not sure.
Printable View
I think my issue is with how I'm passing the variable from main to openFile but I'm not sure.
This is a function declaration for a function that takes no arguments and returns an int. It's not a function call. As openFile is defined after it is going to be used, openFile needs to have a forward declaration before it is used so the compiler knows what arguments it takes and the return type if any. This function declaration would beCode:int openFile();
and usually function declarations are placed near the beginning of the program.Code:int openFile(ifstream& inFile);
To call openFile you would use
openFile is defined as returning an int but the function definition doesn't return any value. What value do you want the function to return if the open succeeds and what value if the open fails? Then when you call the function you should test its return value to check whether the function succeeded or failed.Code:openFile(inFile);
I changed it to void openFile() since it doesn't return a value really, just a statement. Does that work? I could add inside the if(!inFile) someway to close the program?
Also, I put openfile() before main() already. Haha. Noticed that. I changed it to openfile(ifstream& inFile). Figured those out on my own! Yay. I'm learning.
I didn't get the openFile(inFile) though. I'll try that right now :-)
How do you break a string into different variables?
I got openFile to work. And I've gotten getLine figured out. And my loop! Yay.
However, my getLine is putting the whole line into a string. How do I parse that string into the 3 variables I need?
openfile() really should return some value to indicate that the file has been opened properly oe not which is checked for in the calling program. One possibility is have bool openfile() which returns true if the file opened or false if it did not.Quote:
I changed it to void openFile() since it doesn't return a value really, just a statement
Rather than getLine putting the whole line into a string, why not just extract the 3 parts direct via stream extraction (eg as I did in my example program is post #21). Something likeQuote:
However, my getLine is putting the whole line into a string. How do I parse that string into the 3 variables I need?
You can parse the getLIne string into the 3 parts yourself, but its much simpler/easier just to extract them direct from the file.Code:infile >> first >> second >> third;
Thanks! I keep posting questions and then figuring out how to get past them. Hahaha. I've gotten as far as writing data to my array but for some reason the loop is running 1 too many times and giving an extra value in each array that is something like 3.49e-59.
The most common cause of looping 1 too many times is using <= in a condition rather than <. Without seeing the code I can't really offer more guidance.
I figured that out! Hahahaha. Told you. I seem to be asking, and then figuring it out!! Makes me feel so good!!!
However, can you possibly explain enum types to me? I'm supposed to use them in my program to store LOW, HIGH and AVERAGE.
I've gotten my program to work completely including cout statements that display the low high and average values but I don't understand enum at all.
You'll be answering questions next! :cool:Quote:
I figured that out! Hahahaha. Told you. I seem to be asking, and then figuring it out!! Makes me feel so good!!!
Here's my code (just the relevant parts - the rest is checked) but I'm obviously doing it wrong since it isn't working. Suggestions on how to fix it?
Code:void displayData (const int& vehicleCount, const string& vehicleType, float stats[])
{
cout << "For " << vehicleCount << " " << vehicleType << " registrations in the county: " << endl;
cout << "Lowest Registration Charge = $" << stats[LOW] << endl;
cout << "Highest Registration Charge = $" << stats[HIGH] << endl;
cout << "Average Registration Charge = $" << stats[AVERAGE] << endl;
}
int main()
{
// declare the needed variables
ifstream inFile;
const int MAX = 500;
float autoArray[MAX], motoArray[MAX];
int autoCount = 0, motoCount = 0;
enum charges {LOW, HIGH, AVERAGE};
openFile (inFile);
sortData (inFile, autoArray, autoCount, motoArray, motoCount);
charges autoStats[2], motoStats[2];
autoStats[LOW] == minCharges (autoArray, autoCount);
motoStats[LOW] == minCharges (motoArray, motoCount);
autoStats[HIGH] == maxCharges (autoArray, autoCount);
motoStats[HIGH] == maxCharges (motoArray, motoCount);
autoStats[AVERAGE] == avgCharges (autoArray, autoCount);
motoStats[AVERAGE] == avgCharges (motoArray, motoCount);
displayData (autoCount, "automobile", autoStats[charges]);
displayData (motoCount, "motorcycle", motoStats[charges]);
You are using the == conditon where I think you want the = assignment. What does minCharges() function return?
When you create an enum type, the elements are assigned consecutive numbers starting at 0 (unless specified otherwise). So LOW is 0, HIGH is 1 and AVERAGE is 2. You are defining the arrays as having 2 elements (access 0 and 1) but then access using 2 (AVERAGE) so you have a memory buffer overflow as you are accessing memory outside that reserved for the arrays.
You'll also need to change the definiton of autoStats etc from charges to double. Having autoStats an array of charges means that the array elements can only take the values of the type charges (ie LOW HIGH or AVERAGE) which doesn't look like is what you want.
Why is vehicleCount of type const int& ? Parameters of type string are often passed this way but why are you passing an int this way?Code:void displayData (const int& vehicleCount, const string& vehicleType, float stats[])
minCharges() returns the variable MIN. I got the whole assignment to work though!!!