0

code not working after first loop for entering name but getting input and output for rollno. i think it's a problem with the cin.getline part. any help would be appreciated.

#include<iostream>
#include<fstream>
using namespace std;

class lmao {
    char name[50];
    char rollno;

public:
    void display(void);
    void input(void);
};

void lmao::input(){
    cout<<"Enter name: \n";
    cin.getline(name,50);
    cout<<"Enter roll no.: \n";
    cin>>rollno;
    cout<<"\n";
}

void lmao::display(){
    cout<<"The name is: "<<name<<"\n";
    cout<<"The roll no. is: "<<rollno<<"\n";
}

int main(){
    lmao lol[3];

    ofstream fout;
    fout.open("cuz.txt",ios::in);
    for(int i=0;i<3;i++){
        lol[i].input();
        fout.write((char*)&lol[i],sizeof(lol[i]));
    }
    fout.seekp(0);
    fout.close();

    cout<<"Contents are: \n";

    ifstream fin;
    fin.open("cuz.txt", ios::in);
    for(int i=0;i<3;i++){
        fin.read((char*)&lol[i],sizeof(lol[i]));
        lol[i].display();
    }
    fin.close();
    return 0;
}
alter igel
  • 5,696
  • 3
  • 16
  • 38
That Guy
  • 73
  • 7
  • 1
    I'm *guessing* that the problem is the *usual* problem with mixing `std::getline` and formatted input: The formatted input leaves the newline in the input buffer, and the next call to `std::getline` reads that as an empty line. To be sure you should [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Nov 07 '18 at 04:56
  • Do you really need `lmao lol[3]` and you can use `ofstream` as you would use `cout` – macroland Nov 07 '18 at 04:56
  • @Someprogrammerdude so should i change cin.getline? – That Guy Nov 07 '18 at 05:01
  • @macroland i do need lol[3] to take 3 inputs from the user and fstream let's me use ofstream as well – That Guy Nov 07 '18 at 05:02
  • @ThatGuy Actually, you need only one `lmao` object; you don't ever use the contents of the three objects in parallel, so you can reuse the single instance in further loop runs. – Aconcagua Nov 07 '18 at 05:58
  • @Aconcagua i need to input details of 3 customers so i would need 3 objects right? – That Guy Nov 07 '18 at 06:07
  • @ThatGuy No, you don't... You save away to file the data of one customer within your loop. As soon as this is done, the data in your customer object is redundant/obsolete and you can safely overwrite it with the data of the next customer. – Aconcagua Nov 07 '18 at 06:16
  • By the way: Why do you open an *output* stream in `ios::in` mode? Actually, you can entirely leave out this second argument (for ofstream *and* ifstream), the parameter already has an appropriate default value. – Aconcagua Nov 07 '18 at 06:19
  • @Aconcagua okay, thanks a lot! also should i get rid of the for loop then? – That Guy Nov 07 '18 at 06:25
  • You still need the loop. But inside it you only need one object. – Some programmer dude Nov 07 '18 at 06:44
  • As for the `getline` issue, it really depends on if the users should be allowed to enter multiple names separated by space. I recommend that you read about [the input stream `ignore` function](http://en.cppreference.com/w/cpp/io/basic_istream/ignore) which can help you. – Some programmer dude Nov 07 '18 at 06:45
  • @Someprogrammerdude what if i use string instead? – That Guy Nov 07 '18 at 07:55
  • You should do that anyway, but it won't solve you issue. It will also make your current file-handling invalid. – Some programmer dude Nov 07 '18 at 07:58
  • @Someprogrammerdude oh alright, thanks alot! – That Guy Nov 07 '18 at 08:03

0 Answers0