#include <iostream>
#include <fstream>
#include <string>
#include <vector>
usingnamespace 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
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).
struct A { int i ; operatorbool() const { return i > 0 ; } };
struct B { int i ; explicitoperatorbool() 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
elsereturn b ; // *** error: no implicit conversion to bool
}
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. returnstatic_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.