-4

I am trying to compare a string input to a string returned by a function while looping through a file, and if the input is equal to the returned string from the file it should print the details associated with the input string. But it keeps giving me the error message invalid use of void expression ...

#include <string>
#include <fstream>
#include <iostream>

using namespace std;

struct student { /* ... */ };

void search_course(string course)
{
    int check = 0;
    student st;
    
    ifstream file;
    file.open("student.dat", ios::binary | ios::out);
    
    if (!file) {         
        cout << "No Such file in Database...";      
        return; 
    }
    
    while (!file.eof() && check == 0) {
        file.read((char *) &st, sizeof(student));
        if (strcmp(st.return_prog(), course) == 0) {             
           st.showid_detail();
        }
    }
    
    file.close(); 
}

int main()
{
    string course = "physics";
    search_course(course);
}

The showid_detail() is a function in a class which displays information about a record as follows.... id, index, name, course, year, Hall
return_prog() is a void function that outputs a course of a student I expected the code to display all those students doing physics, but it kept telling me invalid use of void expression...

  • please fix your code formatting. Seems like you used the `"` button which is for formatting text quotes. Use `{}` for formatting code instead – 463035818_is_not_a_number Apr 25 '19 at 11:48
  • ... and please provide the definition of `student`. And a sample `student.dat`-file. – Swordfish Apr 25 '19 at 11:50
  • 1
    In future don't hide parts of the [MCVE]. The problem is literally in the class you decided to shelter us from. – Lightness Races in Orbit Apr 25 '19 at 11:50
  • [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Swordfish Apr 25 '19 at 11:51
  • 2
    If `return_prog()` returns `void`, why are you passing it to a function? Why are you comparing a `std::string` using `strcmp()`? – Peter Apr 25 '19 at 11:53
  • 2
    *`file.read`* – `istream::read()` is not meant to read C++ classes that aren't [POD](https://stackoverflow.com/a/146454/3975177)s (and since `student` most likely contains `std::string`s, `student`isn't POD). Overload a stream extraction operator (`std::istream& operator>>(std::istream &is, student s)`) instead. – Swordfish Apr 25 '19 at 11:54
  • @light Well, answering such dumb questions is questionable as well, if you know what I mean ;-) – πάντα ῥεῖ Apr 25 '19 at 12:10
  • Judge speaking, πάντα ῥεῖ, LightnessRacesinOrbit, both is dumb. \*duck 'n run\* ;) – Swordfish Apr 25 '19 at 12:11
  • 1
    @LightnessRacesinOrbit propably a commitingproblem. I edited the first version of the code maybe in that time someone else did too and then my edit came in third. If you look at the first version and then my edit youll see i only indented the code as i stated and neither removed nor added anything. – Yastanub Apr 25 '19 at 12:15
  • @Swordfish We all have our _bad hair days_. – πάντα ῥεῖ Apr 25 '19 at 12:22
  • @Yastanub Apologies, yes, that looks like an edit race condition. Curse this platform! (But it still should have been caught during review! Unless the diff is cached...) – Lightness Races in Orbit Apr 25 '19 at 12:41
  • @Peter The Use will input a string and that string will be compared with the string the return_prog() function will return and if they are the same the following code will execute –  Apr 27 '19 at 14:46

1 Answers1

3

return_prog() is a void function that outputs a course of a student

So there's your problem.

It doesn't return the student's course, it outputs it.

You're trying to use it in an expression strcmp(st.return_prog(), course) as if it had a return type & value, but it does not. strcmp will not magically examine console output generated by an operand; it observes the operand's value.

I suggest making that the case. You can then still do std::cout << st.return_prog() somewhere if you want output.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
  • >return_prog() is a void function that outputs a course of a student...actually that is waht i did inside return_prog() is cout< –  Apr 25 '19 at 12:54
  • @LawrenceDarko My point stands. It would make for a more flexible function (and would solve this problem) if you returned the string rather than hardcoding it to be sent to `cout` then returning nothing. (Though you have other problems, as described by people in the comments section for some reason!) – Lightness Races in Orbit Apr 25 '19 at 13:13
  • @Swordfish Pls can you elaborate for me well?...i copied your suggestion straight to the code buh did not work –  Apr 27 '19 at 09:47
  • @πάνταῥεῖ I changed it from void function to char function that returns program but instead i get this error [Error] invalid conversion from 'char' to 'const char*' [-fpermissive] –  Apr 27 '19 at 10:09
  • @LawrenceDarko Ýour function should return a `std::string` type. – πάντα ῥεῖ Apr 27 '19 at 10:29
  • You can't just "copy straight to the code" then immediately give up; you have to read, study and understand. (And I did say that you have other problems, that others have already explained) – Lightness Races in Orbit Apr 27 '19 at 11:34