H E L P !! how to make a Question & Answer using STRUCTURE

//" this is a example using INFO. of persons "



#include<iostream>
#include<fstream>
using namespace std;


struct inf
{
string fname;
string mname;
string lname;
int age;

void display()
{
cout<<"\n\n\nFirst Name: "<<fname;
cout<<"\nMidle Name: "<<mname;
cout<<"\nLast Name: "<<lname;
cout<<"\nAge: "<<age;
}
};

main()
{
ofstream out;
ifstream in;

int x,y,sum;

out.open("C:\\MyPro\\Output Sample File.txt",ios::app);
in.open("C:\\MyPro\\MyInFile.txt",ios::in);

if(out.is_open())
cout<<"\n\nOutput File is now open for any transaction:";

if(in.is_open())
cout<<"\n\nInput File is now open for any transaction:";
else
cout<<"\n\nFailed to open your input file";

inf *person;

person = new inf[3];

for(int i=0;i<3;i++)
in>>person[i].fname>>person[i].mname>>person[i].lname>>person[i].age;

for(int j=0;j<3;j++)
person[j].display();

sum = x+y;
out<<x<< " + "<<y<<" = "<<sum<<endl;

in.close();
out.close();
cout<<"\n\n";
system("PAUSE");
return 0;
}
what's the problem?
Other than the fact x and y are never given a value, I don't see a problem.
You're Variables have no values assigned to them. By the title are you asking how to ask for a question and answer with i/o files using structs?
thats only example for the create a structure of information of the person ... but my problems is how to form that or to program compose a question and answer of the quest using structure ..

example and also this is the output ..




1.) how many feet of the dog ?

answer : 4 ..

What about something like this?

1
2
3
4
5
6
#include <string> 

struct QandA
{  string question; 
    string answer;
};


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.

help me to program it guys pls ... i have a little idea.. but not enough :(
closed account (28poGNh0)
You really need to explain more

however,I assumed that your Output Sample File.txt is empty for now and your MyInFile.txt file containes these data

Chandler meyrial bing 30
ross something green 31
joy actor turbiani 32
1.) How many feet of the dog?
2.) How many feet of the check?
3.) How many feet of the snake?


The program load the whole name also the age of three know people ,then load a question ,ask it to the used ,the both quesion and answer are saved in the Output Sample File.txt file

Hope that's what you're looking for

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# include <iostream>
# include <fstream>
# include <ctime>
# include <cstdlib>
using namespace std;

struct inf
{
    string fname;
    string mname;
    string lname;
    int age;

    void display()
    {
        cout << "\n\n\nFirst Name: " << fname;
        cout << "\nMidle Name: " << mname;
        cout << "\nLast Name: " << lname;
        cout << "\nAge: " << age;
    }
};

int main()
{
    ofstream out;
    ifstream in;

    int answer;

    out.open("Output Sample File.txt",ios::app);
    in.open("MyInFile.txt");

    if(out.is_open()&&in.is_open())
    {
        cout << "\n\nOutput File is now open for any transaction:";
        cout << "\n\nInput File is now open for any transaction:";

        inf *person;

        person = new inf[3];
        string question[3];

        for(int i=0;i<3;i++)
            in >> person[i].fname >> person[i].mname >> person[i].lname >> person[i].age;

        getline(in,question[0]);

        for(int i=0;i<3;i++)
            getline(in,question[i]);

        for(int j=0;j<3;j++)
            person[j].display();

        srand(time(NULL));
        int randNbr = rand()%3;

        cout << endl << endl << question[randNbr] << endl;
        cin >> answer;
        out << question[randNbr] << endl << "answer : " << answer << endl;

        in.close();
        out.close();
        cout << "\n\n";
    }
    else cout << "\n\nFailed to open your input file";

    system("PAUSE");

    return 0;
}


Hope that helps
my professor give me some tips .. the output it should be show all the answer after the quiz then, it will calculate the score of the quiz if he/she pass the quiz


this is the code that my prof. gave..







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<fstream>

using namespace std;

main()
{
   ifstream in;
   ofstream out;
   
   string q,a,b,c,d;
   char ans;
   
   
   in.open("Quiz.txt");
   
   getline(in,q);
   in>>a>>b>>c>>d;
   cout<<"1.) "<<q<<" ?"<<"\n\na.) "<<a<<"\tb.) "<<b<<"\nc.) "<<c<<"\td.) "<<d;
   cout<<"\n\nAnswer: ";
   cin>>ans;
   
   
   
      
      
      
      cout<<"\n\n";
      system("PAUSE");
      return 0;
}




  ==>This is the input of the Quest and Answer


What is the name of American President?
A. Barak
B. Gloria
C. Ninoy
D. Erap





1
2
3
4
5
6
7
8
9
10
 This is the output

  1.)What is the name of American President?

  A. Barak             B. Gloria
  C. Ninoy             D. Erap

  Answer: __

 


Your professor showed you how to read the question and possible answers from the file. You also need to read the correct choice from the file and compare that choice to the users answer, then keep counters for the number of questions and the number of correct answers. Then you need to loop until you reach end of file.
Upon reaching end of file, you need to calculate and display the score.

You need to attempt to write the code yourself. We will help with questions, but we're not going to write it for you.

hello..,can you give me a sample of the program?or can you help me loop the questions? i tried to loop it keep repeating the question..,please help me
i tried to loop it keep repeating the question..

Sounds like you might be opening and closing the file inside your loop.
You need to open the input file before the loop and close the input file after the loop.

A separate function to read the question, possible answers and correct answer might simplify things.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct QandA; 
{  string q;
    string a,b,c,d;
    char correct_ans; 
};

bool getQandA (ifstream & in, QandA & qa)
{  getline (in, qa.q);
    if (in.eof()) 
      return false;  // no more questions
   // read a,b,c,d
   // read correct answer
  return true;  // Successfully got a question and answers
}


Now in main, you can simply loop on getQandA()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
  QandA  qa; 

  //  Open input
  while (getQandA(in, qa))
  {  // got good information in the struct 
      // prompt the user
      // get the answer from the user
      //  compare the user's answer to the correct answer
      // count if right or wrong
      // write the result to the output file 
    } // end of loop
    //  calculate and display the score
    // close the files  

Last edited on
Topic archived. No new replies allowed.