GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Mar-2004, 20:28
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

Program using a for loop


I cant figure this out. I have tried working on this for several hours with no luck. I have this so far and dont know if I am even on the right track. Here is the code I wrote so far:

CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

int main()
{

	int count, total, days;
	float temp;


	total=0;

	printf("\nPlease enter the number of days temperatures you wish to enter:   ");
	scanf("%d", days);

	for (count=0;count=?;++count)
	{
		printf("\nTemperature is:   ");
		scanf("%.2f", &temp);
		
		total=total+days;

	}
	
	getch();
	return 0;


}

This program asks the user how many days to enter the temperature of any certain day. Use a for loop that allows the user to enter the number of temperatures that the user has specified. Compute and display the average temp at end of file. Any help would greatly be appreciated. I am having trouble because the for loop. Count=0; count=??? This should be equal to whatever the user puts in for the amount of days.
  #2  
Old 10-Mar-2004, 20:46
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Tommy. When you post code, it really helps if you enclose it between [c] and [/c]. It makes it much easier to read as you can see.

You probably want your for statement to look more like:
CPP / C++ / C Code:
for (count=0;count<days;++count)

I give a break down of the elements of a for statement in this post.

Also, I think that you would want your total to be a float and that you should total your tempatures in your for statement like this:
CPP / C++ / C Code:
total = total + temp;   //or total += temp;

and then when you exit your for loop you could print the average with
CPP / C++ / C Code:
printf("The average temperature was: %5.2\n", total/days);
  #3  
Old 10-Mar-2004, 21:24
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

almost there


CPP / C++ / C Code:
#include <stdio.h>
#include <conio.h>

int main()
{

	int count, days;
	float temp, total, avg;


	total=0.0;

	printf("\nPlease enter the number of days temperatures you wish to enter:   ");
	scanf("%d", &days);

	for (count=0;count<days;++count)
	{
		printf("\nTemperature is:   ");
		scanf("%.2f", &temp);
		
		total=total+temp;

	}

	avg=total/days;
	printf("\n\nThe average temperature was: %5.2f\n", avg);
	
	getch();
	return 0;


}






Here is the results so far:

Please enter the number of days temperatures you wish to enter:   6

Temperature is:   54.3

Temperature is:
Temperature is:
Temperature is:
Temperature is:
Temperature is:

The average temperature was: -107374176.00

I now only need to add the temps.  Do I need another printf or scanf statement?  Thanks again for your help.....
  #4  
Old 10-Mar-2004, 23:16
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
The problem is scanf().

Define a character buffer called buf[12]; and change each scanf()s to:
CPP / C++ / C Code:
fgets(buf, 10, stdin);
sscanf(buf,"%d", &days);
------
fgets(buf, 10, stdin);
sscanf(buf,"%f", &temp);

scanf() is leaving crap in the input buffer.
Also note the change in the %f
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
C program to convert NFA to DFA ayoub C Programming Language 11 14-Sep-2007 15:51
Please Help, problems writing newbie c program soulfly C Programming Language 14 04-Mar-2004 16:16
compiling a program within another (C++) Siphiro C++ Forum 5 06-Feb-2004 16:35
error during program rjd72285 C++ Forum 0 11-Nov-2003 19:49
one program access another? dgoulston C++ Forum 1 07-Oct-2003 12:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:57.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.