do-while loop with if trouble

I got stuck with something I know is staring me right in the face.
The rest of the code works the way I want it to, but if have suggestion to make my code loo more professional, please. My problem is that I want my program to keep looping ONLY if either the 'y' or 'Y' is entered. I tried having the if statement on either side of the while loop and it only show the if statement once but won't loop back.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
 /*
this program will calculate and display a user's clothing size
based off the user input of height, weight and age
the output will be hat, jacket, and waist size
*/

#include <iostream>
#include <cmath>
#include <iomanip>



using namespace std;
/** hatSize will take in 2 arguments
will divide the 2 arguments and multiply them by 2.9
*/
int hatSize (int lbs, double length);

/** jacketSize will take in 3 arguments
the first two arguments will be multiplied and then divided by 288
.125 will be added if third argument is higher than variable time
*/
double jacketSize (double length, double waist, int time);


/** waist size will take in 2 arguments
the first will be divided by 5.7
than if the age is over 28
.10 will be added for every 2 years
*/
double waistSize (double lb, int time);


int main()
{
    double height;
    int age , weight;
    char userInput;

do
{



   cout << "_____________Welcome______To______Your_______True________Fit________\n\n\n";
   cout << " How tall are you in inches??" << endl;
   cin >> height;
   cout << " what is your current weight?? no cheating" << endl;
   cin >> weight;
   cout << " How many years young are you??" << endl;
   cin >> age;

    cout << " \n\n\n You should look for a size  " << hatSize(weight, height)
         << " in hats." << endl ;
    cout << fixed << setprecision(2);
    cout << " Your jacket should give your chest at least "
         << jacketSize (height, weight, age) << " of inches around. " << endl;
    cout << " Based off your weight, you should have  "
         << waistSize(weight, age) << " inches around your waist " << endl;

    cout << " Would you or anyone else like to find their true fit?? (y/n) " << endl;
    cin >> userInput;


        if (userInput != 'y' ||  userInput != 'Y') // to have the user try again
            {
                cout << " Awww well me know if you change your mind. (y/n)";



            }


}while (userInput == 'y' || userInput == 'Y' ); // loop until the user ends the loop




    return 0;
}


/** waist size will take in 2 arguments
the first will be divided by 5.7
than if the age is over 28
.10 will be added for every 2 years
*/
double waistSize (double lb, int time)
{

    double waist;
    waist = lb / 5.7;
    if (time > 28)
    {
        for (int i = 0; i <= time; i += 2)
        {
            waist += .10;
        }
    }

    return waist;
}


// receiving height and weight to give size
// time is from the age variable
double jacketSize (double length, double lb, int time)
{
    double coat;

    coat = (length * lb) / 288;
        if ( time > 30)        // if the person is older than 30
        {
            for (int i = 0; i <= time; i += 10) // adding .125 to coat for every 10 years
            {
                coat += .125;
            }

        }

        return coat;

}

//
int hatSize (int lbs, double length)
{
    double headSize;

    headSize = (lbs / length) * 2.9;


    return headSize;
}
You need to use a case statement.
Example:

1
2
3
4
5
6
7
8
9
10
11
12
char userinput;
input = _getch();
switch(char(userinput))
{
    case 'y':
    case 'Y':
        // code.

    case 'n':
    case 'N':
        // code.
}
Last edited on
What is your if statement for? The sentence inside it doesn't make sense so I'm not exactly sure what you're trying to do with it. If you remove the if statement and its contents the program runs fine and will only run again if y or Y is entered. Are trying to test for proper input or give a goodbye message or ask them if they're sure they want to leave? What is the if statement for?
What I'm trying to do is make sure the user inputs the correct letter of 'y' or 'Y' to loop the program. If any other input is made whether it be a number, symbol, letter that it will display an error message or goodbye message.
Your if() statement is really not necessary since you have the same conditions in the while() statement.


However you're probably having problems with the input for userinput. When retrieving a single char from the input stream whitespace characters are not skipped by default. In this case you have an unwanted character in the input buffer when you get to the input for userinput (the end of line character) which must be handled before you try to input the value for userinput.

Look at this snippet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
    int age , weight;
    char userInput;

do
{
...
   cout << " How many years young are you??" << endl;
   cin >> age;   /////////////////// This leaves the end of line character in the input buffer.
...
    cout << " Would you or anyone else like to find their true fit?? (y/n) " << endl;
    cin >> userInput;  ////////////// This will use the end of line character as the value for userinput.
...


Your can use istream.ignore() to ignore this character or you can consume the whitespace with the stream.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
    cin >> age;
    cin.ignore();
...
    cin >> userInput;
...

//// OR 

...
    cin >> age;
...

    cin >> ws >> userInput;



Topic archived. No new replies allowed.