while loop

Hi i am trying to write a program that will ask a user to enter number between 1 and 20. If number is greater than 20 the first functione which is inputInformation must loop untill the number between 1 and 20 is entered. I've tried to come up with this program.

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
#include <iostream>
using namespace std;

int funcInputFormation(int nr)
{
if (nr < 20)
cout << "The number of room is  "<<nr << endl;
while (nr >= 20)
{
cout << nr << " NOT PERMISSIBLE "<< endl;
}}

int funcNumberForBreakfast(int nrDouble)
{
if (nrDouble > 1)
nrDouble *=2;
cout << "Poeple who qualify for breakfast "<< nrDouble << endl;
}

int funcIncomeOneNight(int nrSingle)
{
if (nrSingle > 1) 
nrSingle *= 250;
cout << "Amount for single R "<< nrSingle <<endl;
}

int main()
{
int nrSingle, nrDouble, nr;

cout << "Enter number of Singles "<< endl;
cin >> nrSingle;
cout << "Ente number of Double "<< endl;
cin >> nrDouble;
nr = nrSingle + nrDouble;

funcInputFormation(nr);
funcNumberForBreakfast(nrDouble);
funcIncomeOneNight(nrSingle);

return 0;
}



You are using the while loop incorrectly; what it will do is, if the number is >= 20, it infinitely loop saying "# not permissible. I do not think this is what you were going for. You need to include the asking for the number in the loop otherwise the user will never have a chance to input a "correct" number. For example:

1
2
3
4
5
6
7
8
9
10
11
bool ii =  false;
int someint = -1;
while(!ii) {
    cout<<"Please enter a number: ";
    cin>>someint;
    if(someint <= 20 && >= 1) ii = true;
    else {
        cout<<"That number is not between one and 20."<<endl;
        ii = false;
    }
}
You could also do something like this. It won't continue until a number between 1 and 20 is entered.

1
2
3
4
5
int number;
do{
       cout << "Please enter a number: ";
       cin >> number;
}while(number < 1 || number > 20);

Last edited on
Another problem is that your funcInputFormation() function doesn't return the number that was entered.

However, when dealing with user input in C++ using streams, it is almost always best to read the input as a string and convert it to integral format. The reason being that mikeb's example code goes into an infinite loop if the user enters anything other than a number.

Here is how I'd implement funcInputFormation():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int funcInputFormation() {
    // Exit loop if standard input is closed (CTRL-D, or EOF if redirected)
    while( cin ) {
        string answer;
        cout << "Enter a number between 1 and 20: " << flush;
        cin >> answer;
        
        // Sorry, I'm going to use an advanced way to convert the number
        // to make the example shorter.        
        try {
           int result = boost::lexical_cast<int>( answer );
           if( result < 1 || result > 20 ) 
               cerr << "Invalid number entered.  Try again." << endl;
           else   
               return result;
        } catch( ... ) {
            // If the input was not integral, output error and try again.
            cerr << "Invalid input.  Try again." << endl;
    }

    cerr << "End of input reached!" << endl;
    exit( 0 );        // Just exit program I guess....
}


boost::lexical_cast<> is a convenient way to convert strings to integers, however you can also use functions like strtod, strtol, and atoi (though atoi can't tell you if the conversion failed, so I'd use strtod).

Lastly, in your main program, you need to capture the return code from funcInputFormation():

1
2
int answer = funcInputFormation();
// Now use answer for whatever you need it for... 


[Edit: and forget about the try {} catch {} syntax too... that is how you detect if boost::lexical_cast<> failed to convert the number.]
Last edited on
Guys thank you very much for your input i really appriciate, The function still is not coming write. The while loop is the problem in this function int funcInputFormation(int nr) jsmith easy on me man i am begginer that was deep.

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
#include <iostream>
using namespace std;

int funcInputFormation(int nr){
if (nr > 1 && nr < 20)
cout << "The number of room is  "<<nr << endl;
else
cout << nr << " NOT PERMISSIBLE "<< endl;
}

int funcNumberForBreakfast(int nrDouble)
{
if (nrDouble > 1)
nrDouble *=2;
cout << "Poeple who qualify for breakfast "<< nrDouble << endl<<endl;
}

int funcIncomeOneNight(int nrSingle)
{
if (nrSingle > 1) 
nrSingle *= 250;
cout << "Amount for single R "<< nrSingle <<endl<<endl;
}

int funcDisplayNumberAndAmount(int nrDouble)
{
nrDouble *= 2;
cout << "The number of guest for breakfast "<< nrDouble << endl << endl;
if (nrDouble > 1)
nrDouble *= 400;
cout << "The amount for specific bookings R "<< nrDouble << endl << endl;
}

int main()
{
int nrSingle, nrDouble, nr, i;
float income, totalIncome;

for (i = 1; i <= 1; i++)
{
cout << "Enter number of Singles "<< endl;
cin >> nrSingle;
cout << "Ente number of Double "<< endl;
cin >> nrDouble;
nr = nrSingle + nrDouble;

funcInputFormation(nr);
funcNumberForBreakfast(nrDouble);
funcIncomeOneNight(nrSingle);
funcDisplayNumberAndAmount(nrDouble);
}
nrSingle += nrSingle;
nrSingle *= 250;
cout << "The total for Single rooms is R "<< nrSingle << endl;

nrDouble += nrDouble;
nrDouble *= 400;
cout << "The total for Double rooms is R "<< nrDouble << endl;

totalIncome = nrSingle + nrDouble;
cout << "The total income is R "<< totalIncome << endl;

return 0;
}
Topic archived. No new replies allowed.