Howdy,
I am trying to write a program that reads in a .txt file with questions and multiple choice answers. The program then needs to search the text and convert the questions into a new format and write to a new file. I don't have much so far because I am stuck. I do not understand how to read the file into something such as an array and then be able to search it so that I can reformat the questions. I need this done in order to reformat the tests for students to take in order to integrate them into WebCT. The book I purchased does not cover this unfortunately. An example of the question format is as follows.

1. A sequence of steps that must be taken to perform a task is called a(n):
a. Algorithm
b. Program
c. Binary
d. Computer
e. None of these
ANS: A

This needs to be reformatted by the program as such:

1. A sequence of steps that must be taken to perform a task is called a(n):
*a. Algorithm
b. Program
c. Binary
d. Computer
e. None of these

The * marks the correct answer now. I will greatly appreciate any help anyone can give. I don't expect anyone to write this for me but if you can show me how to do some of these things then that would be great! Thanks for any help!

int main()
{
  const int SIZE = 30;
  char input[30];
  ifstream inFile;

  cout<< "Plese make sure the test.txt file is in the same folder as the program."<<endl;
  cout<< "\nWhat is the name of the test file you wish to convert? : " ;
  cin >> input;
  inFile.open(input);
  if (!inFile) {
    cerr << "Unable to open file...";
    exit(1);
  }
std::string buf;
std::string line;
std::ifstream in(input);
while(std::getline(in,line))
buf += line;
std::cout << "Reading input file: " << buf << "\n";
char *strPtr;
strPtr = strstr(input, "1. ");
cout << strPtr <<endl;



  getch();
}
Member Avatar for iamthwee

You need to explain yourself more clearly. Your example is superficial.

Hmmm.....well, I'm not really a programming person and don't know a whole lot about it. Maybe that is why I cannot explain what I need. Basically, I just need to know how to read in the text file into an array so that I may alter it with some sort of function. The text file is full of questions with answers as such:

1. Question bla bla bla bla bla
a. bla bla bleh
b. ble ble bla
c. bla
d. bla bla ble ble blah
ANS: A

The text file then lists the answers by showing ANS: A and such at the end of each multiple choice question. This format however is bad and is not compatible with Resondus, the software I'm using to import my tests into. The Respondus program requires the questions to look like this: using an asterisk to mark the correct answer rather than the first method.

1. Question bla bla bla bla bla
*a. bla bla bleh
b. ble ble bla
c. bla
d. bla bla ble ble blah

My goal is to create a program that will convert all of my tests into this new format for the Respondus software. I need the program to read in the text file which I think I understand how to do. I do not know how to read it into the program in order to have code that will run through the text file and convert the questions to the new format and write them all to a new text file. Basically, I just need guidance showing me how to write functions to search the input text file for questions and answers and then changing them to this new format so Respondus will accept them. I'm really sorry if this doesn't help any more than my first post and I hope someone can help me understand. I don't know what else to say since my C++ knowledge is limited. Thanks again for any replies and assistance!

Basically it sounds like you want to eliminate the last line of the file but not before you place an asterix before the correct answer. Correct?

If so, then there are several possible approaches based on the exact format of the file.

If each question section in the file consists of a single question line, 5 lines of possible answers and a line with correct answer, then read in 7 lines in sequence. The last line of the sequence is the answer. Then determine which of the lines of possible answers has the first char equal to the last char of the answer line. Create a temp string consisting of an asterix and append the correct answer line to the temp string. Assign the temp string to the string with the correct answer. Erase, or overwrite, or flag the answer line so it's not written to the new file.

But what if some questions have 2 possible answers, some have 4, and some have 5? And what if the question can have more than one line? Then you have to look at each line as it's read in. The first line with text after an aswer line will always be a question line. Choice lines will always have a period as the second element of the string and the answer line is the first line after the choices or the line with the substring ANS: as the first token in the string (word in the line). Once you've figured out which line is which, the elimination of the answer line and placing the asterix before the appropriate choice line remain the same as before. Note that this is just a generalization of the first more structured file format.

What if there can be blank lines in the file? You should have enough informnation now to try to figure out how to deal with that possibilty yourself.

Do the program in small steps:
1) Start by reading each question and all the answers into 6 strings. Continue until all questions read.
2) Next step, read as above and write all but your answer to the new file.
3) Third step, after reading, look at your answer and replace the correct letter with '*' before writing.

Do the program in small steps:
1) Start by reading each question and all the answers into 6 strings. Continue until all questions read.
2) Next step, read as above and write all but your answer to the new file.
3) Third step, after reading, look at your answer and replace the correct letter with '*' before writing.

Thank all of you for all of your help!
I guess what I do not understand how to do is what you explain to do in step number 1. I don't understand how to have the program search the text file to pull out the different segments.....example:

(1. question.....end.)
a. bla
b. der
c. bluh
d. nah

How do I get the program to pull out just the section in parenthesis and place it into a string. It needs to do this for all of the questions and I don't understand that part. The book I have does not cover such a task. Again, thanks for your help and your patience. I am highly appreciative.

