Error in file



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
  #include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

struct Student {
	int id;
	string name;
	string program;

};

// Function to read a Student  from a file
bool readStudent(std::istream & is, Student & st)
{
	is >> st.id;   is.ignore(100, '\n');
	getline(is, st.name);
	getline(is, st.program);
	return is;
}

// Function to display a Student
void showStudent(std::ostream & os, const Student & stud)
{
	os << "\nStudent ID :      " << stud.id;
	os << "\nStudent Name :    " << stud.name;
	os << "\nStudent Program : " << stud.program;
	os << '\n';
}

int main()
{
	ifstream data("data.txt");

	if (!data)
	{
		cout << "\nError Opening The File\n";
		cin.get();
		return 1;
	}

	// Define a vector to store the entire file contents
	std::vector<Student> students;


	// Read all Students from file into vector
	Student stud;

	while (readStudent(data, stud))
	{
		students.push_back(stud);
	}


	char choise = '1';

	while (choise == '1')
	{
		// Display all the contents of the vector
		for (size_t i = 0; i<students.size(); ++i)
			showStudent(cout, students[i]);

		cout << "\nEnter The Student ID to be deleted : ";
		int id;
		cin >> id;

		// find and delete Student from vector
		for (auto it = students.begin(); it != students.end(); ++it)
		{
			if (id == it->id)
			{
				students.erase(it);
				break;
			}
		}

		cout << "\n1 to try again : ";
		cin >> choise;

	}
}


i dont know why i am getting error in this function
1
2
3
4
5
6
7
bool readStudent(std::istream & is, Student & st)
{
	is >> st.id;   is.ignore(100, '\n');
	getline(is, st.name);
	getline(is, st.program);
	return is;
}


error
Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) e:\muhammad ali 23626\recursion\recursion\source.cpp 25 1 Recursion
Error 2 error C1903: unable to recover from previous error(s); stopping compilation e:\muhammad ali 23626\recursion\recursion\source.cpp 25 1 Recursion
3 IntelliSense: no suitable conversion function from "std::istream" to "bool" exists e:\Muhammad Ali 23626\Recursion\Recursion\Source.cpp 21 9 Recursion




i dont know why i am getting error in this function
1
2
3
4
5
6
7
bool readStudent(std::istream & is, Student & st)
{
	is >> st.id;   is.ignore(100, '\n');
	getline(is, st.name);
	getline(is, st.program);
	return is;
}


The function claims it is going to send back a bool, but you try to return it a stream.
any help how to fix it
Check the status of the stream, at least after the first input, and put it in a bool variable, which can be returned.

I'm not sure what the other two error messages are referring to.

Edit: now I'm flummoxed - my compiler actually ran your original code! (although it insisted that I deleted a student irrespective of whether I wanted it to).
Last edited on
One of the errors can be fixed by changing this line
 
bool readStudent(std::istream & is, Student & st)
to
 
std::istream & readStudent(std::istream & is, Student & st)


The other error according to what I can find, is caused by missing the header include for string,
 
#include <string> 
though that line is present in the above code.

http://stackoverflow.com/a/22724103
The function claims it is going to send back a bool, but you try to return it a stream.
This seems to be compiler-dependent.

It is possible to test a stream as a bool, for example
1
2
3
4
    ifstream data("data.txt");

    if (!data)
    {

and some compilers can similarly convert implicitly from stream to bool. I'm not sure what the ISO C++ standard says on this.

An explicit conversion should work:
 
    return bool(is);

http://www.cplusplus.com/reference/ios/ios/operator_bool/
Last edited on
I knew this code looked familiar from somewhere (I wrote it - for better or for worse):
http://www.cplusplus.com/forum/beginner/203361/#msg966505
@Chervil: you know you have arrived when people start quoting your stuff back to you ;)
std::basic_ios::operator bool is an explicit conversion function: explicit operator bool() const;

A stream object is implicitly convertible to bool only when it is used in direct initialisation (or in a boolean context).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
struct A { int i ; operator bool() const { return i > 0 ; } };

struct B { int i ; explicit operator bool() const { return i > 0 ; } };

bool foo( A a, B b )
{
    bool x = a ;

    bool y{b} ; // fine: direct initialisation, conversion to bool

    bool z = b ; // *** error: copy initialisation, no implicit conversion to bool

    if(a) { /* ... */ }

    if(b) { /* ... */ } // fine; boolean context

    if( x && y && z && a && b ) // fine; boolean context
        return a ; // fine: implicit conversion to bool

    else return b ; // *** error: no implicit conversion to bool
}

http://coliru.stacked-crooked.com/a/4ed19d0da1954309
http://rextester.com/COKMO32804
I'm not sure what the ISO C++ standard says on this.


The problem might not be an actual compile issue rather:
3 IntelliSense: no suitable conversion function from "std::istream" to "bool" exists e:\Muhammad Ali 23626\Recursion\Recursion\Source.cpp 21 9 Recursion

You really shouldn't rely on IntelliSense "errors" always compile your code and worry about the actual compile errors.

An explicit conversion should work:

But if you're going to cast the value make it obvious.
return static_cast<bool>(is);

Really the function probably should be returning the stream instance not a bool but the implicit conversion probably should work as well.
Thanks for the comments / information.
thnx guys can i do this same problem without using vectors
can i do this same problem without using vectors

Depends how you mean that. There are alternatives to vectors, such as dynamically allocated arrays.

But if you mean, to delete a record from the file itself, without storing the entire contents, then the logic changes.

One file in, one file out. Write each record from input to output - except for the matched id, for which the record is not written to the output.
Now i am understanding thanks @chervil
Topic archived. No new replies allowed.