Question:
C++ reading names from a file and reversing the order they are in?
soccerproplayer135
2010-11-19 22:14:46 UTC
My question is basically what it says. I've been having trouble trying to write this program. What I need to do is read in up to 100 different names. Using arrays these need to be read in and be output in reverse order such as I would input:
Joey
Tom
Samuel
Kelsey
Dino

and what the output should be is:
Dino
Kelsey
Samuel
Tom
Joey


Please help I've been trying to figure this out for the last hour and all I've managed to accomplish is reading in the first name of the file.

Here is my broken *** code. You would probably be better off starting from scratch.
Four answers:
SomeGuy
2010-11-19 23:53:05 UTC
Well what you first want to do is load everything from the file using the ifstream



#include



ifstream load

load.open ("file.txt");



// loop

// get all the contents from the file using an array

load >> getcontent [counter];

// end loop



Next you'll want to display the names in reverse order

Just simply reverse the array numbers



for (int i = 99 ; i >= 0 ; i --) {

cout << getcountent [i] << endl;

}



That should be it



If you have any further questions don't be afraid to ask me.



Hope this Helped :D
Francis M
2010-11-19 22:35:29 UTC
its always easier to break your problem into smaller parts so it will be manageable. With your problem, the simplest breakdown will be:

1. Read the file

2. Store the items into a STL stack (push)

3. Create a new file for the reordered list

4. Pop each item from the stack, and write it to the new file



Depending on which library you are more comfortable to use, the first and third steps can be achieved via fstream class ofstream. For the data storage, you could use the containers from STL such as list but it will be easier if you use stack since it implements Last In First Out (LIFO) so you will be sure that the new file written will have the names in reverse.
Brandon
2010-11-19 22:37:42 UTC
You forgot to include the code. If you are reading in the first name of the file, make sure you are using a loop to read in 100 names instead of just 1...





// reading a text file

#include

#include

#include

using namespace std;



int main () {

int names [100];

int x = 0;

string line;

ifstream myfile ("names.txt");

if (myfile.is_open())

{

while ( myfile.good() )

{

getline (myfile,line);

names[x] = line;

x++;

}

myfile.close();

}



else cout << "Unable to open file";



return 0;

}





Once you have the array populated with the 100 names, you don't actually need to make another array, all you have to do is count backwards using the original array...



for (ctr=99;ctr>=0; ctr--)

{

cout<< names[ctr] <<"\n";

}



Mind you, I haven't coded in C++ for about 3 years and don't currently have a c compiler installed, so there may be an error or two in there somewhere, but should be the basic format for what you need to do. I hope that helps.
leta
2016-04-29 19:03:17 UTC
If you have a problem to helping your child that has problem reading, no real matter what era she\he the program is the thing you need, Children Learning Reading from here https://tr.im/dun31 .

Children Learning Reading is distinctive from other applications because it does not rely on teaching phrases by view, a method that utilizes kiddies knowing and memorizing phrases by their material and structure. You could have observed (or also ordered!) programs that promote whole word acceptance understanding, these classes frequently need you to remain your youngster in front of a television or pc check for a significant amount of time in order to allow them to learn phrases that may allow them to start their studying journey.

With Children Learning Reading your child may only need to spend 5\15 minutes at time and he\she may learn to construct the term making the whole means of understanding how to see far more effectively.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...