![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 22
Rep Power: 0
![]() |
Help with this program
The goal is to have the user input the amount of days they would like to input the temperatures for, then have the user input those temperatures, and then output the average, the maximum, and the minimum of the temperatures:
#include "conio.h"
#include "stdio.h"
#include "iostream.h"
void main()
{
int day,temp[30],averagetemp,mintemp,maxtemp;
{
clrscr();
cout << "For how many days in the month would you like to record the temperature?\n";
cin >> day;
cout << "Average temperature:" << averagetemp << "\n";
cout << "Maximum temperature:" << maxtemp << "\n";
cout << "Minimum temperature:" << mintemp << "\n";
getch();
}
}The only place I'm really stuck on is how to only ask the user for the temperature for the number of days inputted and then how to put the data into the array. Help please! |
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
After cin >> day, add a loop something like;
for (int i = 0; i < day; ++day)
{
cout << "Enter temp for day " << (day + 1) << '\n';
cin >> temp[day];
}Once you have the array, computing averagetemp, maxtemp, and mintemp will be trivial. BTW: <conio.h>, <iostream.h>, clrscr(), and getch() are non-standard. <stdio.h> is standard, but deprecated (i.e. using it is discouraged) in C++. main() also returns an int (not void). |
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 22
Rep Power: 0
![]() |
here's what I have now:
#include "conio.h"
#include "stdio.h"
#include "iostream.h"
void main()
{
int day,maxday,temp[30],averagetemp,mintemp,maxtemp;
{
clrscr();
cout << "For how many days in the month would you like to record the temperature?\n";
cin >> maxday;
}
for(day=1;day<=maxday;day++);
{
cout << "What is the temperature for day" << day << "?\n";
cin >> temp[];
}
if(day>=31)
{
cout << "There is not 31 days in a month.";
}
cout << "Average temperature:" << averagetemp << "\n";
cout << "Maximum temperature:" << maxtemp << "\n";
cout << "Minimum temperature:" << mintemp << "\n";
getch();
}the only problem now is that i don't know how to insert values into each part of the array for the temp (cin >> temp[] ![]() |
|
|
|
|
#4 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
A couple of pointers to do with your code, first of all. First and foremost, use tabbing.
for (int i = 0; i < 10; i++)
{
if (something)
{
do_stuff(); // bad
}
}
for (int i = 0; i < 10; i++)
{
if (something)
{
do_stuff(); // good
}
}Second of all, you're using old, deprecated headers. #include <iostream.h> // bad #include <stdio.h> // bad in C++, good in C #include <iostream> // standard C++ headers don't end in ".h" #include <cstdio> // C headers in C++ have a "c" prefix OK, onto the problem, now I've made sure you've paid attention. ![]() cin >> temp[day]; |
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5
![]() |
Indeed. There are a few months with 31 days.
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I move we change to 13 months of 28 days. The extra day "between" years will be "Tween Day" (you may have to give it to your wife to make up for the anniversary or birthday you forgot about, or it can just be party day).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
#7 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
![]() |
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Hell, they're not listening to me either. Wanna go commiserate in a pub?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Hey, why DON'T you read the FAQ? Forum rules mean nothing to you because your mama pats your butt and tells you what a good boy you are? Fahgeddaboudit. She doesn't actually LIE, she's just your mama.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
#10 | |
|
Professional Programmer
|
Don't troll skoob. It's a horrible act and you can be banned if you do it enough times. I'm sure if big_k reads this thread and sees the post if no one's already warned him about it, he will consider this your first offense.
He's helped me and many others countless times, even if it was a simple "Read the forums FAQs." So before preparing a speech degrading yourself, the least you could do is search, just to back yourself up.
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|