I have this program that is placeing output into an outside text file, and it's working just fine (unless our reference file "register.txt" has a \n at the end of the file). I know I need to use a priming read to control this situation for a \n at the end of the file, but I'm not quite sure how to use it in this situation.
Thanks for any assistance! Below is the main part of my code that includes this issue.
I know I need to use a priming read to control this situation for a \n at the end of the file, but I'm not quite sure how to use it in this situation.
What you need to do is ensure your data extraction was successful before using the data you extracted. Also, consider not using the C-style of declaring every variable at the beginning of a function and limit them to smaller scopes when possible.
This works fine when I have one line, but let's say that my "REGISTER.txt" file has multiple lines for each household. I want it to reproduce this loop everytime a new line is started. But, once you reach end of the file (whether it's after the final data extracted or it's another line in the data) I want it to stop the loop.
consider not using the C-style of declaring every variable at the beginning of a function and limit them to smaller scopes when possible
I realize the importance of this in general as you mentioned, but does this apply even in while-loops like where you declared the char variable vehicle_type inside the loop? Might that lead to unnecessary overhead during runtime to have the program re-declaring the variable every iteration of the while-loop, or am I off in left field? Please let me know, I'm curious.
Let me show you my entire code thus far, since I still can't figure it out. (By the way, i declare every variable at the beginning because it's a requirement of my class).
So, my text file "REGISTER.txt" says:
1 2 3 4
111111 A ABC123 2012 36666 A XYZ789 2005 15555
222222 A DDD333 1999 22222
333333 A FFF888 2000 12345 M BBB888 2000 9988 M GHW888 1990 7711
//<this is end of file on new line>//
#include <sstream>
// replace your nested loops with:
std::string line ;
while ( std::getline(register_file, line) && line.length() ) // retrieve all of a single line from the file.
{ // and if it isn't empty:
sum = 0 ;
count = 0 ;
std::istringstream is(line) ; // convert the line into a stream
is >> id_num ;
while ( is >> vehicle_type >> license_plate >> model_year >> msrp )
{
calcTax(msrp, model_year, vehicle_type, ownership_tax, registration_charge) ;
vehicle_file << fixed << setprecision(2) << vehicle_type << ' '
<< license_plate << ' ' << setw(10) << registration_charge
<< endl ;
sum += registration_charge ;
++count ;
}
std::cout << fixed << setprecision(2) << setw(9) << id_num << setw(8)
<< count << setw(9) << '$' << setw(10) << sum << endl ;
total_collected += sum ;
}
Also, your code should not compile since you tell the compiler that calcTax will return a value but it does not. total_collected should be initialized to 0. In your code, it is not initialized to any meaningful value. There is no reason for ownership_tax to be a parameter to calcTax since it should be a local-to-calcTax variable in its current implementation.
So, I tried using a "return registration_charge" in my function, but it isn't returning for some reason. This is why I swapped it out for a pass parameter. Why is it not returning the value back to the main function when i try to use return?
So, I tried using a "return registration_charge" in my function, but it isn't returning for some reason. This is why I swapped it out for a pass parameter. Why is it not returning the value back to the main function when i try to use return?
Did the call look something like the following when you were attempting to return a value?