Looking for a Mentor - Page 3
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 44 of 44

Thread: Looking for a Mentor

  1. #31
    Join Date
    Jan 2014
    Location
    Portland, OR
    Posts
    34

    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.

  2. #32
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: Looking for a Mentor

    Code:
    int openFile();
    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
    Code:
    openFile(inFile);
    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #33
    Join Date
    Jan 2014
    Location
    Portland, OR
    Posts
    34

    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 :-)

  4. #34
    Join Date
    Jan 2014
    Location
    Portland, OR
    Posts
    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?

  5. #35
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #36
    Join Date
    Jan 2014
    Location
    Portland, OR
    Posts
    34

    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.

  7. #37
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #38
    Join Date
    Jan 2014
    Location
    Portland, OR
    Posts
    34

    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.

  9. #39
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    Re: Looking for a Mentor

    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #40
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  11. #41
    Join Date
    Jan 2014
    Location
    Portland, OR
    Posts
    34

    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]);

  12. #42
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    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 06:08 PM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #43
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #44
    Join Date
    Jan 2014
    Location
    Portland, OR
    Posts
    34

    Re: Looking for a Mentor

    minCharges() returns the variable MIN. I got the whole assignment to work though!!!

Page 3 of 3 FirstFirst 123

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center