getting return values to the correct place

Functions end instead of going back to choiced of what the next functi

Page 1 of 1

2 Replies - 759 Views - Last Post: 07 January 2008 - 01:03 PM Rate Topic: -----

#1 C-Rookie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 15-December 07

getting return values to the correct place

Post icon  Posted 07 January 2008 - 11:23 AM

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.

//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 :code:

This post has been edited by jjhaag: 07 January 2008 - 11:30 AM


Is This A Good Question/Topic? 0
  • +

Replies To: getting return values to the correct place

#2 no2pencil  Icon User is online

  • Head MFIC
  • member icon

Reputation: 5066
  • View blog
  • Posts: 26,449
  • Joined: 10-May 07

Re: getting return values to the correct place

Posted 07 January 2008 - 11:29 AM

Rather than initialize the variable during the assignment of it's value:
int getit = ask();



I would initialize the variable prior to assigning it.

int getit = 0;
getit = ask();
printf("Value : %d\n",getit);




Also :
:code:
Was This Post Helpful? 0
  • +
  • -

#3 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: getting return values to the correct place

Posted 07 January 2008 - 01:03 PM

Please stop making new threads for this same bit of code (you've started five already).

We want to help, but it's hard if you keep moving around. :)

If you really want to fix your problems, I suggest you first add -Wall -Werror -pedantic-errors to your compiler's options (assuming you use GCC, I don't know how VC handles doing that).

Your program may compile fine without those, but with them these errors come up.

tom@midnight:/tmp$ cc -o ah ah.c -Wall -Werror -pedantic-errors
ah.c:1:1: error: C++ style comments are not allowed in ISO C90
ah.c:1:1: error: (this will be reported only once per input file)
cc1: warnings being treated as errors
ah.c: In function ‘main’:
ah.c:15: warning: unused variable ‘cAnswer’
ah.c:14: warning: unused variable ‘fCelsius’
ah.c:13: warning: unused variable ‘fFahrenheit’
ah.c: In function ‘ask’:
ah.c:49: warning: implicit declaration of function ‘isdigit’
ah.c:49: warning: suggest parentheses around && within ||
ah.c:58: warning: control reaches end of non-void function
ah.c: In function ‘calc1’:
ah.c:75: warning: suggest parentheses around && within ||
ah.c: In function ‘calc2’:
ah.c:97: warning: implicit declaration of function ‘toupper’
ah.c:97: warning: statement with no effect
ah.c:110: warning: control reaches end of non-void function



I'm not sure you understand functions completely. Every function returns something. What it returns comes before the function name.
void foo(); // Returns 'void'.
int foo(); // Returns an 'int'.

// And so on....



If you specify anything other than 'void', your function must return the proper type of data.

Also, if you want your program to loop, you're going to need to implement a 'while' loop, or use 'goto' statements. A 'switch' statement is not a loop.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1