I am new to streams and files in C++.
I am having a problem in this code. When i run the program, and after entering the password, a message is displayed "error opening infile".
Why the program is not reading from the file or opening it?
#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>
#include<conio.h>
#include<ctype.h>
using namespace std;
class quiz
{
private:
int i;
char buffer[5][1000];
char new_Q[1000];
ifstream infile;
ofstream outfile;
int arr1[5], arr2[5];
int ans;
public:
void insertnewQ() //to get new data from user and insert in the array, and update back to file.
{
i=0;
cout<<"Enter new question: ";
for (i=0; i < 5; i++)
{
if (strcmp ((const char *)buffer[i], (const char *)"")==0)
{
cin.getline(buffer[i], 1000, '$');
cout<<"\nalso enter its correct option";
cin>>arr1[i];
break; //get out of for loop
}
}
storeQ(); //update questions back to file.
}
void loadQ() //load questions from file into array
{
infile.open("quiz.txt", ifstream::in);
if (!infile) cout <<"error opening infile";
i=0;
while (i < 5) strcpy (buffer[i++], (const char * )""); //clear array of existing entries
i=0;
while( !infile.eof() && i < 5)
infile.getline(buffer[i++], 1000);
cout<<"\nArray populated with questions loaded from file \n ";
infile.close();
}
void storeQ() //update questions from array back to file.
{
outfile.open ("quiz.txt", ios::trunc);
if (!outfile) cout <<"error opening outfile";
for ( i=0 ; i < 5; i ++)
{
if (strcmp ((const char *) buffer[i], (const char *) "") != 0 ) //record not empty
{
outfile<<buffer[i]<<"\n"; //write if record is not empty
//cout <<buffer[i]<<"yeh hai";
}
}
outfile.close();
}
void listQ() //to show questions stored in the array.
{
for (i=0; i < 5; i++)
{
if (strcmp ((const char *)buffer[i], (const char *)"")!=0)
cout<<buffer[i]<< "\n";
cin>>ans;
}
for (i=0; i<5; i++)
arr2[i]=ans;
cal_marks();
}
void delQ(int item)
{
strcpy(buffer[item], (const char *)"");
}
void delQ(bool val)
{
if (val == true)
{
for (i=0; i<5; i++)
strcpy(buffer[i], (const char *) 0);
}
}
quiz()
{
i=0;
while (i < 5) strcpy (buffer[i++], (const char * )""); //initiaze with null 0
if (strcmp ((const char*) buffer[4], (const char*) "") == 0)
cout<<" ";
}
void cal_marks()
{
int counter=0;
for (int i=0; i<5; i++)
if (arr1[i]==arr2[i])
counter++;
cout<<"\nYour marks are: "<<counter<<"/"<<"5";
}
};
int main()
{
int ch, choice, choice2, i=0, item;
char *tempPassword = new char;
quiz gk;
top:
cout<<"\n\"Welcome to the General Knowledge based Quiz\"\n\n";
cout<<"\nEnter your choice: \n";
cout<<"\n1. Administrator"
<<"\n2. User\n\n";
cin>>choice;
if (choice==1)
{
cout << "enter your password: ";
char ch = _getch();
int j = 0;
while (ch != '\r')
{
if (ch == '\b')
{
if (j == 0)
ch = _getch();
else
{
cout <<"\b \b";
j--;
tempPassword--;
ch = _getch();
}
}
else
{
cout <<"*";
*tempPassword = ch;
ch = _getch();
tempPassword++;
j++;
}
}
*tempPassword = '\0';
for (; j>0; j--)
tempPassword--;
cout << endl;
if (strcmp(tempPassword, "afifa")==0)
{
cout<<"\n\nWelcome to Administrator Mode\n";
cout<<"\n\tMenu\n";
gk.loadQ(); //initial loading from file.
back:
cout<<"\n1. Insert new question"
<<"\n2. List all questions"
<<"\n3. Delete a question"
<<"\n4. Delete all questions"
<<"\n5. Press 5 to exit\n";
cin>>choice2;
if (choice2==1)
gk.insertnewQ(); //insert a new questions
else if (choice2==2)
gk.listQ(); //list all questions
else if (choice2==3)
{
cout<<"\nWhich question do you want to delete: (e.g. 1 etc.)";
cin>>item;
gk.delQ(item);
} //function to delete one question
else if (choice2 ==4)
gk.delQ(true);
else if (choice2==5)
goto top;
}
goto back;
}
else if(choice==2)
{
cout<<"\nWelcome to the User Side ";
cout<<"\nAnswer the questions displayed below: ";
//gk.loadQ();
gk.listQ();
}
else
{
cout<<"\nInvalid Choice entered";
cout<<"\nPress any key to continue... ";
}
cin.get();
cin.get();
return 0;
}