Why does this program skip Cin

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

int main()
{
	
	int Height, Weight, Age, Calories; //The variables that will be used in the program. 
		double BMR;
			char Gender, ExerciseType;
				std::string food_choice;
	
	cout.setf(ios::fixed); 
	cout.setf(ios::showpoint);
	cout.precision(0); 
	
	cout << "Please provide your basic information" << endl; //Collecting of inputs for later phase of program.
	cout << "male or female (m or f)? ";
		cin >> Gender;
		
	cout << "age in years? ";
		cin >> Age;
		
	cout << "weight in pounds? ";
		cin >> Weight;
		
	cout << "height in inches? ";
		cin >> Height;
		
	cout << "How active are you?" << endl; 
				cout << "s - sedentary" << endl;
				cout << "c - casual exerciser--exercise occasionally" << endl; 
				cout << "a - active exerciser--exercise 3-4 days per week" << endl;
				cout << "d - devoted exerciser--exercise every day" <<endl;
	cout << "Enter the letter association with your activity level: "; 
		cin >> ExerciseType;
		
	cout << "What type of food do you want to eat? Please use _ between words to create a single word.";
		std::getline(std::cin, food_choice);
		
	cout << "How many calories per item? ";
		cin >> Calories;
		
		
		if (Age >= 65) // Statements that build only once conditions are met and fufills the program.
		{BMR = 1.375 * (655 + (4.3 * Weight) + (4.7 * Height) - (4.7 * Age));
		}
		
			else if (Gender == 'f' || Gender == 'F')
			{BMR = 1.375 * (655 + (4.3 * Weight) + (4.7 * Height) - (4.7 * Age));
			} 
		
				else  (Gender =='m' || Gender == 'M');
				{BMR = 1.375 * (66 + (6.3 * Weight) + (12.9 * Height) - (6.8 * Age));
				}
					
switch(ExerciseType) // Calculations for the differing case
{
	case 's' :
		BMR = (BMR - (BMR * .05));
		cout << "BMR = " << BMR << endl;
		BMR = (BMR / Calories);
		cout << "number of " << food_choice << " eaten = " << BMR;
		break;
	case 'c' :
		BMR = ((BMR * .30) + BMR);
		cout << "BMR = " << BMR << endl;
		BMR = (BMR / Calories);
		cout << "number of " << food_choice << " eaten = " << BMR;
		break;
	case 'a' :
		BMR = ((BMR * .40) + BMR);
		cout << "BMR = " << BMR << endl;
		BMR = (BMR / Calories);
		cout << "number of " << food_choice << " eaten = " << BMR;
		break;
	case 'd' :
		BMR = ((BMR * .50) + BMR);
		cout << "BMR = " << BMR << endl;
		BMR = (BMR / Calories);
		cout << "number of " << food_choice << " eaten = " << BMR;
	default :
		cout << "Invalid Choice" << endl;
	return 0;
}

}

Problem on line 40 and 41 The program won't run.
Last edited on
Please place your code [code]between code tags[/code] - you can edit your post.

Please provide the example input that causes the problem.
OK reformatted
Line 38 - where does food come from? The program does not compile because it does not exist:
http://ideone.com/GD2xUq
Sorry during reformatting i erased it. i can make this easy by asking this. i want to someone to put in something like "ice cream" and later in the program i say you can eat 8 " ics cream"s or whatever the user puts in.
You need to be more specific than that. I don't know how to help when I can't read your mind.
The program takes the users age, height, weight and calculates BMR(so equation to keep weight stable). The program needs to be edited so that the user can add some different variable and get an answer. The only problem i'm having is getting the program to store the users food choice as a word instead of a number.
Have you tried using a std::string in conjunction with std::getline()?
1
2
std::string food_choice;
std::getline(std::cin, food_choice);
okay but now the program is skipping the input for food 37-38.
Please repost your current code, I cannot see your screen or your finger.
Here it goes
imamillion wrote:
Here it goes
Next time don't use invisible ink ;p
1
2
3
4
5
	cout << "Enter the letter association with your activity level: "; 
		cin >> ExerciseType;
		
	cout << "What type of food do you want to eat? Please use _ between words to create a single word.";
		std::getline(std::cin, food_choice);
cin >> leaves a newline in the buffer after you hit enter. By default getline reads until a newline is found so you should remove the extra newline from the buffer before using getline. You can use std::cin.ignore();, std::cin.ignore(1024, '\n');, or std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); I prefer the last option.\

EDIT What is up with your formatting btw?
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
#include <iostream>
#include <string>
using namespace std;

