Like Tree1Likes
  • 1 Post By manasij7479

A bit of an issue, converting char into int...

This is a discussion on A bit of an issue, converting char into int... within the C Programming forums, part of the General Programming Boards category; Hello you wonderful people... I was wondering if I could get your help real quick please, and thank you... So ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    20

    A bit of an issue, converting char into int...

    Hello you wonderful people...

    I was wondering if I could get your help real quick please, and thank you...

    So I want to write a code for an assignment to validate an ISBN number.

    So once the user inputs the number, I need to take each digit and multiply it by a predetermined weight, but since I am using a string array for the input, I am having a bit of a problem. The program runs, but gives me the wrong results...

    Can you please help me to figure out how to convert a char into int. I have used atoi, but that converts the entire input rather than digit by digit.

    Thanks a bunch

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    main(void)
    {
        char isbn[80];
        int len, n,  weight  ;
        int weighted_value;
        int counter=0;
    
        printf("please enter the ISBN number: ");
        scanf("%s", isbn);
    
        printf("the first number is %c\n", isbn[0]);
        printf("the second number is %c\n", isbn[1]);
        printf("the third number is %c\n", isbn[2]);
        printf("the fourth number is %c\n", isbn[3]);
        printf("the fifth number is %c\n", isbn[4]);
        printf("the sixth number is %c\n", isbn[5]);
        printf("the seventh number is %c\n", isbn[6]);
        printf("the eighth number is %c\n", isbn[7]);
        printf("the nineth number is %c\n", isbn[8]);
        printf("the tenth number is %c\n", isbn[9]);
    
        len = strlen(isbn);
    
        if (len <= 10)
        {
            printf("valid\n");
            //while (counter!=10)
                //{
                    weight = 10;
       
                    //HERE IS WHERE I NEED THE HELP, THANKS//
    
                    n=atoi(isbn[0]);
    
                    weighted_value=n*weight;
    
                    printf("the n value for the first number is %d\n", n);
    
    
                    printf("the weighted value for the first number is %d\n", weighted_value);
                //}
        }
    
    }

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Like this, if I understand your question correctly.
    Code:
    n  = (int)(isbn[counter] - '0');
    c_dever likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    20
    ok so basically say I input the number "1234567890"

    what I want to do is take 1 and multiply it by 10 and get a value

    so I need to convert the input digit by digit into an int so I can multiply it by the predetermined weight that I will set in my code, in this case being 10

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    20
    that worked perfectly... thank you so much

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    20
    but just so I understand what you did, do you mind explaining the code to me? so I can learn and not just copy it? thank you so much

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    The individual digits in the character array are represented by their ascii ( or utf-8..whatever) values.
    In the ascii table, the numbers are placed next to each other....so subtracting the character value of zero from the value of a single digit number, will give that number back.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    20
    sweet... thank you sir. Greatly appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting char to char*, strcpy problems
    By Gareth321 in forum C Programming
    Replies: 9
    Last Post: 05-24-2011, 12:23 AM
  2. float/int issue if converting C to F
    By XodoX in forum C++ Programming
    Replies: 13
    Last Post: 02-16-2009, 01:03 AM
  3. Converting unsigned char[] to char[]
    By Maragato in forum C Programming
    Replies: 8
    Last Post: 09-04-2006, 08:24 PM
  4. Converting from type char to const char*
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 05-21-2003, 09:51 PM
  5. Converting const char to char array
    By HomerJ in forum C++ Programming
    Replies: 4
    Last Post: 04-30-2002, 03:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21