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;
}
|