Display Data in txt.file

Can i have a simple code of the user enter the data (name) then it will display the entire information of it?
Last edited on
1
2
3
4
5
6
7
std::string name;

std::cout << "\n Enter your name: ";
std::getline(std::cin, name);

std::cout << name<<'\n';
Hello rhap123,

The coffer finally kicked in and I noticed that the subject says text file, but the question tends to allude to keyboard input.

First off if you are using a file provide the file, or a fair sample, so everyone can see what you have to work with and use the same information.

Next if this is a school assignment post the instructions that you were given. Not you interpretation which could be different or not quite right.

Then post any code that you have written to show what you have tried.

There is no short code that can be used for reading a file because every file is different and the code has to work with what you need.

With out seeing the input file and knowing what you need to do most of the code would be just a guess and anu suggestions made would have to be reworked for your needs.

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
std::cout << endl << "Please enter name your name : ";
	std::string name;
	getline(cin,name);


	while (name.empty())
	{

	if (std::getline(std::cin, name))
		{
		if (name.empty())
		{
		std::cout << "You pressed enter without typing anything" << std::endl;
		continue;
		}
		else if (name == name){
		ifstream inFile;
		inFile.open("test.txt",ios::in);
			
		cout <<"Name: " << name <<endl;
		cout << "ID: " << ic << endl;
		cout << "Address: " << address <<endl<<endl;
						
		
		myfile<<name<<endl;
		myfile<<id<<endl;
		myfile<<address<<endl;
		inFile.close(); 
		}
						
		else{
		cout << "There is no data" << endl;

		}
	}			
}


So here is my code but somehow it doesnt get the information from the txt file but instead of generating random id
Last edited on
Hello rhap123,

First you have not shown what the input file looks like, so I can not even begin to know if you are reading the file correctly.

Second you have not explained what the program should do. If you expect people to guess at what you need to do, do not expect much response.

When you post code it is best to post a complete program that can be compiled and run.

You are not reading the file stream that you opened, but a totally different file stream that is not even defined.

All your code does is produce errors. Some errors are from variables that are not defined.

The while loop will most likely be bypassed most of the time because of the return value of ".empty()".

The statement else if (name == name) You are comparing the same variable to its-self, so it will always be true. Also in this else if you are opening the file stream and closing at the end of the block. The "close()" is not needed because the closing ( } ) of the block will close the file when it loosed scope.

Your 2 sections of code print to the screen before you read the file and have given the variables something to print.

Some of this may be called "old school", but that is what I first learned.

Define variables.
Open file stream
 You can use
 
std::ifstream inFile("test.txt");  // <--- This defines the file stream and opens the stream at the same time, or when the object is constructed. 

Check that the file opened:
1
2
3
4
5
6
if (!inFile)
{
    std::cout<<"\n     File \"test.txt\" did not open!\n";

    return 1;
}

Read file
Process what is read

When I have more to work with I will have a better idea what you need to do.

For now I would work on opening the file and reading 1 record then print that to the screen. once that part is working you can expand the program.

Andy
else if (name == name){

^^ everything is always equal to itself.

you are attempting writing to the file.
<< is write.
>> is read. they work like cin and cout.
Hello rhap123,

I have asked for the instructions, description of what the program should be and what you need to do, but you have ignored me so far. Now you are saying that you want to do something completely different. Now I am at a loss as to what to do.

There are several suggestions, but no guarantee that any would work.

Compared to your first code this last bit appears to be a step backwards.

My mistake. I should have mentioned about proper indenting before now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "Please enter name." << endl;
cin >> name;

while (!inFile.eof())
{
    getline(inFile, line);

    if ((line == name) != string::npos);
    {
        cout << "name is found!" << endl;

        break;
    }
}


What the compiler actually sees is this:
1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Please enter name." << endl;
cin >> name;

while (!inFile.eof())
{
    getline(inFile, line);

    if ((line == name) != string::npos);

    cout << "name is found!" << endl;

    break;
}

In both the above I have no idea what if ((line == name) != string::npos); is trying to do. The semi-colon at the end of the line limits the if statement to just that line. I am not sure if != string::npos would actually do anything.

What should work better is:
1
2
3
4
5
6
7
8
9
10
11
12
cout << "Please enter name. ";  // <--- Puts the "cin" on the same line.
std::getline(std::cin, name);   // <--- Changed.

while (getline(inFile, line))
{
    if (line == name)  // <--- No (;) needed.
    {
        cout << "name is found!\n";  // <--- But you have no way to know if name is not found.

        break;
    }
}


Now I am thing that a struct to hold name, id and place would work and then put thin in a vector of structs or an array if you have not learned about vectors. This way you could fine any element of the vector and make changes to the struct then eventually write this back to the file.

Andy
Topic archived. No new replies allowed.