Hey guys, i am currently working on an assignment of an intro to c++ class, and i am being asked to read from a file called Bands.txt which has a list of band information (rank/sales/band name (in that order)) and then write from that Bands.txt file to a file called Results.txt and it's supposed to have the sales converted from a number to asterisks (ex. 5 to ******).
What i have so far is the reading part from the Bands.txt, but i cannot get the program to create Results.txt and write the list there :(
Any help is appreciated, thanks :)
Here's what i have:
fstream writeFile;
writeFile.open( "Result.txt", ios::out );
// inside while loop --
// parse info
// if info is sales
// write out with *****
// else
// write out line
// ------------------------
writeFile.close();
Computers are stupid - they will do exactly what you tell them to do.
BTW, did you just copy, paste and edit your whole chunk of code?
Ask yourself: what did I just tell the computer to do in the new code?
1. Open Bands.txt, read all the text in the file, close the file.
2. Open Results.txt, write what's in info, ten times, close the file.
Well, at the end of 1, info will contain just the very last value from Line 28, right?
So the computer just did what you told it to do!
Clue: you need to intermingle your code a little bit - rather than doing one file at a time, open both files and process one line at a time: read one line, write one line, etc...
When coding, think about what the computer will do when it reads your code, because the machine is really dumb - it will do exactly what you tell it to do.
Oh wow thanks, i got it to write bands.txt to results.txt, man i was stuck on this for so long its not even funny, and with help too, but for some reason your suggestion was the easiest to understand for me. Ok well now that i have this, do you have any clue on how to do the asterisks? I have to convert an int to an asterisk, and this is another thing i have been stuck at, and i ask for help on that one but no one answers :(
A big part of problem-solving and programming is breaking down what you want to do into smaller, easy-to-do parts. When you get more experience, you will be able to do this naturally.
You are thinking about it the wrong way.
1. You start with a character array, char info[SIZE];
2. If you detect the pattern that you are looking for, let's say the 5 in XXXXXX5YYY
3. Then you want to copy XXXXXX into your new string, append ****** to that string, and then append YYY into that string.
4. Write out your new string into Result.txt
If you want to continue using a character array (which is fine), as opposed to std::string in C++, look into strncpy() and strcat(). Good luck.