简体   繁体   中英

Problems with a program [C arrays]

My friend and I are trying to build a program together, but it just doesn't seem to be working. Neither of us have much experience with C, so we just can't spot the issue... Any advice or help would be much appreciated! Apologies for the slightly awkward lyrics?

[Edit] The problem is that when we input values, we get ridiculous figures like 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);

}

My first step would be to correct the second for loop { } ... fix this and ask again.

[EDIT] your calculations with usages of int values divided by other ints (compwatt / 1000) ... are you sure your idea of using int is correct?

or:

cookLoad=(cookLoad*7)*365;

why multiplying with 7 AND 365? should not the average / day be multiplied by 365 only?

You should change this:

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

to that:

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

Same is true for all your other integer variables.

There were quite a few mistakes in your code:

  • you printed the memory-address instead of result value (don't use & with printf if your variable is a plain int )
  • the computer for-loop had no curly brackets (so only the printf statement was looped)
  • results were not summed up (in all loops you've just overwritten your inputs from the last loop)
  • the rooms[] Array was never used - a few other variables also (possible error source, if you wanted to use them and just forgot it)
  • the result from a multiplication with 1.5 will hold a double value - you should cast that back to int ( dishLoad )

The bold mistake is probably that one, why your values were wrong... Also notice: The 'average number of washing machine loads/clothes dryer loads/ dishwasher loads' should better be asked by week or month... Or should hold Floating Point values: Because everyone I know don't use the washing machine and clothes dryer every day multiple times. So now you can't enter something like once a week (which would be an factor of 0.14, but is not enterable cause all values are stored as int ).

Here Comes the code with everything fixed, I could found:

#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;
}

I hope it helps you out - if you got any questions left, feel free to ask.

For more readability of your code, you can employ compound assignment operators as below,

   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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM