Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Closed Thread
 
Thread Tools Display Modes
Old May 16th, 2006, 9:54 AM   #1
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 22
Rep Power: 0 lilmul123 is on a distinguished road
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!
lilmul123 is offline  
Old May 16th, 2006, 10:02 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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];
  }
This assumes that day has a value between 1 and 30 (and will not work correctly otherwise). You might want to ensure that assumption is true.

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).
grumpy is offline  
Old May 16th, 2006, 10:23 AM   #3
lilmul123
Newbie
 
Join Date: Mar 2006
Posts: 22
Rep Power: 0 lilmul123 is on a distinguished road
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[]
lilmul123 is offline  
Old May 16th, 2006, 11:39 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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
Note that system headers, by convention, use angle-brackets (< and >), whereas double-quotes ( " ) are used for your project headers.

OK, onto the problem, now I've made sure you've paid attention.

cin >> temp[day];
Easy. I suggest you declare temp to have 31 elements, not 30, by the way.
__________________
Me :: You :: Them
Ooble is offline  
Old May 17th, 2006, 6:39 AM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Indeed. There are a few months with 31 days.
grumpy is offline  
Old May 17th, 2006, 8:28 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline  
Old May 17th, 2006, 1:27 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by DaWei
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).
I've been saying that for years now. No one listened.
__________________
Me :: You :: Them
Ooble is offline  
Old May 17th, 2006, 2:30 PM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline  
Old May 17th, 2006, 6:00 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline  
Old May 17th, 2006, 6:23 PM   #10
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
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:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline  
Closed Thread

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC