-1

I know this is going to be something of a silly slip or oversight on my behalf, but I can't get the array in this to print out correctly. When I run the code and put in my inputs, I get seemingly random numbers. For example, number of rooms was 1 wattage of lights was 2 hours used was 2 TV/computers was 2

The output I got was 3930804. What did I miss?

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

int main()
{
    int room[20] = {0.0};
    int i;
    int rooms = 0;
    char option = 0;
    int lights[20]; 
    int hrsUsed[20]; 
    int Telly_Computer[20];

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


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

        printf("%d \n", lights);

}
1
  • 1
    You're not doing any of your scanf_s right. Commented Nov 11, 2013 at 0:08

2 Answers 2

1
printf("%d \n", lights);

You're printing the array directly. You need to loop over it and print the elements one at a time.

int i;
for (i = 0; i < 20; ++i)
  printf("%d\n", lights[i]);
Sign up to request clarification or add additional context in comments.

Comments

1

You are just printing the address of lights (and using UndefinedBehavior by the way, address must be printed with %p). You must use a loop to print out all of the contents of each array slot.

for(int i=0;i<(sizeof(lights)/sizeof(int));i++)
    printf("%d\n",lights[i]);

1 Comment

sizeof(lights) will return the number of bytes in teh array. need to divide it by sizeof(int)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.