int main()
{
	
	int Height, Weight, Age, Calories; //The variables that will be used in the program. 
		double BMR;
			char Gender, ExerciseType;
				std::string food_choice;
	
	cout.setf(ios::fixed); 
	cout.setf(ios::showpoint);
	cout.precision(0); 
	
	cout << "Please provide your basic information" << endl; //Collecting of inputs for later phase of program.
	cout << "male or female (m or f)? ";
		cin >> Gender;
		
	cout << "age in years? ";
		cin >> Age;
		
	cout << "weight in pounds? ";
		cin >> Weight;
		
	cout << "height in inches? ";
		cin >> Height;
		
	cout << "How active are you?" << endl; 
				cout << "s - sedentary" << endl;
				cout << "c - casual exerciser--exercise occasionally" << endl; 
				cout << "a - active exerciser--exercise 3-4 days per week" << endl;
				cout << "d - devoted exerciser--exercise every day" <<endl;
	cout << "Enter the letter association with your activity level: "; 
		cin >> ExerciseType;
		
	cout << "What type of food do you want to eat? Please use _ between words to create a single word.";
		std::getline(std::cin, food_choice);
		
	cout << "How many calories per item? ";
		cin >> Calories;
		
		
		if (Age >= 65) // Statements that build only once conditions are met and fufills the program.
		{BMR = 1.375 * (655 + (4.3 * Weight) + (4.7 * Height) - (4.7 * Age));
		}
		
			else if (Gender == 'f' || Gender == 'F')
			{BMR = 1.375 * (655 + (4.3 * Weight) + (4.7 * Height) - (4.7 * Age));
			} 
		
				else  (Gender =='m' || Gender == 'M');
				{BMR = 1.375 * (66 + (6.3 * Weight) + (12.9 * Height) - (6.8 * Age));
				}
					
switch(ExerciseType) // Calculations for the differing case
{
	case 's' :
		BMR = (BMR - (BMR * .05));
		cout << "BMR = " << BMR << endl;
		BMR = (BMR / Calories);
		cout << "number of " << food_choice << " eaten = " << BMR;
		break;
	case 'c' :
		BMR = ((BMR * .30) + BMR);
		cout << "BMR = " << BMR << endl;
		BMR = (BMR / Calories);
		cout << "number of " << food_choice << " eaten = " << BMR;
		break;
	case 'a' :
		BMR = ((BMR * .40) + BMR);
		cout << "BMR = " << BMR << endl;
		BMR = (BMR / Calories);
		cout << "number of " << food_choice << " eaten = " << BMR;
		break;
	case 'd' :
		BMR = ((BMR * .50) + BMR);
		cout << "BMR = " << BMR << endl;
		BMR = (BMR / Calories);
		cout << "number of " << food_choice << " eaten = " << BMR;
	default :
		cout << "Invalid Choice" << endl;
	return 0;
}

}
is pretty hard to read with the obfuscated indentations.
Last edited on
Thanks and srry i just started c++ i just format it crazily with no rules just idententions everywhere.
Is your problem solved, or do you still need help? It's not clear from your most recent post.
Generally you will indent inside each code block surrounded by a set of curly braces {}

So this is how I would write yours.

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

int main()
{
    int Height;
    int Weight;
    int Age;
    int Calories; //The variables that will be used in the program. 
    double BMR;
    char Gender, ExerciseType;
    std::string food_choice;
	
    std::cout.setf(ios::fixed); 
    std::cout.setf(ios::showpoint);
    std::cout.precision(0); 
	
    std::cout << "Please provide your basic information" << endl; //Collecting of inputs for later phase of program.
    std::cout << "male or female (m or f)? ";
    std::cin >> Gender;
    	
    std::cout << "age in years? ";
    std::cin >> Age;
    
    std::cout << "weight in pounds? ";
    std::cin >> Weight;
		
    std::cout << "height in inches? ";std::cin >> Height;
        	
    std::cout << "How active are you?" << std::endl; 
    std::cout << "s - sedentary" << std::endl;
    std::cout << "c - casual exerciser--exercise occasionally" << std::endl; 
    std::cout << "a - active exerciser--exercise 3-4 days per week" << std::endl;
    std::cout << "d - devoted exerciser--exercise every day" << std::endl;
    std::cout << "Enter the letter association with your activity level: "; 
    std::cin >> ExerciseType;
    	
    std::cout << "What type of food do you want to eat? Please use _ between words to create a single word.";
    std::cin.ignore(1024, '\n'); //ignore newline and anything else possibly left in buffer
    std::getline(std::cin, food_choice);
    	
    std::cout << "How many calories per item? ";
    std::cin >> Calories;		
    	
    if (Age >= 65) // Statements that build only once conditions are met and fufills the program.
    {
        BMR = 1.375 * (655 + (4.3 * Weight) + (4.7 * Height) - (4.7 * Age));
    }
    else if (Gender == 'f' || Gender == 'F')
    {
        BMR = 1.375 * (655 + (4.3 * Weight) + (4.7 * Height) - (4.7 * Age));
    } 
    else  if (Gender =='m' || Gender == 'M');
    {
        BMR = 1.375 * (66 + (6.3 * Weight) + (12.9 * Height) - (6.8 * Age));
    }
    				
    switch(ExerciseType) // Calculations for the differing case
    {
    	case 's' :
    		BMR = (BMR - (BMR * .05));
    		std::cout << "BMR = " << BMR << std::endl;
    		BMR = (BMR / Calories);
    		std::cout << "number of " << food_choice << " eaten = " << BMR;
    		break;
    	case 'c' :
    		BMR = ((BMR * .30) + BMR);
    		std::cout << "BMR = " << BMR << std::endl;
    		BMR = (BMR / Calories);
    		std::cout << "number of " << food_choice << " eaten = " << BMR;
    		break;
    	case 'a' :
    		BMR = ((BMR * .40) + BMR);
    		std::cout << "BMR = " << BMR << std::endl;
    		BMR = (BMR / Calories);
    		std::cout << "number of " << food_choice << " eaten = " << BMR;
    		break;
    	case 'd' :
    		BMR = ((BMR * .50) + BMR);
    		std::cout << "BMR = " << BMR << std::endl;
    		BMR = (BMR / Calories);
    		std::cout << "number of " << food_choice << " eaten = " << BMR;
    	default :
    		std::cout << "Invalid Choice" << std::endl;
    	return 0;
    }
}
You also have multiple naming conventions most of them start with uppercase and one has underscores with lowercase. Though that is probably from copying LB's variable :P

Keep in mind most IDE's will indent it for you automatically when you hit enter.
Last edited on
Hey;
if getline() is followed by an earlier cin statement you need to flush out the endof line char from the previous feed.
in your code Before calling getline use the below line
cin.ignore(numeric_limits<streamsize>::max(), '\n');

I believe this should solve your problem.
Topic archived. No new replies allowed.