Hi all, I'm new to these forums and fairly new to C++ programming. I'm entering my first year at University, and I'm trying to learn some C++ so I'm not clueless when my course starts.
I can do the basics, initializing & setting variables, if/and/or statements, etc. However, I'm lost when it comes to reading and writing text files, and the tutorial on this website didn't quite cover what I was after.
What I'm looking to do is simple (I hope). I want to read the lines of a .txt file (all numbers), have the program prompt the user to search for either the highest number or lowest number, validate the selection, and then have the program display the details on screen. Oh, and I also want to convert part of a line to a string.
My text file looks like this:
XX YYY.ZZ
XX YYY.ZZ
etc for as long as needed
|
What I want to do is read the text file, assign the XX value a name based on what it is (For example, if I wanted each number to represent a person, the program would assign their name to the output) and then post the YYY.ZZ value. So if the line was 01 517.13, the program would output it as "Name 517.13".
Once the program has read all of the lines (Going to use 10), I want it to write out the new converted lines to another file, so the lines look like this:
NN XX(Converted) YYY.ZZ Calculation |
NN is the line number, straightforward. 01 for the first line, 02 for the second, etc. The XX(Converted) is the original XX value from the first file, except for it is now the name, not the numerical value. The YYY.ZZ value stays the same, and the calculation is a the YYY.ZZ value after having a mathematical calculation done on it.
Any help is appreciated, as I am fairly lost. My thoughts are as follows:
Initialize variables
Set variables (the names that will be assigned to the XX values, and the calculations I'll be doing on the YYY.ZZ values)
Open the text file
Read the lines (using a While loop, stopping when eof reached)
Assign each line to two variables, so "if XX = 01 assign name, else continue" and then it would check if it was 02, 03, 04, etc. The second variable is the YYY.ZZ value.
Preform the mathematical calculation on the YYY.ZZ value.
Ask the user to enter 1 for lowest to highest, and 2 for highest to lowest.
Take the input and sort the YYY.ZZ values accordingly.
Display either the highest or the lowest value (according to selection)
Write all of the lines to a new text file, based on user selection.