Hello again,
Created a simple program requesting input of an integer, looping for an equivalent duration of the input, and displaying a "$" each time.
Assignment states only to request user input, which needs to be between 1-10; if not display error message and request user input again.
All fine and dandy here; however, the way I have coded it if the user inputs 0 the loop ends. If I didn't want the user to end the function at all, how could I set up this loop?
I prefer the way I created the program (even though it says nothing about terminating the loop in the assignment); but, for knowledge sake I was wondering.
I guess the question concerns looping not zero; apologies there.
Edited by di inferi - 11/3/12 at 9:44am
Created a simple program requesting input of an integer, looping for an equivalent duration of the input, and displaying a "$" each time.
Assignment states only to request user input, which needs to be between 1-10; if not display error message and request user input again.
All fine and dandy here; however, the way I have coded it if the user inputs 0 the loop ends. If I didn't want the user to end the function at all, how could I set up this loop?
I prefer the way I created the program (even though it says nothing about terminating the loop in the assignment); but, for knowledge sake I was wondering.
I guess the question concerns looping not zero; apologies there.
Code:
#include <iostream>
using namespace std;
int main ()
{
int input, // User input.
output; // Output ($).
// Request user input; validate; loop.
do
{
cout << "Enter the next number (1-10). 0 to end function." << endl;
cin >> input;
output = input;
// Output if validated.
if (input >= 1 && input <=10 )
{
for ( output = 1; output <= input; output++ )
{
cout << "$";
}
cout << endl;
}
// Check user option to end function.
else if (input == 0)
cout << "Function ended.";
// Output error if not valid input, loop.
else
cout << "Error, ";
}
while (input != 0); // Terminates function at user request.
cout << endl;
system ("pause");
return 0;
} // End function main.
Edited by di inferi - 11/3/12 at 9:44am






