Thread: function problems

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    21

    function problems

    I need to write a program where i write a function to ask a user to input a number, then i need to write 3 other functions one multiplying it by 2, the other multiplying it by 3 and the other multiplying it by 4. All the while using only one variable name fVal. (This is to teach me to get used to the scope of variables). And I can only use fVal in the functions and the main program. I have tried over and over, but I always end up changing the value of fVal and i get fVal * 2 then that times 3 then that answer times 4. Which is wrong, please help! Thanks!

  2. #2
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    so this is wrong?
    Code:
    #include <stdio.h>
    
    int get_number() {
        int number = 0;
        
        printf("enter a number\n");
        scanf("%d", &number);
        
        return number;
    }
    
    int by2(int number) {
        return (number * 2);
    }
    
    int by3(int number) {
        return (number * 3);
    }
    
    int by4(int number) {
        return (number * 4);
    }
    
    int main() {
        int fVal;
        
        fVal = get_number();
        fVal = by2(fVal);
        fVal = by3(fVal);
        fVal = by4(fVal);
        
        printf("\nfVal now equals %i\n", fVal);
    
        return 0;
    }
    you just want to print the changes and not keep them? then you could replace my returns with printfs. im not sure i get it.

    <edit>
    it took almost a full min for the submit button to actually submit.
    Last edited by mart_man00; 07-12-2003 at 12:51 AM.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    21
    This is what I've tried. My only variable is fVal, and if I entered 5 for fVal when running the program i get this. fVal = 10, fVal = 30, fVal = 120. But it should be 10, 15, 20. See what I mean? If you have more help I'd appreciate it. Thanks!

    Code:
    #include <stdio.h>
    
    float GetValue()
    {   
        float fVal;
        printf("Enter a value for fVal: ");
        scanf("%f", &fVal); 
        return (fVal);	
    }
    
    void Compute1(float fVal)
    {   
        
        fVal = fVal * 2;
        printf("fVal * 2: %f\n", fVal);
        
    }
    
    void Compute2(float fVal)
    {
        
        fVal = fVal * 3;
        printf("fVal * 3: %f\n", fVal);
        
    }
    
    void Compute3(float fVal)
    {
        
        fVal = fVal * 4;
        printf("fVal * 4: %f\n", fVal);
        
    } 
    
    int main(void)
    {
    
    
    float GetValue();
    void Compute1 ();
    void Compute2 ();
    void Compute3 ();
     
       
        
         float fVal = GetValue();
         Compute1();
         Compute2();
         Compute3();
    
    return(0);
    
    }

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Each of the ComputeX() functions takes a float argument, which you have not passed to it. Normally the compiler would complain, but you've also provided overloaded declarations inside main which do not take parameters.

    Code:
    int main(void)
    {
    
         float fVal = GetValue();
         Compute1(fVal);
         Compute2(fVal);
         Compute3(fVal);
    
    return(0);
    
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    18
    As easy as this, if I understood well:

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
    int FVal;

    printf("Input a number: ");
    scanf("%d", &FVal);
    printf("Number = %d \n Multiplied by two = %d \n Multiplied by three = %d \n Multiplied by four = %d \n", FVal, FVal*2, FVal*3, FVal*4);
    system("PAUSE");
    return 0;
    }

    it returns, for example, is you input "5":

    Number = 5
    Multiplied by two = 10
    Multiplied by three = 15
    Multiplied by four = 20

    Pretty simple huh?
    Last edited by Slaught; 07-13-2003 at 06:15 PM.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    21
    thanks for all the help guys, i got it working, as usual it was a simple mistake i was making.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Problems with str.replace function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 03:35 AM