Thread: newbie array question

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    newbie array question

    I think I am having trouble converting a floating number into a character array. Can someone please help me with fixing the line I have commented. Thankyou.

    Code:
    float top;
    char price[5];
    
    printf("Enter top price");
    scanf("%f", &top);
    
    price = top;  // this line is probably wrong
    
    printf(price[0]);
    printf(price[1]);
    printf(price[2]);
    printf(price[3]);
    printf(price[4]);

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I think I am having trouble converting a floating number into a character array.
    Use sprintf.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    printf(price[0]);
    printf(price[1]);
    printf(price[2]);
    printf(price[3]);
    printf(price[4]);
    Those are all wrong also. The first argument to printf() needs to be a string, not a character.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    67
    Code:
    float top;
    float price[5];
    
    printf("Enter top price");
    for(int i=0;i<5;i++){
    
    scanf("%f",&top);
     price[i]=top;
      printf("%f",price[i]);
    I hope this helps!
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie question about 'sizeof' and arrays
    By Sharke in forum C Programming
    Replies: 27
    Last Post: 12-09-2009, 06:30 AM
  2. Embaracing Array question
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 04-06-2009, 10:29 AM
  3. Quick Question: 2D Array with char **
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 01-29-2009, 07:33 AM
  4. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  5. Replies: 5
    Last Post: 05-30-2003, 12:46 AM