Hi everyone:
I'm starting to learn C++ and I'm creating a program to mix up the questions on the multiple choice exams that I give my students.
The part of the program that mixes up the questions is working just fine, but what really bugs me is creating the strings.
Here's the code for all the input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
cout << "Enter each question one by one. Type * and Enter to go to the next question.\n";
//loop to get every question.
for ( i = 0; i < no_questions; i++ ) {
cout << "Enter question number " << i+1 << " :\n";
for ( loop1 = 0; loop1 < 25; loop1++ ) {
getline( cin, temp_string[loop1] );
//check to finish question.
test_string = temp_string[loop1];
if ( test_string[0] = '*' )
break;
}
for ( loop2 = 1; loop2 < loop1; loop2++ )
temp_string[0] = temp_string[0] + "\n" + temp_string[loop2];
questions_str[i] = temp_string[0];
}
Somehow, it seems to completely ignore the third for loop at the bottom. Deleting it doesn't change how the program functions at all.
At the moment, what happens is that when I hit return, it goes to the next question instead of adding a line break.
Thank you!
Anyways, it works now. I'm sort of pissed off that my problem was so simple: I'm usually pretty good at proofreading any typos.
Next step: Edit the word document with the program without having to copy and paste the questions!! :D
No it just outputs them in the console in a random order.
The book that I'm using to learn C++ is explaining file-stream objects and other stuff next, so I'll probably update my program sooner or later.
Here's the whole program if you might be interested :X.
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
usingnamespace std;
int main() {
srand(time(0));
int random, i, yes_no_generate, no_questions, loop1, loop2;
cout << "\nEnter number of questions : ";
cin >> no_questions;
cin.ignore();
string questions_str[no_questions], temp_string[24], test_string;
int questions[no_questions];
cout << "Enter each question one by one. Type * and Enter to go to the next question.\n";
//loop to get every question.
for ( i = 0; i < no_questions; i++ ) {
cout << "Enter question number " << i+1 << " :\n";
for ( loop1 = 0; loop1 < 25; loop1++ ) {
getline( cin, temp_string[loop1] );
//check to finish question.
test_string = temp_string[loop1];
if ( test_string[0] == '*' )
break;
}
for ( loop2 = 1; loop2 < loop1; loop2++ )
temp_string[0] = temp_string[0] + "\n" + temp_string[loop2];
questions_str[i] = temp_string[0];
}
//generate random series of questions
while (1) {
for (i = 0; i<no_questions; i++)
questions[i] = 0;
cout << "\nDo you wish to generate a random series of questions? Press 1 for yes, 0 to exit. :";
cin >> yes_no_generate;
cin.ignore();
if ( yes_no_generate == 0 )
break;
else {
for ( i=0; i<no_questions; i++) {
random = rand() % no_questions;
while (1) {
if (questions[random] == 0)
break;
else {
random--;
if (random == -1)
random = no_questions-1;
}
}
questions[random] = 1;
cout << questions_str[random] << "\n";
}
}
}
}
It looks like that will always print them in them same order that they are input, to me. Did you test this program?
Here's my concern: starting at line 42, i = 0, random = 0, questions[0] == 0 so it breaks, questions[0] = 1, and question_str[0] is output. The first question is the first question output. Continuing from line 42 again, i = 1, random = 0 or 1, then it either breaks if random == 1 or it flips random around until it is 1, then questions[1] = 1, and question_str[1] is output. The second question is the second question output; and so on.
Anyway, the code that I posted before would go inside the for loop. The string( "\n" ) is there to make sure that it compiles for you; I didn't try it. I was explicitly making the string literal (c-style string) an STL string, so that it would use string.operator+. It might not be necessary, but I didn't want to post something that might not work.
Yeah, the program's working perfectly now.
What makes you think that random is 0? I wrote random = rand() % no_questions;
in which no_questions is the number of questions. I also have an integer array called questions, with a number of elements equal to the number of questions.
When it chooses a random number from 0 to the number of questions minus 1, it checks whether that element within the array is set to 0 or 1. If it is set to 0, it then outputs that specific string (questions_str[random]).