string!

This is a discussion on string! within the C Programming forums, part of the General Programming Boards category; nid help... Code: #include<stdio.h> main() { chat num[4]; int a,b,c,d; printf("input num"); scanf("%s",num); a=num[0]; b=num[1]; c=num[2]; d=num[3]; printf("a=%d b=%d c=%d ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    string!

    nid help...

    Code:
    #include<stdio.h>
    main()
    {
    chat num[4];
    int a,b,c,d;
    printf("input num");
    scanf("%s",num);
    a=num[0];
    b=num[1];
    c=num[2];
    d=num[3];
    printf("a=%d b=%d c=%d d=%d",a,b,c,d);
    getch();
    }
    i simply input 1234
    but i dont get the values of:
    1 for a
    2 for b
    3 for c
    4 for d
    whats wrong w/ the program..
    nid ur advice...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Try subtracting '0' from all the values in printf().
    a - '0', b - '0', and so on...

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > chat num[4];
    This won't even compile.
    Paste the code you compiled, not something you just "remembered".

    Or even
    a - 'a';

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    typographical error.... chat is char.....

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    How can you make a typo, if you're supposed to be copy/pasting ?

    If you're not accurately posting the code you're actually compiling, then it's a waste of time even to try and help you.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    43
    Why not simply do this?
    Code:
    #include<stdio.h>
    
    int main()
    {
    char num[3];
    
    printf("input num");
    scanf("%s",&num);
    printf("a=%c b=%c c=%c d=%c",num[0],num[1],num[2],num[3]);
    getch();
    return(0);
    }
    This way avoids the use of additional unneeded variables.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, your array isn't big enough for what you're trying to do. For one thing, it's only 3 characters, which means there is no 'num[3]'. Furthermore, the spot 'num[2]' would be the null character, assuming you only typed 2 characters. The reason is you're using it like it's a string. In order to use it as if it were a string, you'd have to leave room for the null character. A string by definition is zero or more characters terminated by a null character.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compare structures
    By lazyme in forum C++ Programming
    Replies: 15
    Last Post: 05-28-2009, 02:40 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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