Reading txt file and creating an Struct with Arrays

Im given information about a person ID, Name, and Address in a text file for example like this:

123543
Joe, Shomoe
36 Jon Havey Court, Middle, MO 66222

no more than 20 people in the given txt file.

Some requirements:
1.each person should take one structure, and should be defined as to hold name, ID, and address

2.print the information of all people according to ID # in ascending order.

3.Ask a user if additional person to add. if so new person is added to the same format as in the given txt file.

4. Search a person by the last name. Prints out the info immediately.

I know im missing some of the requirements but im having trouble setting compiling the current code i have right now.

Any help is appreciated.

Compiler is: Microsoft Visual Studio

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
 #include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

struct Personalinfo  //Structure for the information: ID, Name, and Address
{
	int ID; //ID number
	char name[25]; //Name of person: Last_name,First_name
	char addr[60]; //Address info for person

};


int main()
{
	string line;
	ifstream t1("a1.txt");
	{
		if (t1.is_open())//opens and reads the information from the file to display
		{
			while (!t1.eof())
			{
				getline(t1, line); //gets information from the text file
				cout << line << endl;
			}

			t1.close();
		}
		else cout << "Can't open file";
	}

	string input;
	cout << "Would you like to enter another person to the database? Yes or No" << endl;
	cin >> input;

	if (input.compare("Yes"))
	{

		cout << "Plese enter ID:" << endl;
		getline(cin, input);
		ofstream t1("t1.txt");
		t1 << input << endl;
		cout << "Please enter name: Lastname,Firstname" << endl;
		getline(cin, input);
		//ofstream t1("t1.txt");
		t1 << input << endl;
		cout << "Please enter address: " << endl;
		getline(cin, input);
		//ofstream t1("t1.txt");
		t1 << input << endl;

	}

	else
	{
		cout << "End user input\n";
	}


	return 0;
}
What kind of trouble? Can you compile? If no, post the errors.

the.Aceix
Last edited on

What kind of trouble? Can you compile? If no, post the errors.

the.Aceix


it was a simple fix, however the last part of adding more people to the database is completely skipped, and i cant seem to understand why
You should empty the responsible buffer to get rid of any stray newline character.
Use std::cin.ignore(); to simply 'skip' newline characters. Insert iit just before asking the user for input.


the.Aceix
Line 40 is wrong. string::compare() returns non-zero if the strings are NOT equal. It returns zero if the strings are equal.
Topic archived. No new replies allowed.