Problem with strings

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!
On line 9, the expression is an assignment rather than a test of equality; try operator ==.

Additionally, those two for loops are a real eye-sore. Why not something like:
1
2
3
4
5
6
7
cout << "Enter it:" << endl;
string input;
while( input != "*" )
{
    if( input.size() ) questions_str[i] += string( "\n" ) + input;
    getline( cin, input );
}
Last edited on
Crap! I thought I had gotten every single one.


The one problem would be that if there is a star somewhere else in the question, then it'll also finish. Which I don't want.

I don't understand how your loop works though...How does the variable i increase?
And what does string( "\n" ) do?

Thanks so much for the help!
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
I'm not really sure what your program does. Does you type the questions in to your program and it writes them to a fnother file in a random order?
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.

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
#include <iostream>
#include <string>
#include <time.h>
#include <stdlib.h>
using namespace 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";
			}
			
		}
	}
}	

oh okay
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]).
Yep, I must have read over it too fast; my fault.
Topic archived. No new replies allowed.