Please help with for loops

Hello for a homework assignment I have to write two "for loops" one to get user inputted data and one to print out user inputted data. Im stuck on how to get "Enter the average temperature in month %d and press Enter" How do you get month to count from 0-11 and ask for a temperature each month?

Here is the task:
Write a program and flowchart. The program should ask the user for the average temperature in each of the last 12 months. After the user has entered the temperatures, the program should display them. Use a "for" loop to ask for the data, and use another "for" loop to display the data. Store the data in an array called "temperature."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  #include "pch.h"
#include <iostream>
#define TEMPS 12

int main()
{
	int temperature[TEMPS];
	int counter;
	int number;
	
	printf("Enter the average temperature in month %d and press ENTER", &number);
	scanf_s("%d", temperature);
	for (number = 0; number < 12; number++);

	return 0;
Last edited on
consider
const int TEMPS = 12; //#define macros cause problems. Avoid them. It works here but later you can get subtle odd problems due to the implicit type of the macro's value.

for (number = 0; number < TEMPS; number++) //you did it. this counts from 0-11. I changed out the 12, but yours was correct, apart from using your constant so if you change it to 13 it still works.. (you wouldn't do that here, but again, that is why hard coded constants give issues)
{
cout << "enter for month " << number+1<< endl;
cin >> temperature[number]; //lets use cin, not C's scanf, if you don't have a very good reason for avoiding the C++ tools.
}

note that your scanf would only ever work on the first array location, due to some pointer magic going on there. it probably should have been the explicit form of &(temperature[number]) if you had used it in the loop.
Last edited on
well my teacher neem teaching us using C and not C++ so i dont want her to take points off for using CIN and cout...which Im not sure what they mean. Im only in entry lvl programming lmao
If you're using only C, then you shouldn't be #include <iostream> because that's something for C++. You should be #include <stdio.h>

I am going to assume you know how for loops work.

The first for loop needs to loop through the array get values for the array.
The second for loop just prints every element within the array.

1
2
3
4
5
6
7
for(...) {
  /* Get 12 values for the array. You can achieve this with literally one statement.*/
}

for(...) {
  /* Print the array. This can also be achieved with a single statement.*/
}
the you need to use the addressing in my note above, for C to work with the array properly.

cin / cout is ironically C INput and C OUTput but they are for C++. C++ has a dozen things named with Cs that are really either for both languages or for c++ only. Its poorly thought out in places.
ah okay thanks guys you both been a lot of help! I got it all to work!
Topic archived. No new replies allowed.