Thread: From int to int

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    15

    From int to int

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int getInput(int userInput);
    
    int main()
    {
        int userInput;
        
        getInput(&userInput);
        printf("%d", userInput);
        
        system ("pause");
        return 0;
    }
    
    int getInput(int userInput)
    {
         char whichShape;
    
         printf("P: Display a parallelogram\n");
         printf("R: Display a rectangle\n");
         printf("S: Display a square\n");
         printf("T: Display a right-trangle\n");
         printf("X: Exit program\n");
         printf("Enter your choice\n");
         whichShape = getchar ();
         if (whichShape == 'P' || whichShape == 'p')
     {
         userInput = 1;
     }
         else if (whichShape == 'R' || whichShape == 'r')
     {
         userInput = 2;
     }
         else if (whichShape == 'S' || whichShape == 's')
     {
         userInput = 3;
     }
         else if (whichShape == 'T' || whichShape == 't')
     {
         userInput = 4;
     }
         else if (whichShape == 'X' || whichShape == 'x')
     {
         userInput = 5;
     }
    }
    I get invalid conversnio from int to int at line 10. I do not understand why

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Read the error message again. It probably says invalid conversion from int * to int.

    The problem is, getInput() is expecting an int, but you're passing &userInput (the address of an int, or an int *) to it.

    Also, your getInput() is declared to return an int, but you're not returning anything. You need to figure out if you want to design your getInput() function to modify userInput via a pointer, or if you want to modify it in the caller by using getInput()'s return value. Once you've figured out the design, work on the implementation.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    15
    Oh yea, I meant to include the return statement. I just put
    Code:
     return userInput;
    after the if statements. and the error was int* to int. I still do not understand why. i call the function in main and i want it to give me the value of userInput i thought that is why i need the &userInput.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The reason you're getting the error is because the types don't match. Here's a quick primer on the design choices for you...

    Method 1 - Pointer:
    Code:
    void caller()
    {
      int num = 6;
      getNum(&num);
      printf("%d\n", num);
    }
    
    void getNum(int *num)
    {
      *num /= 2;
    }
    Method 2 - Return Value:
    Code:
    void caller()
    {
      int num = 6;
      num = getNum(num);
      printf("%d\n", num);
    }
    
    int getNum(int num)
    {
      return num / 2;
    }
    Both versions should print "3", but you can see the difference between the methods of getting that value to the caller.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int getInput(int userInput)
    {
         char whichShape;
    
         printf("P: Display a parallelogram\n");
         printf("R: Display a rectangle\n");
         printf("S: Display a square\n");
         printf("T: Display a right-trangle\n");
         printf("X: Exit program\n");
         printf("Enter your choice\n");
         whichShape = getchar ();
    Bit of a nit in this particular case but... getchar returns an int, not a char.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting Issue?
    By matthewlane in forum C Programming
    Replies: 1
    Last Post: 11-15-2010, 05:55 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM