-
January 18th, 2014, 05:22 PM
#31
Re: Looking for a Mentor
I think my issue is with how I'm passing the variable from main to openFile but I'm not sure.
-
January 18th, 2014, 05:48 PM
#32
Re: Looking for a Mentor
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 be
Code:
int openFile(ifstream& inFile);
and usually function declarations are placed near the beginning of the program.
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 18th, 2014, 09:14 PM
#33
Re: Looking for a Mentor
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 :-)
-
January 19th, 2014, 01:30 AM
#34
Re: Looking for a Mentor
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?
-
January 19th, 2014, 06:57 AM
#35
Re: Looking for a Mentor
I changed it to void openFile() since it doesn't return a value really, just a statement
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.
However, my getLine is putting the whole line into a string. How do I parse that string into the 3 variables I need?
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 like
Code:
infile >> first >> second >> third;
You can parse the getLIne string into the 3 parts yourself, but its much simpler/easier just to extract them direct from the file.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 19th, 2014, 03:14 PM
#36
Re: Looking for a Mentor
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.
-
January 19th, 2014, 03:52 PM
#37
Re: Looking for a Mentor
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 19th, 2014, 04:40 PM
#38
Re: Looking for a Mentor
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.
-
January 19th, 2014, 05:04 PM
#39
Re: Looking for a Mentor
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 19th, 2014, 05:15 PM
#40
Re: Looking for a Mentor
I figured that out! Hahahaha. Told you. I seem to be asking, and then figuring it out!! Makes me feel so good!!!
You'll be answering questions next!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 19th, 2014, 05:44 PM
#41
Re: Looking for a Mentor
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]);
-
January 19th, 2014, 06:04 PM
#42
Re: Looking for a Mentor
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.
Last edited by 2kaud; January 19th, 2014 at 07:08 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 19th, 2014, 06:09 PM
#43
Re: Looking for a Mentor
Code:
void displayData (const int& vehicleCount, const string& vehicleType, float stats[])
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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
January 19th, 2014, 10:31 PM
#44
Re: Looking for a Mentor
minCharges() returns the variable MIN. I got the whole assignment to work though!!!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|