loop a output for certain amout of times

I'm trying to make the code in a way so that the output "its a Yes or no question" will come out like 3 times before having a different output saying something else then resetting. I was thinking of using for(int i =0; i< 3; i++) but i don't know how or where i would enter it. I would appreciated if someone can help me out with this simple thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  int main()
{
    string moving = "yes";
    string no = "no";
    string input;

    do {
        cout << "Are you winning kreznik? " << endl;
        cin >> input;

        if (input == no) {
            cout << "Then it should be a yes if you want to win. you will win for sure if you at least have that mustard seed of a yes! " << endl;
        }
        else{
            
            cout << "Its a yes or no question kreznik... -_- " << endl;
         
        }
    } while (input != moving);

    cout << "Good Job kreznik. Keep going. you will win for sure!!! " << endl;


    return 0;
What you can do is create an int variable and assign it to 0 and increment it until the variable gets to 3 and continue on.
ex:
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

#include <iostream>
#include <string>


using namespace std;



int main()
{
    string moving = "yes";
    string no = "no";
    string input;
    int variable = 0;

    do {
        cout << "Are you winning kreznik? " << endl;
        cin >> input;
        
        for( int i =0; i < variable; i++){
            
                if(i < variable){
                    
                    if (input == no) {
                    cout << "Then it should be a yes if you want to win. you will win for sure if you at least have that mustard seed of a yes! " << endl;
                }
                else{
                    
                    cout << "Its a yes or no question kreznik... -_- " << endl;
                 
                }
            }
        }
        
    } while (input != moving);

    cout << "Good Job kreznik. Keep going. you will win for sure!!! " << endl;


    return 0;
}


that is my guess it may be something simpiler then that, i havent tested it yet but you get the idea
Last edited on
Thank you
@rezy3312,

Just curious, but did you test your program because it does not work.

You said:

What you can do is create an int variable and assign it to 0 and increment it until the variable gets to 3 and continue on.


Except that you never change the vlaue of "variable" and if you did the for loop may never end.

Hello newbieguy105,

I am not sure about the output, but consider this:
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string moving = "yes";
    string no = "no";
    string input;
    constexpr int NUM_OF_REPA{ 3 };  // <--- Added.

    do
    {
        cout << "\nAre you winning kreznik? ";
        cin >> input;

        std::cout<<'\n';
        
        for (int lc = 0; lc < NUM_OF_REPA; lc++)
        {
            if (input == no)
            {
                cout << "Then it should be a yes if you want to win. you will win for sure if you at least have that mustard seed of a yes!\n";
            }
            else
            {
                cout << "Its a yes or no question kreznik... -_- \n";
            }
        }
    } while (input != moving);

    cout << "\nGood Job kreznik. Keep going. you will win for sure!!! \n";

    return 0;
}

Not sure about the if/else in the for loop, but it does produce this output:

Are you winning kreznik? no

Then it should be a yes if you want to win. you will win for sure if you at least have that mustard seed of a yes!
Then it should be a yes if you want to win. you will win for sure if you at least have that mustard seed of a yes!
Then it should be a yes if you want to win. you will win for sure if you at least have that mustard seed of a yes!

Are you winning kreznik? yes

Its a yes or no question kreznik... -_- 
Its a yes or no question kreznik... -_- 
Its a yes or no question kreznik... -_- 

Good Job kreznik. Keep going. you will win for sure!!! 



Andy
Yeah my goal is to make it to where it would output 1 at a time with each input that isn't a yes or no response then after that set amount of times I would want it to output a different message like "Stop Doubting. YES or NO?!!"

example:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Are you winning kreznik? idk

Its a yes or no question kreznik... -_- 

Are you winning kreznik? maybe

Its a yes or no question kreznik... -_- 

Are you winning kreznik? i think

Stop Doubting. YES or NO.

Are you winning kreznik? yes

Good Job kreznik. Keep going. you will win for sure!!! 
I managed to figure it out. what do you guys think? I would also would like to know if theres anything in it that can be better and more organized in this code if you guys are willing to share some suggestions.

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
int main()
{
	string yes = "yes";
	string no = "no";
	string input;
	int repeat = 0;

	do {
		cout << "Are you moving your life britt? " << flush;
		cin >> input;

		if (input == no) {
			cout << "Then it should be a yes if you want to move your life. you will win for sure if you at least have that mustard seed of a yes! " << endl;
		}
		else if (repeat <= 3) {

			cout << "Its a yes or no question britt... -_- " << endl;
			repeat++;
		}
		else {
			cout << "Are you doubting yourself? its a simple yes or no..." << endl;
			repeat = 0;
		}


	} while (input != yes);

	cout << "Good Job britt. Keep going. you will win for sure!!! " << endl;


	return 0;
}
Hello newbieguy105,

Some of the small things I see where you could make better of the language and more so the C++11 standards.

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
int main()
{
    string yes{ "yes" };
    string no{ "no" };
    string input;
    int repeat{};

    do
    {
        cout << "Are you moving your life britt? ";  // <--- The following "cin" will "flush" the output buffer before any input is taken.
        cin >> input;

        if (input == no)
        {
            cout << "Then it should be a yes if you want to move your life. you will win for sure if you at least have that mustard seed of a yes!\n";  // <--- The extra space at the end has no use.
        }
        else if (repeat <= 3)
        {
            cout << "Its a yes or no question britt... -_-\n";
            repeat++;
        }
        else
        {
            cout << "Are you doubting yourself? its a simple yes or no...\n";
            repeat = 0;
        }


    } while (input != yes);

    cout << "Good Job britt. Keep going. you will win for sure!!!\n";  // <--- If this becomes a problem put the "endl" back.

    return 0;
}

With the newer standards and the upgrades the use of "endl" is no longer as necessary. the new line, (\n), should be enough and as the comment noted a "cout" statement followed by a "cin" will flush the output buffer before any input is taken. You can use this to your advantage.

If repeat is is initialized to (0)zero then this: else if (repeat <= 3) would be true 4 times. Not the 3 that you want. You may want to remove the (=).

Andy
Hello newbieguy105,

After some testing I had to change the contents of the do/while loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
do
{
    std::cout << "\nAre you moving your life britt? ";  // <--- The following "cin" will "flush" the bubber before any input is taken.
    std::cin >> input;

    if (input == no)
    {
        // <--- Changed. Added a "\n" so that the type does not reach the right side of the screen and wrap funny.
        std::cout << 
        "\nThen it should be a yes if you want to move your life. you will win for sure if you at\n"
        "least have that mustard seed of a yes!\n";  // <--- The extra space at the end has no use.
    }
    else if (repeat < 3 && input != yes)
    {
        std::cout << "\n  Its a yes or no question britt... -_-\n";
        repeat++;
    }
    else if (repeat < 3 && input != yes)
    {
        std::cout << "\nAre you doubting yourself? its a simple yes or no...\n";
        repeat = 0;
    }
} while (input != yes);


Andy
thanks guys
Topic archived. No new replies allowed.