Help with my looping function

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "stdafx.h"

#include <iostream>

#include <string>

using namespace std;

int main()
{
	int x;
	unsigned short int y; 
	unsigned long int z;
	signed short int a;
	signed long int b;
	unsigned short int age;
	int name;
	string mystr;
	int i;
	int pattern;

	cout << " What is the following pattern?"<<endl;

	y = 2;
	z = 4;
	a = 6;
	b = 8;
	i = 10;
	pattern = 2;
	
	cout << " Here is y's input: ";

	cout << y << endl;    // Prints y
	
	cout << " Here is z's input: ";

    cout << z << endl;    // Prints z
	
	cout << " Here is a's input: ";

	cout << a << endl;    // Prints a
	
	cout << " Here is b's input: ";

	cout << b << endl;    // Prints b
	cout << endl;

	cout << " Here is i's input" << endl;

	cout << " i = ?" << endl;

	cout << "What is the pattern?: ";
	cin >> pattern;

	if
		(pattern == 2){
			cout << " Good Job, It is 2" << endl;}
	else
		cout << " Wrong Restart the program you idiot." << endl << endl; 


	cout << " Oh! I don't know i's input" << endl;


	cout << " If the pattern countines What will the variable i =  ";
	
	cin >> i;
	
	if ( i == 10)
	{
		cout << " That is correct" << endl; 
		cin.ignore();
	
	
	}else
	
		cout << " Incorrect Try again"<< endl;
	cin.ignore();

	return i;
    
	
		cin.ignore();
        return 0;
}


I am new at this and just started practicing with looping functions. I am wondering how if they get the answer incorrect it will loop you back to the question. (ex. What is the pattern?:3 but (correct answer is 2)) The answer is incorrect hit enter to try again? How do I do that. Also i get the answer wrong it still goes to the next question. and If I get the second question wrong it closes super fast but If I get it right it waits until a key is pushed. Please help with this code.
Last edited on
please use [ code] [ /code] tags around your code so it is more legible.

1
2
3
4
5
6
7
8
9
if (pattern == 2)
  cout << " Good Job, It is 2" << endl;
 else
{
 cout << " Wrong Restart the program you idiot." << endl << endl; 
 cout << " Oh! I don't know i's input" << endl;
 cout << " If the pattern countines What will the variable i = ";
 cin >> i;
}


You need to group the four statements following the else in a block, otherwise you're compiler reads the program like this.

Start IF
if pattern == 2, carry out the statement,
End IF
Start Else
else, say "Wrong restart the program you idiot".
End Else

say "Oh I don't know i's input"
say "If the pattern conitnues.."
await user input for i.

Can you see why this is a problem? It will always output the next few lines if they are not blocked within the else.

The reason it still goes to the next question is because you don't use a while loop(to check for a condition, such as while(i != 10)


If you want to, take a look at this code I wrote for your program, see if you can figure out what certain things do on there own, if you have any questions, feel free to ask.
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
#include <iostream>
using namespace std;
int main()
{
    int x,y,z,a,b,i;
    y = 2;
    z = 4;
    a = 6;
    b = 8;
    i = 0;
    int pattern = 0;
    cout << "What is the following pattern?" << endl;
    cout << "y: " << y << endl;
    cout << "z: " << z << endl;
    cout << "a: " << a << endl;
    cout << "b: " << b << endl;
    cout << "i: ?" << endl;
    while (pattern != 2)
    {
        cout << "The pattern is: ";
        cin >> pattern;
        if (pattern != 2)
            cout << "Wrong, try again.\n";
        else
            break;
    }
    cout << "Good job, it is 2!" << endl;
    cout << "If the pattern continues, the variable i should equal: ";
    cin >> i;
    while (i != 10)
    {
        cout << "Incorrect, try again.\n";
        cout << "The variable i should equal: ";
        cin >> i;
    }
    cout << "Correct, good job!";
    return 0;
}
Last edited on

Thank you, sorry I didnt know how to add color code I just signed up yesterday. How do I stop the program from closing right when I hit enter on the second question? Also can you kinda explain the break function
1
2
3
4
5
6
7
8
9
while (pattern != 2)
    {
        cout << "The pattern is: ";
        cin >> pattern;
        if (pattern != 2)
            cout << "Wrong, try again.\n";
        else
            break;
    }


Now that you ask it upon looking back, the "break;" is totally unnecessary and I have no idea why i included it.

Directly from this website :

Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end. For example, we are going to stop the count down before its natural end (maybe because of an engine check failure?):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main ()
{
  int n;
  for (n=10; n>0; n--)
  {
    cout << n << ", ";
    if (n==3)
    {
      cout << "countdown aborted!";
      break;
    }
  }
  return 0;
}


Read more on control structures @ http://www.cplusplus.com/doc/tutorial/control/


Try adding a "cin.get();" statement or two directly after the "Correct, good job!"; statement so it looks something like this.

1
2
3
4
cout << "Correct, good job!";
cin.get();
cin.get();
    return 0;


It should require you to press enter once or twice before the program stops so you can view your results.
Last edited on
Hey thanks very much but can you help me with this one I cant quite grasp looping


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
#include "stdafx.h"

#include <iostream>

using namespace std;



int main()
{
	int a, b, c;
	int answer = 0;
	a = 2;
	b = 3;
	c = 4;

	// question 1

	cout << " a =: ";
	cout << a << endl;

	cout << " b=: ";
    cout << b << endl;

	cout << " c =: ";
	cout << c << endl;



	cout << " What is a + b?: ";
	cin >> a;

	while ( answer != 5);
	{
			cout << " What is a + b?: ";
	        cin >> answer;
			if ( answer !=5);
		cout << "Wrong answer try again.\n";
		          
	}
	cout << " Correct! Great job!!" << endl;
Topic archived. No new replies allowed.