Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 random string line
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
varundesi




PostPosted: Thu Nov 02, 2006 8:21 pm   Post subject: random string line

I need to create a program that creates and administers a true/false quiz. Questions are read from a data file into one array and a related array is used to store the answers, either T or F, for each question. When the user takes the test, questions are presented in random order, without repetition. Keep track of the number of questions answered and the number of correct answers. When the user has finished the quiz, display the mark as a percentage.

I m sorry but thats all i have, plz help me out

code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
        char cAns;
        ifstream fin;
        string sLine;
        fin.open("72bIN.txt");
        while(!fin.fail())
        {
        getline (fin,sLine);
        if(fin.eof())
                        break;
        cout<<sLine<<endl;
        cin>>cAns;
        }
        fin.close();
       
        return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
varundesi




PostPosted: Thu Nov 02, 2006 8:24 pm   Post subject: (No subject)

the input file is


code:
The year is 2005.

F
These computers are really fast.

F
Computer science is the best.

T
Linux will eventually win the OS war.

T
[/code]
Andy




PostPosted: Thu Nov 02, 2006 10:32 pm   Post subject: (No subject)

create a struct containing a string for the question and a bool for the answer. allocate an array of your structure, load it from the file, then randomly sort it.
varundesi




PostPosted: Fri Nov 03, 2006 8:28 pm   Post subject: (No subject)

Andy wrote:
create a struct containing a string for the question and a bool for the answer. allocate an array of your structure, load it from the file, then randomly sort it.


can u tell me how u do that, cuz i have no clue.
Thanks
Andy




PostPosted: Sat Nov 04, 2006 4:51 am   Post subject: (No subject)

after i do your homework, can you do my mine for me? It's just a simple directx to LUA script parser?
varundesi




PostPosted: Sun Nov 05, 2006 7:47 pm   Post subject: (No subject)

Andy wrote:
after i do your homework, can you do my mine for me? It's just a simple directx to LUA script parser?


alrite alrite, fair enough, i ll try myself first.

P.S i m also in waterloo, hope to come to uw next year, later.
bugzpodder




PostPosted: Sat Nov 11, 2006 9:52 pm   Post subject: (No subject)

Andy wrote:
create a struct containing a string for the question and a bool for the answer. allocate an array of your structure, load it from the file, then randomly sort it.


Andy, that doesn't help at all.

varundesi, you are doing great. First of all, there is something wrong with the way you read the input. You have to be *very* careful when you use getline. The problem is this:

Question ...
T
Question 2 ...

Suppose you execute the following sequence of commands
getline(cin,str)
cin>>ans
getline(cin,str)
After you did cin>>ans, rembmer that there is an implicit \n (newline character after the T), so if you do getline(cin,str) after that, you will in fact read in an empty string. This is also true when you execute cin.eof()... it maybe that there is a newline left so the file isn't empty.

The correct way to parse it is:

getline(cin, question);
cin.ignore(); //ignores the blank line between the question and answer
cin>>ans;
cin.ignore(); //ignores the endline character after the answer

so you may have a vector<pair<string,bool> > questions
add: questions.push_back(make_pair(question, ans=='T'))
to store all your quizzes...
to randomly shuffle it:
random_shuffle(questions.begin(),questions.end())
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: