#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ofstream s1,s2;
string name1,name2;
s1.open("student1.txt",ios::out);
s2.open("student2.txt",ios::out);
cout<<"enter the name of student 1";
getline(cin,name1);
cout<<"enter the marks of student1:";
int mark1,mark2;
cin>>mark1;
s1<<name1<<mark1;
cout<<"enter the name of student 2";
getline(cin,name2);
cout<<"enter the marks of student2:";
cin>>mark2;
s2<<name2<<mark2;
s1.close();
s2.close();
return 0;
}
this code skips the input of 2nd student name.....why is this happening?and how can i fix this problem??
Don't make us guess, please. Post the error you received or explain the code's incorrect behavior and the behavior that is considered as correct. Also use code tags to post code.
My guess is it's because you close "s1" twice, but that's only a guess you should post the error you are recieving or the results you expect vs the ones you are getting.
1) You never check if the files are actually open before writing.
2) conio.h is deprecated. You don't even use it.
3) As Computergeek said, you close the same file stream twice.
4) State the errors/warnings you're receiving.
5) State the compiler you're currently using, along with your OS.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
usingnamespace std;
// get input as string and
// then convert it to int
int get_int()
{
string input;
getline(cin, input);
stringstream buffer(input);
int result;
buffer >> result;
return result;
}
int main()
{
ofstream s1, s2;
string name1, name2;
int mark1, mark2;
s1.open("student1.txt");
s2.open("student2.txt");
cout<<"enter the name of student 1: ";
getline(cin,name1);
cout<<"enter the mark of student 1: ";
mark1 = get_int();
cout<<"enter the name of student 2: ";
getline(cin,name2);
cout<<"enter the mark of student 2: ";
mark2 = get_int();
s1 << name1 << endl << mark1;
s2 << name2 << endl << mark2;
s1.close();
s2.close();
return 0;
}