Beginner using C programming. Please see if you can help me.

This is a discussion on Beginner using C programming. Please see if you can help me. within the C Programming forums, part of the General Programming Boards category; I have to create a program that converts temperature values from Fahrenheit to Celsius, ask the user to input the ...

1. Beginner using C programming. Please see if you can help me.

I have to create a program that converts temperature values from Fahrenheit to Celsius, ask the user to input the number of Fahrenheit temperature to be converted (n). Use a for loop to read the temperatures from keyboard , output the original and converted temperatures in a tabulated format with two decimal points.
Calculate and output the average temperature in Celsius with two decimal points.
Below is what I have so far.... Am I right so far with what I have?

Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
#include <iostream>
#include <string.h>
void main ()
{
int fahr, cel;

printf ("Enter the starting temperature in degrees Fahrenheit: ");
scanf("%d", &fahr);
for( x = 0; x < 25; x++ )
{
cel=(fahr-32)*5/9;
printf("\n%d Fahrenheit is equal to %d degrees celsius\n\n",fahr,cel);

}
printf("\nPress any key to exit the program ...\n\n");
getche();
}

2. Try to compile your program with a high warning level. What does your compiler report?

ask the user to input the number of Fahrenheit temperature to be converted (n). Use a ‘for’ loop to read the temperatures from keyboard , output the original and converted temperatures in a tabulated format with two decimal points.

If you need to ask temperature values for every n times, go for providing the scanf inside the for loop.

Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main ( )
{
int n ;
float fahr , cel ;

printf ("Enter the no.of.values for which conversion is needed: ");
scanf("%d", &n);
for( x = 0; x < n; x++ )
{
printf("\nEnter Fahrenheit value %d",x+1) ;
scanf("%f",&fahr) ;
cel=(fahr-32)*5/9;
printf("\n%f Fahrenheit is equal to %f degrees celsius\n\n",fahr,cel);

}
printf("\nPress any key to exit the program ...\n\n");
getch();
}
Do as per this code. Do not include iostream.h in this file. iostream is for c++ format and stdio.h is for c format. Include either any one among those two.

... Use a for loop to read the temperatures from keyboard , output the original and converted temperatures in a tabulated format with two decimal points.
Then the lines 11 and 12 in your code should be moved inside the loop (assuming that you wish to read at most 25 times temperature from the kyboard, yet if I were you I would use a infinite loop which will end based on a specific value entered by the user).

The lines 19 and 20 in your code, as I understand are useless, because once the for loop ends, the program reaches automatically its end point as would be no more statement to run.

NOTE: If you choose to read values inside a loop with scanf don't forget to put a space character before %d in the format string

Therefore:
Code:
scanf(" %d", &fahr);
Code:
scanf("%d", &fahr);
Otherwise you will have problem because end line characters of precedent reads will be mixed during the next reads.

Regards,
Dariyoosh

5. Originally Posted by dariyoosh
Otherwise you will have problem because end line characters of precedent reads will be mixed during the next reads.
Not really.

Example
Code:
#include <stdio.h>

int main(void)
{
int a;

while(1)
{
printf("Type 5 to exit\n");
scanf("%d",&a);
if(a == 5)
break;
printf("ok\n");
}

return 0;
}
It is ok.

Code:
#include <stdio.h>

int main(void)
{
char a;

while(1)
{
printf("Type e to exit\n");
scanf("%c",&a);
if(a == 'e')
break;
printf("ok\n");
}

return 0;
}
not ok.

Why?

Because with the %d it will eat the special characters ( logical enough, because they are not numbers ).
In case of %c, reading characters that is, what you said is needed

6. Code:
#include <stdio.h>
#include <conio.h>

int main ()
{
int fahr, cel, x;

printf ("Enter the starting temperature in degrees Fahrenheit: ");
scanf("%d", &fahr);
for( x = 0; x < 25; x++ )
{
cel=(fahr-32)*5/9;
}
printf("\n%d Fahrenheit is equal to %d degrees celsius\n\n",fahr,cel);
printf("\nPress any key to exit the program ...\n\n");
getch();
}