Thread: help me! return a string

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    6

    help me! return a string

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    int menu();
    char get_item(int choice);
    float get_price(int choice);
    float get_pay(int qty,float price);
    void display(char , char , float, int, float);
    
    int main()
    {
        char name;
        int item,choice,qty;
        float price,payment;
        menu();
        display(name,item ,price,qty,payment);
        item=get_item(choice);
        price=get_price(choice);
        payment=get_pay(qty,price);
        getche(); 
    }
    
    
    int menu()
    {
      char name[20];
      int item,choice,qty;
      float payment,price;
    
            printf("\n----------------------------------------------------");
            printf("\n-           WELCOME TO MYCAR ACCESORIES            -");
            printf("\n----------------------------------------------------");
            printf("\n1.  Pioneer MP3 Player    [ $ 350.00 ]");
            printf("\n2.  MOMO Sports Steering  [ $ 770.00 ]");
            printf("\n3.  MOMO Gear Knob        [ $ 150.00 ]");     
            printf("\nEnter name      : ");
            gets(name);
            printf("Enter choice    : ");
            scanf("%d", &choice);
            printf("Enter quantity  : ");
            scanf("%d", &qty);
    }
    
    char get_item(int choice)
    {
         int item[30];
         switch (choice) 
        {
            case 1: strcpy(item" Pioneer MP3 Player");
                       break;
            case 2: strcpy(item" MOMO Sports Steering");
                       break;
            case 3: strcpy(item" MOMO Gear Knob");
                       break;
    	default:
                      strcpy(item"Error! Try again please\n");
         }return item;
    }
    
    float get_price(int choice)
    {
        	float price;
    
    	switch (choice) 
        {
            case 1: price = 350.00;
                       break;
            case 2: price = 770.00;
                       break;
            case 3: price = 150.00;
                       break;
    	    default:price=0;
         }
      return price;
    
    }
    float get_pay(int qty,float price)
    {
    	float payment;
    	
    	payment = qty * price;
    
    	return payment;
    }
    
    	
    
    void display(char name,char item , float price, int qty, float payment)
    {
    	printf("\nName                : %s", name);
    	printf("\nItem                : %s", item);
    	printf("\nPrice               : RM %.2f", price);
    	printf("\nQuantity            : %d", qty);
    	printf("\nPayment             : RM %.2f\n\n", payment);
    }
    i cant get it to run..help please..i'm a newbie here

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As long as you've got conio.h and gets in your program, no one here will be able to compile it; so if you want help with the errors you'll have to tell us what the errors are.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Code:
    char get_item(int choice)
    {
         int item[30];
         switch (choice) 
        {
            case 1: strcpy(item" Pioneer MP3 Player");
                       break;
            case 2: strcpy(item" MOMO Sports Steering");
                       break;
            case 3: strcpy(item" MOMO Gear Knob");
                       break;
    	default:
                      strcpy(item"Error! Try again please\n");
         }return item;
    }
    i'm stuck here...i cant return it...

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    Bangalore,India
    Posts
    24
    Quote Originally Posted by nawzays View Post
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    int menu();
    char get_item(int choice);
    float get_price(int choice);
    float get_pay(int qty,float price);
    void display(char , char , float, int, float);
    
    int main()
    {
        char name;
        int item,choice,qty;
        float price,payment;
        menu();
        display(name,item ,price,qty,payment);//values in any function wont affect these variables
        item=get_item(choice);
        price=get_price(choice);
        payment=get_pay(qty,price);
        getche(); 
    //return 0; missing here
    }
    
    
    int menu()
    {
      char name[20];
      int item,choice,qty;
      float payment,price;
    
            printf("\n----------------------------------------------------");
            printf("\n-           WELCOME TO MYCAR ACCESORIES            -");
            printf("\n----------------------------------------------------");
            printf("\n1.  Pioneer MP3 Player    [ $ 350.00 ]");
            printf("\n2.  MOMO Sports Steering  [ $ 770.00 ]");
            printf("\n3.  MOMO Gear Knob        [ $ 150.00 ]");     
            printf("\nEnter name      : ");
            gets(name);
            printf("Enter choice    : ");
            scanf("%d", &choice);
            printf("Enter quantity  : ");
            scanf("%d", &qty);
    }
    
    char get_item(int choice)
    {
         int item[30];
         switch (choice) 
        {
            case 1: strcpy(item," Pioneer MP3 Player");
                       break;
            case 2: strcpy(item," MOMO Sports Steering");
                       break;
            case 3: strcpy(item," MOMO Gear Knob");
                       break;
    	default:
                      strcpy(item,"Error! Try again please\n");
         }return item;
    }
    
    float get_price(int choice)
    {
        	float price;
    
    	switch (choice) 
        {
            case 1: price = 350.00;
                       break;
            case 2: price = 770.00;
                       break;
            case 3: price = 150.00;
                       break;
    	    default:price=0;
         }
      return price;
    
    }
    float get_pay(int qty,float price)
    {
    	float payment;
    	
    	payment = qty * price;
    
    	return payment;
    }
    
    	
    
    void display(char name,char item , float price, int qty, float payment)
    {
    	printf("\nName                : %s", name);
    	printf("\nItem                : %s", item);
    	printf("\nPrice               : RM %.2f", price);
    	printf("\nQuantity            : %d", qty);
    	printf("\nPayment             : RM %.2f\n\n", payment);
    }
    i cant get it to run..help please..i'm a newbie here
    there are too many mistakes in the code...all the variables are local to the functions which do not affect the local variables of main from which u are trying to get price,item,etc...even display wont work.....the item is declared and used as atleast 3 different datatypes...
    Last edited by subhash.rao; 08-16-2011 at 06:23 AM.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    int menu();
    char get_item(int choice);
    float get_price(int choice);
    float get_pay(int qty,float price);
    void display(char , char , float, int, float);
    
    int main()
    {
        char name,item;
        int choice,qty;
        float price,payment;
        
     	menu();
        display(name,item ,price,qty,payment);
    
        getche(); 
        return 0;
    }
    
    int menu()
    {
      char  item,name[20];
      int choice,qty;
      float payment,price;
    
            printf("\n----------------------------------------------------");
            printf("\n-           WELCOME TO MYCAR ACCESORIES            -");
            printf("\n----------------------------------------------------");
            printf("\n1.  Pioneer MP3 Player    [ $ 350.00 ]");
            printf("\n2.  MOMO Sports Steering  [ $ 770.00 ]");
            printf("\n3.  MOMO Gear Knob        [ $ 150.00 ]");     
            printf("\nEnter name      : ");
            gets(name);
            printf("Enter choice    : ");
            scanf("%d", &choice);
            printf("Enter quantity  : ");
            scanf("%d", &qty);
            
             item=get_item(choice);
             price=get_price(choice);
             payment=get_pay(qty,price);
             
    }
    char get_item(int choice)
    {
    	char item[30];
    	
    	switch (choice) 
        {
            case 1: strcpy(item, " Pioneer MP3 Player");
              break;
            case 2: strcpy(item, " MOMO Sports Steering");
              break;
            case 3: strcpy(item, " MOMO Gear Knob");
              break;
    	    default:
              strcpy(item,"Error! Try again please");break;
         }
         return item;
    }
    
    float get_price(int choice)
    {
        
     	float price;
    
    	switch (choice) 
        {
            case 1: price = 350.00;
              break;
            case 2: price = 770.00;
              break;
            case 3: price = 150.00;
              break;
    	    default:
              price=0;
         }
      return price;
    
    }
    float get_pay(int qty,float price)
    {
    	float payment;
    	
    	payment = qty * price;
    
    	return payment;
    }
    
    	
    
    void display(char name,char item, float price, int qty, float payment)
    {
    	printf("\nName                : %s", name);
    	printf("\nItem                : %s", item);
    	printf("\nPrice               : RM %.2f", price);
    	printf("\nQuantity            : %d", qty);
    	printf("\nPayment             : RM %.2f\n\n", payment);
    }
    i repaired it but still wont work.. :'(

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    it say in function char get_item
    invalid conversion from 'char*' to 'char'

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    now your code seems a little more formatted you might get some other replies ;->

    You forgot ',' in the strcpy for one thing
    perhaps have a look at the following,
    Also perhaps make an enum list to label your switch cases

    Code:
    enum ITEM_LIST 
    {
        PIONEER_MP3,
        MOMO_SPT_STEERING,
        MOMO_GEARKNOB,
    };
    
    
    void GetItem(int choice, char item[30]);
    
    int main()
    {
        char item[30];  //fixed size remember....
        int choice = 0;
        
        //get user input for choice (checking for invalid input of course... ;-> )
        //....
        GetItem(choice, item);
        
        //output your results....whatever
        
        return 0;
    }
    
    void GetItem(int choice, char item[30])
    {
        
        switch (choice) 
        {
            case PIONEER_MP3: 
                strcpy(item, "Pioneer MP3Player");
            break;
            case MOMO_SPT_STEERING: 
                strcpy(item, "MOMO Sports Steering");
            break;
            case MOMO_GEARKNOB: 
                strcpy(item, "MOMO Gear Knob");
            break;
            
            default:
            strcpy(item, "Error! Try again please\n");
        }
         
    }
    ??code in tags seems to be losing formatting then not, probably this browser...
    Last edited by rogster001; 08-16-2011 at 09:48 AM. Reason: aieeee! why is the formatting gone nuts
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  8. #8
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by nawzays View Post
    it say in function char get_item
    invalid conversion from 'char*' to 'char'
    item is a char array and you are returning a char. What you can do is, pass a char array into the function and its size. Then input the value directly into it, possibly checking that it doesn't exceed the size.
    Then correct the other problems in the code, if there.

    [Edit: Couldn't we get a mutex for the last reply being posted ?!]

    Quote Originally Posted by rogster001
    ??code in tags seems to be losing formatting then not, probably this browser...
    Happens to me too. More often than not when using a slow connection and when the Edit icons in the Advanced page doesn't appear.
    Last edited by manasij7479; 08-16-2011 at 07:25 AM.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    can someone modified my answer?

    the question ask me to call menu,get_item,get_price,payment,and display....isk3..i quite blur in c programming...sorry guys..

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I won't fix it for you but here's one reason it's not working....
    Code:
    char get_item(int choice)
    {
    	char item[30];
    	
    	switch (choice) 
        {
            case 1: strcpy(item, " Pioneer MP3 Player");
              break;
            case 2: strcpy(item, " MOMO Sports Steering");
              break;
            case 3: strcpy(item, " MOMO Gear Knob");
              break;
    	    default:
              strcpy(item,"Error! Try again please");break;
         }
         return item;
    }
    Ok inside this function you declare a char array item[]. This exists locally to your function and is destroyed when the function exits. But... you are attempting to return a pointer to it at the end of the function. By the time your next function tries to read the string, it's long gone.

    This is about "scope" (look it up in your text books) basically anything after the opening brace only lasts until the matching closing brace...
    Code:
    // variable not available
    
    { 
       int variable;
       // here variable is useable
    }
    // variable not available
    Now... take a good look at your code with this in mind.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And to add to what Tater just said: when you return something from a function, a copy of that thing is made and that copy goes back to whoever called the function. For a simple data type, that's all that is needed. For arrays, however, the array name by itself refers to the location of the first element (in memory), and when you return that array a copy of that address (not the data actually in the array) is made and returned. And then, when that array goes out of scope (as above), that address is no longer useful.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Here's a small sample program... type it up and compile it.... Try it with and without the static keyword...
    This is why you should never try to return an array from a function. And yes "char string[20]" is an array.
    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    thank to all of guys....tahnk you so much.u guys help alot

  14. #14
    1337
    Join Date
    Jul 2008
    Posts
    135
    Code:
    char get_item(int choice)
    {
    	char item[30];
    	
    	switch (choice) 
        {
            case 1: strcpy(item, " Pioneer MP3 Player");
              break;
            case 2: strcpy(item, " MOMO Sports Steering");
              break;
            case 3: strcpy(item, " MOMO Gear Knob");
              break;
    	    default:
              strcpy(item,"Error! Try again please");break;
         }
         return item;
    }
    To return a string, do this:
    Code:
    char* get_item(int choice)
    {
    	char item[30];
    	
    	switch (choice) 
        {
            case 1: strcpy(item, " Pioneer MP3 Player");
              break;
            case 2: strcpy(item, " MOMO Sports Steering");
              break;
            case 3: strcpy(item, " MOMO Gear Knob");
              break;
    	    default:
              strcpy(item,"Error! Try again please");break;
         }
         return item;
    }

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Did you miss all the other posts about this topic!?! You cannot, I repeat, cannot return a pointer to a local variable, this includes arrays. The variable goes out of scope when the funtion ends, hence the pointer (like your solution) is useless.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return a String
    By jimlau in forum C++ Programming
    Replies: 11
    Last Post: 07-24-2010, 06:52 PM
  2. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  3. Replies: 4
    Last Post: 01-30-2006, 05:04 AM
  4. Return value string
    By Garfield in forum C Programming
    Replies: 14
    Last Post: 09-14-2001, 11:33 AM