for things of this kind, using a scripting language like perl would be much easier than programming it in c++.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <locale>
using namespace std;

const string choice_letters = "abcde" ;
const string answer_prefix = "ANS:" ;

inline bool is_choice( const string& str )
{
  return str.find_first_of( choice_letters ) == 0 &&
         str.find_first_of( '.' ) == 1 ;
}

inline bool is_answer( const string& str )
{ return str.find( answer_prefix , 0 ) == 0 ; }

int main()
{
  ifstream fin( "q_and_a.txt" ) ;
  ofstream fout("q_and_a.new.txt") ;
  string line ;
  string question ;
  vector<string> choices ;
  const ctype<char>& ct =
      use_facet< ctype<char> >( fin.getloc() ) ;
  
  while( getline( fin, line ) )
  {
    if( is_choice(line) ) choices.push_back(line) ;
    else if( is_answer(line) )
    {
      string ans ;
      size_t i = answer_prefix.size() ;
      for( ; i<line.size() ; ++i )
        if( !ct.is( ctype_base::space, line[i] ) )
            ans += ct.tolower( line[i] ) ;
      fout << question ;
      for( size_t i=0 ; i<choices.size() ; ++i )
      {
        if( ans.find_first_of( choices[i][0] ) != string::npos )
           fout << '*' ;
        fout << choices[i] << '\n' ;
      }
      question = "" ;
      choices.clear() ;
      ans = "" ;
    }
    else question += line + '\n' ;
  }
}
commented: That wasn't help, that was just big-time spoon-feeding a complete answer. -2
commented: Oh my god...! you learn a greal of things just be reading vijayan's code snipsets! +2

Wow! Above and beyond help! Thanks a million! If that is what it really took to make this work I definitely never would have been able to do it on my own. It is obvious to me now that I really really don't know much about programming. Thank you so very much!

Great, you've got your free cookie, but have you learnt anything in the process?

> I definitely never would have been able to do it on my own.
Sure you would, eventually.
But instead you're dazed by vijayan121 'brilliance' rather than building up your knowledge towards your own solution. Which whilst it might have been longer, used less C++ features, would have been YOUR solution and not someone else's.

commented: well said +13

Yeah, I understand what you mean...I didn't really expect that anyone was basically going to do it for me. Its not too much of a big deal though because I wouldn't have eventually learned in the long run. I don't have much interest in programming. I was just doing this one thing because a friend suggested it as a solution to my problem. I bought a book and just started dabbling around until I realized it takes quite some time to learn the skill needed to complete what I was needing and this is time that I do not have. I appreciate everyones input though and I actually have learned a little bit about programming in the process!

Member Avatar for iamthwee

>I appreciate everyones input though and I actually have learned a little bit about programming in the process!

I somehow doubt this. I bet you I could pick any line in vijayan121's post and you wouldn't have a clue what it is doing?

And that is why his post was more counter-productive than helpful.

Sorry to resurect an old post... but i haven't read it until now...

i disagree with this statement:

And that is why his post was more counter-productive than helpful.

someone could categorize programmers in the following categories:

  • cat1: Those who want to learn the art of programming {or want to become software enginneers}...
  • cat2: Those who know how to program{they are software enginneers}, they have experience but still learnirning...
  • cat3: Those who are required to aquire a minimum of programming knowledge in order to complete their studies..
  • cat4: Those who become involved with programming by "accident" because they think that they can solve some of their problems

The categories are not exclusive and someone may fall to more than one... for example a student who likes programming may fall under category 1 and 4...
A student who doesn't like programming and tries to get a "free cookie" {nicely stated by salem, although rumours have it that this cookie
in the end will be really expensive} may fall under cat3....

In general the forum's policy of not giving spoon fed answers is correct because it helps everyone in the longrun...
From what Deadvacahead has written it seems that he falls under cat4 and that he begun learning c++ being motivated from his problem
in text processing... Given the circumstances i think that all answers prior to vijayan's wouldn't help someone
in cat4 {i.e. Deadvacahead}, although they would be more than helpful for someone in cat2 or cat1. In the contrary vijayan suggested a more "correct"
tool for the job{Perl}... and together he proposed a solution that helped Deadvacahead and along him helped everyone like me who would be able
to solve this problem{hopefully} but not with the "delicate" manner vijayan did...

So in the end Deadvacahead learned that if he wants to do mainly text processing he should try perl {one reason viji's post was productive}...
, people like me{amateur-noob but not a leecher} have seen a really delicate solution{second reason the post was productive} ...
and people who are even more amateurs than me can follow iamthwee's, Lerner's, WaltP's steps and also have a sample solution....

As for me i didn't write all these wasn't just to defend vijayan's decision or start a flame thread... but to stress a {sometimes}
forgotten point... In programming problems there are a lot of solutions and although all of them are correct few of them are trully "elegant". And i can't think a better way to learn to write nice code than reading code written by experienced professionals....

-nicolas

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.