#include<iostream>
#include<fstream>
using namespace std;
int main(){
char str[100],decision;
int num=0,i=0;
ifstream ifile;
do {
cout<<"please select from the following\n"
<<"Enter 1 for level 1 of Question\n"
<<"Enter 2 for level 2 Of Question\n";
cin>>num;
switch(num){
case 1:{
cout<<" For Algebra of level 1 please Enter 1\n";
cout<<"For Differentiation of level 1 Enter 2\n";
cin>>num;
switch (num){
case 1:{
ifile.open("_algebra_1.txt");
ifile.get(str,100,'\n');
cout<<str<<endl;
ifile.close();
break ;
}
case 2:{
ifile.open("_differentiation_1.txt");
ifile.get(str,100,'\n');
cout<<str;
ifile.close();
break ;
}
default : {
cout<<"Enter from the above number\n";
}
}
break ;
}
case 2:{
cout<<" For Algebra of level 2 Enter 1\n";
cout<<"For Differentiation of level 2 Enter 2 \n";
cin>>num;
switch (num){
case 1:{
ifile.open("_algebra_2.txt");
ifile.get(str,100,'\n');
cout<<str<<endl;
ifile.close();
break ;
}
case 2:{
ifile.open("_differentiation_2.txt");
ifile.get(str,100,'\n');
cout<<str;
ifile.close();
break ;
}
default : {
cout<<"Enter from the above number\n";
}
}
break ;
}
default : {
cout<<"Enter from the above numbers\n";
}
}
cout<<"\nIF YOU WANT TO PLAY AGAIN ENTER 'y'"
<<"\nOTHER WISE ENTER 'n'";
cin>>decision;
}while(decision=='y' ||decision=='Y' );
return 0;
}
Now I want to ask that how can I pick a random line from the file which will be display on a console and user will answer the Question:::Please help me
Just stick that in a loop until you have the desired line number. Just make sure to either save the number of lines at the beginning of the .txt file, or check that getline() isn't returning false during each iteration.
EDIT: getline() doesn't actually return a bool, it returns the istream which implicitly converts to NULL in a boolean context if any failure bits are set.