Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,938 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,432 people online right now. Registration is fast and FREE... Join Now!




getting return values to the correct place

 
Reply to this topicStart new topic

getting return values to the correct place, Functions end instead of going back to choiced of what the next functi

C-Rookie
7 Jan, 2008 - 10:23 AM
Post #1

New D.I.C Head
*

Joined: 15 Dec, 2007
Posts: 8


My Contributions
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 code.gif

This post has been edited by jjhaag: 7 Jan, 2008 - 10:30 AM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Getting Return Values To The Correct Place
7 Jan, 2008 - 10:29 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,143



Thanked: 77 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Rather than initialize the variable during the assignment of it's value:
CODE

int getit = ask();


I would initialize the variable prior to assigning it.

CODE

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



Also :
code.gif
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Getting Return Values To The Correct Place
7 Jan, 2008 - 12:03 PM
Post #3

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,590



Thanked: 12 times
Dream Kudos: 325
My Contributions
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. smile.gif

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.

CODE

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.
CODE

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.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 04:27PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month