I am trying to modularize this program and make it so that at the end of the conversions, it will go back to the choice and let the user start over with a new conversion, or enter a new temperature to do the same type conversion over again, or exit. When it gets to the end of the conversion, even though the new selection is entered, it exits as soon as the choice is made, or just exits after it gives the answer to the conversion. It does not give any errors. I know I must have a logic error somewhere, but since I have never done this before, I do not know how a return function is supposed to look and how to code it. I have spent way too much time on this one program flaw that I know is basic to someone that actually does any C coding on a regular basis. Please help me learn how to do this correctly and point me in the right direction. Here is the code I am using.
CODE
//title:pkc-ver256.c
/* Fahrenheit to Celsius or Celsius to Fahrenheit conversion program, Revision 5 */
#include <stdio.h> /*Define data type*/
int ask(); //function ask prototype
int calc1(); //function calc1 prototype
int calc2(); //function calc2 prototype
char display(); //function display prototype
int main()
{
float fFahrenheit = 0.0f; /*Variable to hold Fahrenheit as float*/
float fCelsius = 0.0f; //Variable to hold Celsius as float
char cAnswer = '\0'; /*Variable to hold yes/no answer*/
// Ask will be responsible for asking and returning their choice.
// We store the choice to evaluate in the switch.
int getit = ask();
// Using a switch we evaluate what choice they made.
// 1, 2 and any other input is considered exit.
switch (getit) {
case 1:
calc1();
break;
case 2:
calc2();
break;
default:
// Exits on any choice besides 1 and 2
break;
}
return 0;
}
int ask()
{
int choice=0;
// Prompt for choice
printf("Enter 1 to convert a Fahrenheit temperature to Celsius\n");
printf("Enter 2 to convert a Celsius temperature to Fahrenheit\n");
printf("Enter 3 to exit\n");
scanf("%d",&choice);
// Check if the number they chose is one of the 3 and is a digit
if ((isdigit(choice)) && (choice < 1) || (choice > 3)) {
printf("Sorry, you must choose 1, 2, or 3. Please try again.\n");
scanf("%d",&choice);
} //end if
else {
// Return that choice to main
return choice;
} // end else
} // end ask
// Again we match the prototype.
// Since we are void, we don't return anything. We just collect, calculate, and print message.
int calc1()
{
float fFahrenheit = 0.0f; /*Variable to hold Fahrenheit as float*/
float fCelsius = 0.0f; //Variable to hold Celsius as float
int choice=0; /*Variable to hold answer*/
printf("Please enter a Faherinheit temperature to be converted to Celsius\n example 82 or 71.5\n");
scanf("%f",&fFahrenheit); /*Get Fahrenheit variable from user input*/
fCelsius = (float)(fFahrenheit - 32)/1.8;
printf("Fahrenheit = %.1f\nCelsius = %.1f\n" , fFahrenheit, fCelsius); /*Print out Fahrenheit number given and the Celsius Equivilant*/
printf("press 1 to enter another fahrenheit number, 2 for Celsius, or 3 to exit/n");
scanf("%d",&choice);
// Check if the number they chose is one of the 3 and is a digit
while ((isdigit(choice)) && (choice < 1) || (choice > 3)) {
printf("Sorry, you must choose 1, 2, or 3. Please try again.\n");
scanf("%d",&choice);
}
// Return that choice to main
return choice;
}
// Prints a message showing our conversion.
int calc2()
{
float fFahrenheit = 0.0f; /*Variable to hold Fahrenheit*/
float fCelsius = 0.0f; //variable to hold Celsius as float
char cAnswer = '\0'; /*Variable to hold yes/no answer*/
printf("Please enter a Celsius temperature to be converted to Fahrenheit\n example 10 or 21.5\n");
scanf("%f",&fCelsius); /*Get Celsius variable from user input*/
fFahrenheit = (float)(fCelsius * 1.8) + 32;
printf("Fahrenheit = %.1f\nCelsius = %.1f\n" , fFahrenheit, fCelsius); /*Print out Fahrenheit number given and the Celsius Equivilant*/
printf("Would you like to enter another temperature? enter y for yes or n for no/n");
scanf("%c",&cAnswer);
toupper(cAnswer);
if (cAnswer=='Y') {
printf("Please enter a Faherinheit temperature to be converted to Celsius\n example 82 or 71.5\n");
scanf("%f",&fFahrenheit); /*Get Fahrenheit variable from user input*/
fCelsius = (fFahrenheit - 32) * (005.00/009.00); /*Perform calculation to convert fahrenheit to Celsius*/
printf("Fahrenheit = %.1f\nCelsius = %.1f\n" , fFahrenheit, fCelsius); /*Print out Fahrenheit number given and the Celsius Equivilant*/
printf("Press enter to exit");
getchar();
}
else {
printf("Press enter to exit");
getchar();
return 0;}
}
*mod edit - fixed code tags - use them like this
This post has been edited by jjhaag: 7 Jan, 2008 - 10:30 AM