0

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);

}
4
  • 1
    What is the program supposed to do? What are the expected results? What are the actual results? Commented Nov 11, 2013 at 12:26
  • 2
    Can you sort out your formatting? I can't tell if your second for loop is lacking a brace or if it's just nasty layout! Commented Nov 11, 2013 at 12:26
  • 1
    The intended block after for(i=0;i<Computer;i++) needs surely to be enclosed in braces. Commented Nov 11, 2013 at 12:33
  • I apologise for the formatting; copy and pasting seems to change the layout. The for loop is fixed with no change. The program is designed to try and calculate the total energy requirements of our house. Commented Nov 11, 2013 at 13:11

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

cookLoad=(cookLoad*7)*365; should be cookLoad=cookLoad *365; what is the meaning of showeruse*7?
@nabuchodonossor I don't think so - The 7 is probably some factor the OP is using to calculate the energy that is used by the shower & cooking. See also i.e. dishwasher & clothes dryer the 3 and 1.5 makes no sense there either...
1

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?

1 Comment

The second for loop was a slip when copy and pasting over... It's fixed and in the program and I'm still not getting a proper result.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.