C ++ Cin没有正确执行

时间:2014-09-14 23:23:22

标签: c++

我无法正常执行“cin>>卡路里”。代码自动将卡路里设置为-279846518796,有时还设置其他数字(通常是像之前一样的大负数)。从而使最终结果变得一团糟。即使我将“卡路里”设置为int,我也会得到同样的错误。

#include <iostream>

int main( ) 
{
int height, weight, age, edible_newfoods;
double bmr, calories;
char gender, activity_level, newfood;

//Take user inputs.
cout << "Please provide your information." << endl;
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;

//Ask the user how active they are, to get an input between four choices
cout << endl << "How active are you?" << endl; 
cout << "   s - sedentary" << endl;
cout << "   c - casual exerciser--exercise ocasionally" << endl;
cout << "   a - active exerciser--exercise 3-4 days a week" << endl;
cout << "   d - devoted exerciser--exercise everyday" << endl;

//Take users activity level as a char
cout << "Enter the level of association with your activity level: ";
cin >> activity_level;

//Adding more food
cout << endl << "What type of food do you want to eat?" << endl;

cout << "Please use _ between words to create a single word. ";
cin >> newfood;

cout << "How many calories per item? ";
//This is where the cin automatically makes calories a giant negative number.
// causing the final output of results to be a mess.
cin >> calories;

//Calculations - Adjusting BMR to accommodate for activity level
switch (activity_level)
{
    case 's':
    case 'S': activity_level = 0.95; break;
    case 'c':
    case 'C': activity_level = 1.30; break;
    case 'a': 
    case 'A': activity_level = 1.40; break;
    case 'd':
    case 'D': activity_level = 1.50; break;
} 

if (age <= 65 && (gender == 'm' || gender == 'M')) {
    bmr = 1.375 * ((66 + 6.3 * weight) + (12.9 * height) - (6.8 * age));
}
else {
    bmr = 1.375 * ((655 + 4.3 * weight) + (4.7 * height) - (4.7 * age));
}

bmr = bmr * activity_level;

edible_newfoods = bmr / calories;

cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(0);

//output the results to user.
cout << endl << "The BMR for a " << weight << " pound " << gender << " who is " << age << " years    old is " << bmr
<< " calories number of " << newfood << " eaten = " << edible_newfoods;
}

请善待,我是stackoverflow和编码新手。任何帮助,将不胜感激。谢谢

2 个答案:

答案 0 :(得分:1)

activity_level属于char类型,将double分配给它看起来不正确:

char gender, activity_level, newfood;
...
activity_level = 0.95;

虽然它仍然是一种有效的语言结构,但它会导致浮动部分截断并将0分配给activity_level。请参阅 @KeithThompson 的评论。

似乎需要两个不同的变量,

  • 一个(char)要保留输入并在switch中使用,
  • 另一个(double)作为bmr = bmr * activity_level;
  • 的因素

答案 1 :(得分:0)

问题从这里开始:

cout << "Enter the level of association with your activity level: ";
cin >> activity_level;

activity_level的类型为char。当您执行cin >> char时,不会跳过前导空格。因此,此行会选取您在上一个数字输入后按下的Enter键。您应该已经注意到该程序从未等待您在此处输入任何内容,并直接显示“输入与您的活动级别关联的级别”。

下一个问题是下一个输入:

cout << "Please use _ between words to create a single word. ";
cin >> newfood;

newfood的类型为char。这意味着单个字符。它不能保留任何单词(“a”或“I”除外)。

要解决这两个问题,请对所有这三个字符使用std::string而不是char。此外,不是使用只获得单个单词的cin >> newfood,而是getline(cin, newfood);获取整行(并使用换行符)。将此技术用于activity_levelgender

下一个问题是:

switch (activity_level)
{
    case 's':
    case 'S': activity_level = 0.95;

您正试图将0.95存储在char中,但这不起作用。在这里使用一个新变量,例如double activity_factor。确保设置了默认值或default个案例。否则,如果该人未输入预期的角色,您将获得垃圾。

最后,为了防止输入无效时的垃圾输出,您可以在最终输出之前添加以下内容:

if ( !cin )
{
    cerr << "Invalid input entered.\n";
    return 0;
}

或类似的东西。那么至少你会得到一些明智的东西。要提供更有针对性的错误消息,请在每次输入后执行此检查。