Thread: converting a string to an array

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by curlyfries View Post
    char main(void) <-- horrible!
    Code:
    int main()
    {
    int i,num[5];
    printf("Enter a 5 digit number\n");
    printf("XXXXX\n");
    scanf("%1d%1d%1d%1d%1d",&num[0],&num[1],&num[2],&num[3],&num[4]);
    for( i=0;i<5;++i )
      printf("\nnum[%d]=%d %s",i,num[i],num[i]>=0&&num[i]<=9?"OK":"error");
    return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BillyTKid View Post
    Code:
    int main()
    {
    int i,num[5];
    printf("Enter a 5 digit number\n");
    printf("XXXXX\n");
    scanf("%1d%1d%1d%1d%1d",&num[0],&num[1],&num[2],&num[3],&num[4]);
    for( i=0;i<5;++i )
      printf("\nnum[%d]=%d %s",i,num[i],num[i]>=0&&num[i]<=9?"OK":"error");
    return 0;
    }
    I think you missed the point of the exercise...
    To convert a numeric string to an array of integers...

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    I think you missed the point of the exercise...

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by BillyTKid View Post
    I think you missed the point of the exercise...
    this is an example of what I want...

    user inputs 14364 as a string (scanf("%s",number))

    The number then gets stored in an array like this

    int number[5];

    number[0]=1
    number[1]=4
    number[2]=3
    number[3]=6
    number[4]=4
    Nope, don't think so.

  5. #5
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198
    I see laserlight already gave her critique which is correct as always...

    A couple of things...
    Per K & R 2nd ed., "It's bad practice to bury 'magic numbers' in a program; they
    convey little information to someone who might have to read the program later, and
    they are hard to change in a systematic way."

    One suggestion given, in dealing with "magic numbers" is to give them meaningful
    names for example, using a #define statement.

    I believe the above is the correct concept, not to be taken lightly.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Char*Pntr View Post
    Per K & R 2nd ed., "It's bad practice to bury 'magic numbers' in a program; they
    convey little information to someone who might have to read the program later, and
    they are hard to change in a systematic way."

    One suggestion given, in dealing with "magic numbers" is to give them meaningful
    names for example, using a #define statement.

    I believe the above is the correct concept, not to be taken lightly.
    Yep... #define is the way to do things in that case.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Converting to expression to string of array elements
    By Sailors in forum C Programming
    Replies: 12
    Last Post: 07-26-2007, 03:01 PM
  5. help converting a char array to a string object?
    By axlton in forum C++ Programming
    Replies: 4
    Last Post: 07-25-2003, 01:34 PM