For loop instead of goto

okay. I am very new to to C++ (i just started learning it a week ago). I am making a command-line Final Fantasy quiz. here is my current code for the first question and the stuff at the start.
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
#include <iostream>
using namespace std;

int main()
{
	cout << "\nJames & Sam's Final Fantasy Quiz\n\n";
	int q1, q2, q3, q4, q5, q6, q7, q8;
	int points = 0;


	cout << "Where Does FF7 take place?\n";
	cout << " 1: Spira\n 2: Midgar\n 3: Sydney\n";
q1f:
	cin >> q1;
	if(q1 != 1) if(q1 != 2) if(q1 != 3) 
	{
		cout << "Please type 1, 2, or 3\n";
		goto q1f;
	}
	if(q1 == 2) 
	{
		cout << "CORRECT!!!";
		points++;
	}
	if(q1 != 2) cout << "WRONG!!!";


as you can see, i used the goto statement. however, i would prefer to use a loop or user function. does a for loop NEED 3 parts[ for(initialization; condition; increment) ]?
i just want the condition, as what it checks changes in the loop itself.

I have heard that it is not good to get used to using goto.
1
2
3
4
5
	if(q1 != 1) if(q1 != 2) if(q1 != 3) 
	{
		cout << "Please type 1, 2, or 3\n";
		goto q1f;
	}

I think can write as this:
1
2
3
4
5
	if(q1 != 1&&q1 != 2&&q1 != 3) 
	{
		cout << "Please type 1, 2, or 3\n";
		goto q1f;
	}


And , maybe you can try Switch sentence.
a for does need those 3 parts but there a ways round it like just having a part as blank will suffice e.g for(;;;). But if you just want to check the condition a while loop will be better. They take the form:
while(condition){//do stuff} .
Goto is generally not liked as far as I know, i suspect it's because they tend to make the code flow not very clear (at least that's what i find), as they allow you to jump to pretty much anywhere in your code.
Last edited on
Thankyou very much! I applied both of these changes, and it even fixed some other errors i was having!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
	cout << "\nJames & Sam's Final Fantasy Quiz\n\n";
	int q1, q2, q3, q4, q5, q6, q7, q8;
	int points = 0;


	cout << "Where Does FF7 take place?\n";
	cout << " 1: Spira\n 2: Midgar\n 3: Sydney\n";

	while(q1 != 1&&q1 != 2&&q1 != 3)
	{
		cin >> q1;
		if(q1 != 1&&q1 != 2&&q1 != 3) cout << "Please type 1, 2, or 3\n";
	}	
	if(q1 == 2) 
	{
		cout << "CORRECT!!!";
		points++;
	}
	if(q1 != 2) cout << "WRONG!!!";

The code now goes like this. is there anything else i should change before i apply it to the rest off the questions?
It would be a good idea to move the "question asking" code into its own function to avoid massive code duplication:

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
typedef const string& csref;

int ask_question(csref question,csref c1,csref c2,csref c3,int correctAnswer)
{ //returns points scored for this question
  cout << question << "\n";
  cout << " 1: " << c1 << "\n 2: " << c2 << "\n 3: " << c3 << endl;
  int a=0;
  for (;;)
  {
    cin >> a;
    if (a<1 || a>3)cout << "Please type 1, 2, or 3" << endl;
    else break;
  }
  if (a==correctAnswer)
  {
    cout << "CORRECT!!!" << endl;
    return 1;
  }
  else
  {
    cout << "WRONG!!!" << endl;
    return 0;
  }
}

int main()
{
  cout << "\nJames & Sam's Final Fantasy Quiz\n\n";
  int points=0;
  points+=ask_question("Where Does FF7 take place?","Spira","Midgar","Sydney",2);
}
thankyou Ather. I will probably shorten the code like this eventually, but the are alot of things in there that i do not know about. i don't want to add masses of things i don't actually know how to use (for other things).

however, i did use (a<1 || a>3), which i changed to (q1<1 || q1>3). i don't know why i didn't use this before, as it is something i did know about.

also, with this check, and my original check, and bolo809's check, when i type anything other than a number (in the actual executable), the choose 123 message just continuously appears, causing me to have to exit command prompt. what i wanted was to have it so that if the user accidentally types something other than 1, 2, or 3, for it to ask again.

???
hello. I am having more problems with this. the fact that any letter I type in or anything other than a number cause an infinite loop. will changing int to char or similar fix this problem.

edit: oops I just double posted. not surposed to do that. haven't used forums in a while. sorry.
Last edited on
Topic archived. No new replies allowed.