Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Wont call file

0 votes
asked Apr 16, 2020 by Ryan Heim (120 points)
Hello, I am having trouble with calling a file.  ifstream inFile ("index.txt"); program runs but it wont call the index.txt file

// C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <stdlib.h> /* srand, rand */
#include <iomanip>
#include <limits.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <stack>
#include <string.h>

using namespace std;

int main()
{

    int totalQuestion;
    int totalPoints = 0;
    string input, type, question, answer;
    int totalOptions;
    string c;
    int points;

    ifstream inFile ("index.txt");
    if (inFile.is_open())
    {
       inFile >> totalQuestion;
        while(true)
        {
           for (int i = 0; i < totalQuestion; ++i)
           {
              cout << "Question: "<< (i+1) << endl;
               inFile >> type >> points;

               // read type of question
               if(type == "TF")
               {
                // get the question
                inFile.ignore();
                  getline(inFile, question);
                  // get the answer
                  inFile >> answer;

                  cout << "Question: " << question << endl;
                  cout << "Input answer: ";
                  cin >> input;

                  if(input == answer)
                  {
                    cout << "Right Answer\n\n";
                    totalPoints = totalPoints + points;
                  }
                  else
                    cout << "Wrong answer\n\n";
               }

               else if(type == "MC")
               {
                  string options;
                  inFile.ignore();
                  getline(inFile,question);
                  inFile >> totalOptions;

                  cout << "Question: " << question << endl;
                  for (int j = 0; j < totalOptions; ++j)
                  {
                      getline(inFile , options);
                      cout << options << endl;
                  }

                  inFile >> answer;
                  cout << "Enter your choice(A-F): ";
                  cin >> c;

                   inFile.ignore();
                  if(c == answer)
                  {
                    cout << "Right Answer\n\n";
                    totalPoints = totalPoints + points;
                  }
                  else
                    cout << "Wrong answer\n\n";

               }

               if(inFile.eof())
                break;
           }

         
        }
        inFile.close();
    }

else cout << "Unable to open file";

cout << "Total points: " << totalPoints << endl;
  
return 0;
}

/*
index.txt
3
TF 5
There exist birds that cannot fly?
true
MC 10
Who was the President of the USA in 1991?
6
Richard Nixon
Gerald Ford
Jimmy Carter
Ronald Reagan
George Bush Sr.
Bill Clinton
E
TF 10
The city of Boston hosted the 2004 Summer Olympics?
false
*/

1 Answer

0 votes
answered Apr 19, 2020 by Manuel Mateo (1,190 points)
You never opened the file. You need to write

inFile.open("index.txt");

somewhere in your code.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...