0

我和我的朋友正在尝试一起构建一个程序,但它似乎不起作用。我们俩都没有太多的 C 经验,所以我们无法发现问题......任何建议或帮助将不胜感激!为略显尴尬的歌词道歉?

[编辑] 问题是当我们输入值时,我们会得到像 4586368 这样的荒谬数字。

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

void main()
{
    int room[20] = {};
    int i;
    int rooms = 0;
    char option = 0;
    int lights = 0; 
    int hrsUsed = 0; 
    int Telly = 0;
    int TVWatt =0;
    int sumTV;
    int TVuse = 0;
    int Computer = 0;
    int compWatt = 0;
    int compUsed = 0;
    int compTotal;
    int kwH_lights;
    int fridge = 0;
    int washLoad = 0;
    int dryerLoad = 0, dishLoad = 0, cookLoad = 0;
    int showeruse = 0;
    int total_kWh;

    printf("Enter number of rooms");
    scanf_s("%d", &rooms);


        for(i=0;i<rooms;i++)
    {
        printf("input average wattage of lights");
        scanf_s("%d", &lights);
        lights=lights/1000;
        printf("input number of hours use/day (average)");
        scanf_s("%d", &hrsUsed);

        kwH_lights=((lights*hrsUsed)*365);

        printf("input number of TVs");
        scanf_s("%d", &Telly);
        printf("input average wattage");
        scanf_s("%d", &TVWatt);
        printf("input average use a day");
        scanf_s("%d", &TVuse);
        sumTV=((Telly*(TVWatt/1000))*TVuse)*365;
    }
        printf("Input number of fridge/freezer");
        scanf_s("%d",&fridge);
        fridge=(fridge*2)*365;
        printf("input number of Computers and/or video game consoles in the house");
        scanf_s("%d", &Computer);

        for(i=0;i<Computer;i++) {
            printf("input  wattage");
            scanf_s("%d", &compWatt);
            printf("input average hrs used/day");
            scanf_s("%d", &compUsed);
            compTotal=((compWatt/1000)*compUsed)*365; 
                    }


        printf("Input average number of washing machine loads /day");
        scanf_s("%d",&washLoad);
        washLoad=washLoad*365;
        printf("Input average number of clothes dryer loads/day");
        scanf_s("%d",&dryerLoad);
        dryerLoad=(dryerLoad*3)*365;
        printf("Input average number of dishwasher loads/day");
        scanf_s("%d",&dishLoad);
        dishLoad=(dishLoad*1.5)*365;
        printf("Input average cooking load/day");
        scanf_s("%d",&cookLoad);
        cookLoad=(cookLoad*7)*365;
        printf("Input average hrs/day of shower usage");
        scanf_s("%d",&showeruse);
        showeruse=(showeruse*7)*365;

        total_kWh=((kwH_lights)+(sumTV)+(fridge)+(compTotal)+(dryerLoad)+(dishLoad)+(cookLoad)+(showeruse));
        printf("Total= %d", &total_kWh);

}
4

4 回答 4

1

我的第一步是更正第二个 for loop { } ... 解决此问题并再次询问。

[编辑] 您使用 int 值除以其他 int (compwatt / 1000) 的计算...您确定您使用 int 的想法是正确的吗?

或者:

cookLoad=(cookLoad*7)*365;

为什么要乘以 7 AND 365?平均/天不应该只乘以365吗?

于 2013-11-11T12:27:28.980 回答
1

你应该改变这个:

printf("Total= %d", &total_kWh);

对此:

printf("Total= %d", total_kWh);

所有其他整数变量也是如此。

于 2013-11-11T12:27:45.690 回答
1

您的代码中有很多错误:

  • 您打印了内存地址而不是结果值(如果您的变量是普通的,请不要使用&with )printfint
  • 计算机 for 循环没有大括号(所以只有printf语句被循环)
  • 结果未汇总(在所有循环中,您刚刚覆盖了上一个循环的输入)
  • 数组从未使用过rooms[]- 还有一些其他变量(可能的错误来源,如果您想使用它们而忘记了)
  • 与 1.5 相乘的结果将保留一个double值 - 您应该将其转换回int( dishLoad)

