GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Feb-2016, 16:57
budgie1980 budgie1980 is offline
New Member
 
Join Date: Feb 2016
Posts: 6
budgie1980 is on a distinguished road

Need help creating and calling a function


Hey all, just wanted to say thanks for your help last time. Thought you might be able to help me out again. I'm trying to write a program that will ask the user to enter their weight, then ask them to enter a number representing a planet. Then it should send those two inputs to a separate .c file containing a switch statement to calculate their new weight, and then send the new weight back to the main program. here is what I have so far as my main program, it works as is:

CPP / C++ / C Code:
#include <stdio.h>

int main(void)
{
        float input;
        int planetnum;

        printf("\n\nEnter your weight in pounds:   ");
        scanf("%f", &input);

        printf("\nWhich planet do you want to see how much you would weigh on?\n");
        printf("Enter 1-8\n\n");
        printf("1: Mercury\n");
        printf("2: Venus\n");
        printf("3: Mars\n");
        printf("4: Jupiter\n");
        printf("5: Saturn\n");
        printf("6: Uranus\n");
        printf("7: Neptune\n");
        printf("8: The Moon\n\n");
        scanf("%i", &planetnum);

        while(planetnum < 1 || planetnum > 8)
        {
                printf("\nInvaild selection, Please enter (1-8):  ");
                scanf("%i", &planetnum);
        }
        return 0;
}

This is the function I need to send my two numbers to.

CPP / C++ / C Code:
#include <stdio.h>

int main(void)
{
        float input;
        int planetnum;

        switch(planetnum)
        {
                case 1: printf("On Mercury, you would weigh %.2f lbs!\n", input * 0.284);
                        break;
                case 2: printf("On Venus, you would weigh %.2f lbs!\n", input * 0.907);
                        break;
                case 3: printf("On Mars, you would weigh %.2f lbs!\n", input * 0.38);
                        break;
                case 4: printf("On Jupiter, you would weigh %.2f lbs!\n", input * 2.34);
                        break;
                case 5: printf("On Saturn, you would weigh %.2f lbs!\n", input * 0.925);
                        break;
                case 6: printf("On Uranus, you would weigh %.2f lbs!\n", input * 0.795);
                        break;
                case 7: printf("On Neptune, you would weigh %.2f lbs!\n", input * 1.125);
                        break;
                default: printf("On the Moon, you would weigh %.2f lbs!\n", input * 0.166);
                        break;
        }
        return 0;
}

pretty sure the switch statement is right, but not sure how to make it a function. any help would be appreciated. Thanks for your time.
  #2  
Old 01-Mar-2016, 22:23
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,511
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: Need help creating and calling a function


Sounds like two questions?
1. a new function ?
2. a 'separate' C file ?

Anyway, know that with any C [or C++] program that there is one, and only one, main function for the entire program.

Since it appears that the focus is on 1, then where you want to send the inputs/parameters/arugments from main to the 'new' function -- however you perceive what gets 'passed' to the function.

The new function, see the code below main(). Then to call that new function from main(), might look like this: (see the prototype before main() and see just before the return)
CPP / C++ / C Code:
#include <stdio.h>

/* Prototype for the new function. (May not be needed if you have another .h file with this info)  */
void getPlanetWeight( float, int ); 

int main(void)
{
        float input;
        int planetnum;

        printf("\n\nEnter your weight in pounds:   ");
        scanf("%f", &input);

        printf("\nWhich planet do you want to see how much you would weigh on?\n");
        printf("Enter 1-8\n\n");
        printf("1: Mercury\n");
        printf("2: Venus\n");
        printf("3: Mars\n");
        printf("4: Jupiter\n");
        printf("5: Saturn\n");
        printf("6: Uranus\n");
        printf("7: Neptune\n");
        printf("8: The Moon\n\n");
        scanf("%i", &planetnum);

        while(planetnum < 1 || planetnum > 8)
        {
                printf("\nInvaild selection, Please enter (1-8):  ");
                scanf("%i", &planetnum);
        }

        getPlanetWeight( input, planetnum );  /* the new function call. */

        return 0;
}

/* The inputs/parameters/arguments, are 'passed' TO the function here. These should match the data type(s) from main. */
void getPlanetWeight( float input, int planetnum ) 
{
/*        float input;   */   /* These are now 'passed' as arguments, as shown above. */
/*        int planetnum; */

        switch(planetnum)
        {
                case 1: printf("On Mercury, you would weigh %.2f lbs!\n", input * 0.284);
                        break;
                case 2: printf("On Venus, you would weigh %.2f lbs!\n", input * 0.907);
                        break;
                case 3: printf("On Mars, you would weigh %.2f lbs!\n", input * 0.38);
                        break;
                case 4: printf("On Jupiter, you would weigh %.2f lbs!\n", input * 2.34);
                        break;
                case 5: printf("On Saturn, you would weigh %.2f lbs!\n", input * 0.925);
                        break;
                case 6: printf("On Uranus, you would weigh %.2f lbs!\n", input * 0.795);
                        break;
                case 7: printf("On Neptune, you would weigh %.2f lbs!\n", input * 1.125);
                        break;
                default: printf("On the Moon, you would weigh %.2f lbs!\n", input * 0.166);
                        break;
        }
}
HTH -- at least for the 'new' function.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with the function creating updating a linked list nkhambal C Programming Language 3 28-Oct-2004 20:45

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:58.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.