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

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




Program Not returning to switch

 
Reply to this topicStart new topic

Program Not returning to switch, My functions need to be modularized. I am not sure I am doing it the

C-Rookie
4 Jan, 2008 - 11:43 PM
Post #1

New D.I.C Head
*

Joined: 15 Dec, 2007
Posts: 8


My Contributions
CODE
//title:week3-pkc-ver256.c
//author Pamela Crawford
/* 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
    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;
}

// 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);
        getchar();
        
    }

    // Return that choice to main
    return choice;
    return ask;
            }

// Same as calc1, matches prototype
// 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;}
}


This post has been edited by jjhaag: 4 Jan, 2008 - 11:51 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Program Not Returning To Switch
4 Jan, 2008 - 11:51 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Please post your code using code tags, like this: code.gif. It makes it way easier to both read and copy/paste. I've changed it in the above post, but remember for next time.

What problems are you having? The title is usually only used for a brief description of the issues - you need to go into a lot more detail than just a line if you're looking for helpful information. Plus, your title has been cut off because that field only holds so many characters.

Describe the problem, the expected output, the actual output (if any), any errors you're receiving on compilation, crashes and segfaults, etc. It'll make it way easier for us to help you that way.
User is offlineProfile CardPM
+Quote Post

Tom9729
RE: Program Not Returning To Switch
5 Jan, 2008 - 12:16 AM
Post #3

Debian guru
Group Icon

Joined: 30 Dec, 2007
Posts: 1,589



Thanked: 12 times
Dream Kudos: 325
My Contributions
Seems to run fine for me (after fixing an error). smile.gif

For a start, in your calc1() function you try to return two variables. Commenting out the "return ask;" line clears up the error.

Second, after a conversion your program asks if the user would like to make another conversion, or exit. However, it ignores any choice made and exits no matter what.

The other thing I found, which is comment rather than code related; your comments don't all match up to your code, which makes reading your code confusing.

Also, some of your print statements have messed up newline characters at the end (eg. The newline character is C is '\n', you have '/n').
User is online!Profile CardPM
+Quote Post

Bench
RE: Program Not Returning To Switch
5 Jan, 2008 - 04:59 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Could you please elaborate on what you mean by "returning to switch"? Are you attempting to write your program with a repeated menu/option prompt to the user? If so - the problem is that a switch statement does not naturally repeat, therefore you must write that bit yourself.

The usual technique involves putting your switch block inside some kind of loop, typically, one which uses the while keyword.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 03:18PM

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