大胆的错误可能是那个,为什么你的值是错误的......另请注意:最好按周或月询问“洗衣机负载/干衣机负载/洗碗机负载的平均数量”......或者应该保持浮动点值:因为我认识的每个人都不会每天多次使用洗衣机和干衣机。所以现在你不能输入像每周一次这样的东西(这将是一个 0.14 的系数,但由于所有值都存储为 ,所以不可输入int)。

这里是所有已修复的代码,我可以找到:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

int main(int argc, char** argv){
  int i = 0;
  int rooms = 0;
  int lights = 0; 
  int hrsUsed = 0; 
  int Telly = 0;
  int TVWatt =0;
  int sumTV = 0;
  int TVuse = 0;
  int Computer = 0;
  int compWatt = 0;
  int compUsed = 0;
  int compTotal= 0;
  int kwH_lights = 0;
  int fridge = 0;
  int washLoad = 0;
  int dryerLoad = 0, dishLoad = 0, cookLoad = 0;
  int showeruse = 0;
  int total_kWh = 0;

  printf("Enter number of rooms: ");
  scanf_s("%d", &rooms);

  for(i=0;i<rooms;i++){
    printf("A few questions about room %d\n", i+1);
    printf("input average wattage of lights: ");
    scanf_s("%d", &lights);
    lights+=lights/1000;
    printf("input number of hours use/day (average): ");
    scanf_s("%d", &hrsUsed);
    kwH_lights+=((lights*hrsUsed)*365);
    printf("input number of TVs: ");
    scanf_s("%d", &Telly);
    printf("input average wattage: ");
    scanf_s("%d", &TVWatt);
    printf("input average use a day: ");
    scanf_s("%d", &TVuse);
    sumTV+=((Telly*(TVWatt/1000))*TVuse)*365;
  }
  printf("Input number of fridge/freezer: ");
  scanf_s("%d",&fridge);
  fridge=(fridge*2)*365;
  printf("input number of Computers and/or video game consoles in the house: ");
  scanf_s("%d", &Computer);

  for(i=0;i<Computer;i++){
    printf("A few questions about computer %d\n", i+1);
    printf("input  wattage: ");
    scanf_s("%d", &compWatt);
    printf("input average hrs used/day: ");
    scanf_s("%d", &compUsed);
    compTotal += ((compWatt/1000)*compUsed)*365;
  }

  printf("Input average number of washing machine loads/day: ");
  scanf_s("%d",&washLoad);
  washLoad=washLoad*365;
  printf("Input average number of clothes dryer loads/day: ");
  scanf_s("%d",&dryerLoad);
  dryerLoad=(dryerLoad*3)*365;
  printf("Input average number of dishwasher loads/day: ");
  scanf_s("%d",&dishLoad);
  dishLoad=(int)((dishLoad*1.5)*365);
  printf("Input average cooking load/day: ");
  scanf_s("%d",&cookLoad);
  cookLoad=(cookLoad*7)*365;
  printf("Input average hrs/day of shower usage: ");
  scanf_s("%d",&showeruse);
  showeruse=(showeruse*7)*365;

  total_kWh=((kwH_lights)+(sumTV)+(fridge)+(compTotal)+(dryerLoad)+(dishLoad)+(cookLoad)+(showeruse));
  printf("Total= %d\n", total_kWh);
  system("Pause");
  return 0;
}

我希望它对您有所帮助-如果您还有任何问题,请随时提出。

于 2013-11-11T13:02:36.003 回答
0

为了使您的代码更具可读性,您可以使用如下复合赋值运算符,

   Operator Name             Syntax    Meaning
-------------------------------------------------
Addition assignment         a += b    a = a + b 
Subtraction assignment      a -= b    a = a - b
Multiplication assignment   a *= b    a = a * b
Division assignment         a /= b    a = a / b
于 2013-11-11T13:37:08.137 回答