Thread: Help with functions

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    13

    Help with functions

    This is part of my instructions and what i'm trying to code at the moment...

    · To accelerate slowly, the user presses ‘a’. This will increase the speed by 3mph.
    · To accelerate quickly, the user presses ‘A’. This will increase the speed by 10mph.

    // I have not tried reading in the 10mph yet... just the 3mph...

    Code:
    #include <stdio.h> 
    #include <math.h>
    
    
    
    
    int increase(int z, int y)
    {
        
      printf("|---------------------|\n");
      printf("|   %d                      |\n", total=total+num);
      printf("|                             |\n");
      printf("|                             |\n");
      printf("|---------------------|\n");
      return z=z+y;
      
      
    }
    
    
    int main(){
    
    
    int i=0;
    int num=3;
    int total=0;
    char number;
    
    
    printf("\nEnter in an a to increase by 3 mph ,or a capital A to increase by 10mph!");
    number=getch();
    
    
    
    
    
    
        while(i<1){
        switch (number)
        case 'A':
        case 'a': 
        int increase(total=total+num));
        break;
        i++;}
        
        
        
        getch();
        return(0);
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Code:
    int increase(int z, int y)
    {
         
      printf("|---------------------|\n");
      printf("|   %d                      |\n", total=total+num); //Ask yourself how this function knows what total and num are?
      printf("|                             |\n");
      printf("|                             |\n");
      printf("|---------------------|\n");
      return z=z+y; 
       
    
    int main(){
    
    
    int i=0;
    int num=3;
    int total=0;
    char number;
    
    
    printf("\nEnter in an a to increase by 3 mph ,or a capital A to increase by 10mph!");
    number=getch();
    
    
    
    
    
    
        while(i<1){
        switch (number)
        case 'A':
        case 'a': 
        int increase(total=total+num)); //Look at your function definition and compare the parameters with what you are passing on this line. You can also drop the int from the function on this line.
        break;
        i++;}
        
        
        
        getch();
        return(0);
    }
       
    }
    I suggest you read up on the difference between function definition and actually calling the function.

    http://www.cprogramming.com/tutorial/c/lesson4.html

  3. #3
    Registered User Suhasa010's Avatar
    Join Date
    Sep 2013
    Posts
    10
    first thing is that neither you have declared total and num variables globally nor you have passed it to increase(), second thing is that you have 2 parameters in the increase() function while you are passing only 1 parameter i.e total from main().Third thing is that, in your main() function you have called "int increase(total=total+num)", where int is not needed. And there's no variable to hold the returned value from increase().And no idea why you included math.h.That's all.Please try to rectify these small errors.

  4. #4
    Registered User
    Join Date
    Oct 2013
    Posts
    13
    This is what i have now...

    Code:
    #include <stdio.h> 
    
    
    
    
    
    
    int increase(int total, int num)
    {
        
        
      printf("|---------------------|\n");
      printf("|   %d                |\n", total,num);
      printf("|                     |\n");
      printf("|                     |\n");
      printf("|---------------------|\n");
      
      
      
    }
    
    
    int main(){
    
    
    int i=0;
    int num=3;
    int total=0;
    char number;
    
    
    printf("\nEnter in an a to increase by 3 mph ,or a capital A to increase by 10mph!");
    number=getch();
    
    
    
    
    
    
        while(i<5){
        switch (number)
        case 'A':
        case 'a': 
        scanf("%d",increase(total,num)); // I'm trying to get total and num to read in for int total and int num in my prototype function... and have my function add 3 mph every time i press the a key... 
        total=total+num;
        break;
        i++;}
        
        
        
        getch();
        return(0);
    }
    Last edited by aron10197; 10-13-2013 at 12:57 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    where your increase function does something with its parameters?
    Where it returns result for using in the main?

    Did you compile this code? you should get at least warnings with decent compiler
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    13
    It doesn't give me any warnings or errors (using dev c++ bloodshed)

    i changed up my prototype function to this...
    Code:
    int increase(int total, int num)
    {
    
    
      printf("|---------------------|\n");
      printf("|   %d                |\n", total,num);
      printf("|                     |\n");
      printf("|                     |\n");
      printf("|---------------------|\n");
      return total=total+num; // I'm trying to get my function to add 3mph every time i press the a key... this should do it, right?
    
    
    
    }
    

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    You should use the return value to store in your total variable in main.

    ex.) total = increase(total, num);

    This way the value of total will change. You have your scanf function wrong, technically you do not need the scanf for this to work. What are you planning on doing with scanf?

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by aron10197 View Post
    It doesn't give me any warnings or errors (using dev c++ bloodshed)
    As I understand dev c++ is IDE and if you are using gcc compiler - you need to increase the warning level

    Code:
    gcc -c -o obj/test.o src/test.c -Wall -pedantic -march=core2 -Iinclude -std=c99
    src/test.c: In function ‘increase’:
    src/test.c:13:3: warning: too many arguments for format [-Wformat-extra-args]
    src/test.c: In function ‘main’:
    src/test.c:33:1: warning: implicit declaration of function ‘getch’ [-Wimplicit-function-declaration]
    src/test.c:44:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
    src/test.c: In function ‘increase’:
    src/test.c:20:1: warning: control reaches end of non-void function [-Wreturn-type]
    gcc -o test obj/test.o -Wall -pedantic -march=core2 -Iinclude -std=c99 -lm
    obj/test.o: In function `main':
    test.c:(.text+0x82): undefined reference to `getch'
    test.c:(.text+0xcf): undefined reference to `getch'
    collect2: ld returned 1 exit status
    make: *** [test] Error 1
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Oct 2013
    Posts
    13
    I'm just trying to get my prototype function to increase by 3mph every time... i thought having the scanf func in main would make it repeat the total=total+num assignment operator... scanf("%d");... every time i put in a it would add 3mph... or should it be %c

  10. #10
    Registered User
    Join Date
    Oct 2013
    Posts
    13
    Can you give me an example of how it should look with this instruction:

    To accelerate slowly, the user presses ‘a’. This will increase the speed by 3mph.

  11. #11
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Pseudo code
    Code:
    print(To accelerate slowly, the user presses ‘a’. This will increase the speed by 3mph.)
    while(i<5)
    scan number
    switch
    case a: total = increase (total,num)
    ..
    ...

  12. #12
    Registered User
    Join Date
    Oct 2013
    Posts
    13
    see, i didn't even know you could put anything front of the case a:

    Thank you so much!

  13. #13
    Registered User
    Join Date
    Oct 2013
    Posts
    13
    print(To accelerate slowly, the user presses ‘a’. This will increase the speed by 3mph.)
    while(i<5)
    scan number
    switch
    case a: total = increase (total,num)

    are you saying to put in scanf("%d") for scan number or scan (number);

  14. #14
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Its just pseudo code for scanf function

  15. #15
    Registered User
    Join Date
    Oct 2013
    Posts
    13
    Code:
    #include <stdio.h> 
    
    
    
    
    
    
    int increase(int total, int num)
    {
        
        
      printf("|---------------------|\n");
      printf("|   %d                |\n", total);
      printf("|                     |\n");
      printf("|                     |\n");
      printf("|---------------------|\n");
      return;
      
      
      
      
    }
    
    
    int main(){
    
    
    int i=0;
    int num=3;
    int total=0;
    char number;
    
    
    
    
    
    
    
    
    
    
        printf("\nEnter in an a to increase by 3 mph ,or a capital A to increase by 10mph!");
        while(i<5){
        scanf("%d",&num);
        switch (number)
        case 'a':total = increase (total,num);
        break;
        i++;}
        
        
        
        getch();
        return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-27-2013, 06:43 PM
  2. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  3. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread