While(user==gullible)

I am continuing my basic C++ exercises. The main objective of this exercises is the following:

Requires:
variables, data types, and numerical operators
basic input/output
logic (if statements, switch statements)
loops (for, while, do-while)

Write a program that ccontinues to asks the user to enter any number other than 5 until the user enters the number 5.
Then tell the user "Hey! you weren't supposed to enter 5!" and exit the program.

★ Modify the program so that after 10 iterations if the user still hasn't entered 5 will tell the user "Wow, you're more patient then I am, you win." and exit.

★★ Modify the program so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.)

I finished the first part of the program, where it asks you to enter a number other than five and continues to ask until you enter 5. I am having a difficult time figuring out how to modify the program, so that if the user has not entered 5 after 10 iterations, it will tell the user "Wow, you're more patient then I am, you win." and exit. What is an iteration exactly? I assume it means how many times you perform an imput. Please look at my code and make 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
// While(user == guillible).cpp : Defines the entry point for the console application.
//

//Program that asks the user to imput a number other than 5 until the user enters the number 5
#include "stdafx.h"
#include <iostream>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	int Answer; //The number that the user imputs
	
	do{
	cout << "Please enter any number other than 5!\n";
	cin >> Answer;
	} while(Answer != 5); //Loop that keeps asking the user to enter a number until the user enters 5
	
	if(Answer == 5) //If the user enters 5, then the program says the following
	{
		cout << "Hey! you weren't supposed to enter 5!\n";
	}
	
	system("PAUSE");
	return 0;
}
Last edited on
Here is the solution 4 ur problem


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>
#include<cstdlib>

using namespace std;
int main()
{
    int input,a;
    for(a=0;a<10;a++)
    {
        cout<<"Enter your input : ";
        cin>>input;
        if(input==5)
        {   cout<<"Hey! you weren't supposed to enter 5!\n";
            break;
        }
        else if(a==9)
        {
            cout<<"Wow, you're more patient then I am, you win.\n";
            break;
        }
        
    }
    system("PAUSE");
    return 0;
} 



enjoy programming :)
prashant gupta

else if(input==9)

^_^
Sorry Bassam, but Prashant's code is correct and your modification is not. Although I would make a couple of modifications to the code so it would show the intent of programmer with code. In my opinion writing self-documenting code is a very good practice and you have to learn it right from the beginning. For example reading Prashant's code one may ask: why is it (a == 9)? Should it always be 9, or should it change with the number of iterations? In this case the question is quite obvious but if you imagine more complex code - it will not be.
So this is how this code should look in my opinion:

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
#include<iostream>
#include<cstdlib>

using namespace std;
int main()
{
    // give meaningful names to variables
    bool userHasInput5 = false;

    for(int inputAttempt = 0; inputAttempt < 10; inputAttempt++) 
    {
        cout << "Enter your input : ";
        int input; // declare the variables as close to the place you use them as possible
        cin >> input;
        if (input == 5) {   
            cout << "Hey! you weren't supposed to enter 5!\n";
            userHasInput5 = true;
            break;
        }
    }
 
    // here, we check if the user didn't input 5 - note that there's no dependency on the number of iterations
    if (!userHasInput5) {
        cout<<"Wow, you're more patient then I am, you win.\n";
    }

    system("PAUSE");
    return 0;
} 
thank you KRAkatau

i was wrong ^_^
All right! Thanks a lot for all of your guys help. I finally got my program working! Prashant, KRakatau, Bassam300 == rockstars xD
Who DARES to use system()????? I will find you and add a .dll to your compiler that will make it refuse to compile if it finds the word system outside of comments or quotes. Never think about it again, I command you!

Instead use cin.get(); and escape the clutches of batch programming.
OMG I'm so sorry I insulted you!!! xD I'm only a beginner
kaseron, please focus on the important stuff. I don't like the use of system either, using namespace std; also doesn't excite me. But this is small stuff that certainly doesn't require the amount of emotion you expressed (unless it was a joke). I mean how important do you think is system("pause"); in production code? Why doing it? If it's a command-line tool - output to a file or redirect stdout to a file. If it is not - you wouldn't need it at all.
Topic archived. No new replies allowed.