Problem in fileheadling

How To Get Error message when no user is found not vector etc...

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  #include<iostream>
#include<string>
#include<fstream>
#include<stdlib.h>
using namespace std;

void checks();
void display(int);

int main()
{
	/*string name, program, semester;
	int id;
	ofstream data;
	data.open("open.txt", ios::in | ios::out | ios::app);
	cout << "\nEnter Id : ";
	cin >> id;
	cout << "\nEnter Id : ";
	cin.ignore();
	getline(cin, name);
	cout << "\nEnter Id : ";
	cin >> program;
	cout << "\nEnter Id : ";
	cin >> semester;

	data << id << endl << name << endl << program << endl << semester << endl;
*/
	checks();
	system("pause");
}


void checks()
{
	string name, program, semester;
	int id;
	bool flag = false;
	int choise = 0;
	ifstream data;
	data.open("open.txt");
		cout << "\nEnter The Id : ";
		cin >> choise;
		while (data >> id && data.ignore(30, '\n'),
			getline(data, name) && data >> program >> semester)
		{
			if (choise == id)
			{
				display(choise);
				break;
			}
			else
			{
				flag = true;
			}
		}

	if (flag == true)
	{
		cout << "\nInvalid id is input\n";
		cout << "\n1-Try Again";
		cin >> choise;
		if (choise == 1)
			checks();
		else
			exit(1);
	}
}


void display(int a)
{
	string name, program, semester;
	int id;
	ifstream data;
	data.open("open.txt");

	while (data >> id && data.ignore(30, '\n'), getline(data, name) && data >> program >> semester)
	{
		if (a == id)
		{
			cout << id << endl << endl;
			cout << name << endl << endl;
			cout << program << endl << endl;
			cout << semester << endl << endl;
		}
	}
}


text file data


12345
Muhammad Salman
BS(CS)
1st
12344
Hamza Rizwan
BS(SE)
2nd


if user enter 12345
i get this
 
12345
Muhammad Salman
BS(CS)
1st

invalid id is input // how to get rid  of this msg



if user enter invalid id this progam work fine




You set the flag = true as an else statement.
Consider how your programs flow reaches this point.
Hint:look at your while loop and your syntax
Topic archived. No new replies allowed.