codecrypt 0 Newbie Poster

Hello, I've been having a little dificulty with passing this array to my text file.

What i'm doing is writing a questionaire type quiz which involves outputting questions, receiving input, and taking each input value and listing it in a text file.

What I have works, except for writing the arrays separatly to the text file. At the moment I don't even get what I typed into the console - into my text file, it's weird... it shows up like this:

871 : b
872 : b
873 : b
ect...

anyways, this is what I've coded so far, maybe one of you could help me improve this, as i'm not to familiar with C++.

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
#include <windows.h>

using namespace std;


int main()
{
int qNum = 1;
int i = 0;
int j = 0;   
     string question [1000];
     string answer [1000];
     string startTest;
     string startTestWords = "start";
     string startTestWords2 = "Start";

     question [0] = "What is java?";
     question [1] = "Why do people use java?";
     question [2] = "What is whitespace?";
     question [3] = "What are variables used for?";
     question [4] = "What are datatypes?";
     question [5] = "Give examples of 5 datatypes.";
     question [6] = "What is an array?";
     question [7] = "Give an example of an array declaration.";
     question [8] = "What is the difference between public and private modifiers?";
     question [9] = "What are operators?";
     question [10] = "Why do we use operators?";
     question [11] = "Give examples of 12 operators in java.";
     question [12] = "How many bits are in 21 bytes?";
     question [13] = "Convert the number 250 to binary.";
     question [14] = "What is a loop?";
     question [15] = "What types of loops are there?";
     question [16] = "Give an example of 3 types of loops.";
     question [17] = "What is a class?";
     question [18] = "Why do people use classes?";
     question [19] = "How do you get declarations from different classes?";
     question [20] = "Give an example of the code required to place within a new class.";
     question [21] = "What is input?";
     question [22] = "What is output?";
     question [23] = "Give an example of a statement in java that displays output.";
     question [24] = "Give an example of a statement in java that requests input.";
     question [25] = "What does (System.currentTimeMillis();) do?";
     question [26] = "What are Objects in java programming?";
     question [27] = "How do switch cases in java work?";
     question [28] = "What is the difference between parameters and Arguments?";
     question [29] = "What does a return statement do?";


     // processes Start Here



     cout << "\t\t\t      Java Programming Quiz" << endl << endl << endl;

     cout << "                                - Instructions -" << endl << endl;
     cout << "- You will be given a series of questions." << endl;
     cout << "- You must answer them to your best potential." << endl;
     cout << "- After answering a question and hitting (Enter), there is no way to undo it." << endl;
     cout << "- Make sure you check your answer before you proceed to the next question." << endl;
     cout << "- Your Answers will be recorded to a file called Response.txt" << endl;
     cout << "- Use as much detail as possible." << endl;
     cout << "- You are (NOT) allowed to use any study reference while doing this test." << endl;
     cout << " " << endl;
     cout << " " << endl;
     cout << " " << endl;
     cout << "* Type the word (start) when you are ready to begin the test." << endl;

     cin >> startTest;


     if (startTest == startTestWords || startTest == startTestWords2) {
                   system("cls");
                   cout << "We begin in 5 seconds..." << endl;
                   Sleep(1000);
                   cout << "We begin in 4 seconds..." << endl;
                   Sleep(1000);
                   cout << "We begin in 3 seconds..." << endl;
                   Sleep(1000);
                   cout << "We begin in 2 seconds..." << endl;
                   Sleep(1000);
                   cout << "We begin in 1 seconds..." << endl;
                   Sleep(1000);


                   system("cls");
                   cin.clear();



     for(i = 0; i <= 29;i++) // PROBLEM WITHIN THIS FOR LOOP
           {

           cout << question [i-1] << endl;
           cout << "" << endl;
           getline(cin,answer [i+1]);


           ofstream file;
           file.open("Response.txt");

           for (int j = 0; j <= 29; j++)
           {
           file << qNum << ": " << answer [i]  << endl; // Tk
           lkHIS IS WHERE THE PROBLEM IS

           qNum++;
           } 

           file.close(); 
           system("cls");


           }

                   } if (startTest != startTestWords) {
                          system("cls");
                          cout << "You decided not to take the test." << endl;
                          cin.clear();
                          }






    system("pause");

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.