Thread: String w/number and chars...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    44

    String w/number and chars...

    Hmmm... I seem to be stuck. I'm using structures and I need to get user Adress and user Postal Code...

    For address how do I stop current string/start new one with one space? Like 123 Fake street... I want to store 123 and Fake in a different var (int, and char), because I can't really store them any other way.

    And my second question is the postal code... if any of you are from canada you'll know the postal code is 6 digit and has letters and numbers... example: M2C5G8.... that's fake but thats how it works- letter number letter, number letter number. How do I go about inputting this? Since it can't really have any spaces...

    Please help... TIA.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Just store the postal code in an array.
    Use fgets() to get the input.

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Store them in a char array. The address 123 isn't an integer. They're actually characters also.

    For example the binary value 00000001 would be interpreted differently depending on it's data type being a character or an integer. Take a look at an ascii table for more info.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    printf("\nEnter Adress(IE, if your adress was\n123 Fake street, type in 123 Fake street).");
    scanf("%s", person[x].adressno);

    So then would the above be valid? I don't want to use fgets because I haven't learned that yet. And then if I wanted to print out the same thing, like "The address of person 1 is 123 fake street" then could I use the same thing?

    printf("%s", person[1].address);

    Thanks... =0)

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Two things:

    1
    scanf() uses pointers. As such, you'd better be passing it an address. If not, then your call to scanf() is incorrect. If so, then yes, that'd work.

    2
    %s stops on spaces. So no, it won't work they way you expect it to in your example.

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

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    If you haven't learned fgets() yet, just learn it now:

    Code:
    #include <stdio.h>
    
    int main()
    {
       char address[30];
       printf("Enter your address: ");
       fgets(address, 30, stdin);
       //scanf("%s", address);
       printf("\n%s\n", address);
       return 0;
    }
    Try commenting out fgets and run it with scanf. Notice any difference in output if you enter "123 Fake Street"?
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Spaces seperate my strings... so it takes the value for the next scan f... and Quzah... man stop being so harsh on me! I know I forgot the & op... I was typing in a hurry. Okay a few questions about fgets... what's stdin for ... what does the 30 mean (30 elements in array?), can it be used with structures? I'm assuming Yoda, that you define address as a var before hand?

    Thanks.

  8. #8
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Let's look at the definition of fgets():
    char *fgets( char *string, int n, FILE *stream );

    string: Storage location for data
    n: Maximum number of characters to read
    stream: Pointer to FILE structure
    > what's stdin for
    This function normally reads from a file but in order to read from the keyboard, we use stdin. Standard input comes from the keyboard.

    >what does the 30 mean (30 elements in array?), can it be used with structures?
    It specifies the maximum number of characters to read. You can use it for structures but your results won't be correct. This function is used to read in strings. But you can have a string as an attribute of your structure and read that in if that's what you meant.

    > I'm assuming Yoda, that you define address as a var before hand?
    Look at the code. It's defined. Hehe, somebody called me Yoda

    EDIT - Don't diss Quzah. He's only trying to help. He's one of the most helpful members on this board btw.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by diddy02
    and Quzah... man stop being so harsh on me! I know I forgot the & op... I was typing in a hurry.
    Trust me, if you think that was harsh, you haven't seen anything yet.

    At any rate, there was nothing 'harsh' in the entire post. I simply pointed out potential problem. Since you didn't show us your structure definitions, how am I supposed to know if you're using them correctly. Consider the following:

    Code:
    struct mystruct 
    {
        char array[80];
        char letter;
    };
    
    
    struct mystruct myInstance;
    
    printf("Enter a string: ");
    scanf( "%s", myInstance.array );
    This is the correct usage. You do not need the & sign. Why is that? Well, because you can use the name of an array as a pointer to its first element. As such, like with your example, it is possible for you to be using the name of a member of a structure, which is not simply a pointer, without the & symbol, and have it work.

    So it very well may be that you DIDN'T forget the & symbol, but in fact that it didn't need it.

    Now then, if "address" were simply an integer or something, here you'd need the &. This is the reason I stated:

    "scanf() uses pointers. As such, you'd better be passing it an address. If not, then your call to scanf() is incorrect."

    I didn't say you were incorrect, I said that you may be incorrect.

    I don't understand how people are so thin skinned. You have to act like you're walking around on egg shells...

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

  10. #10
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    That little Yoda bit (haha, yodabyte) shows you how absent minded Iam... I forget things easy... but try to understand my plight... my tutor didn't teach my any of that stuff. She expects me to do stuff from what she's taught me since she assigns me ... the assignments! She mentioned something about spaces, she did NOT however mention anything about fgets.

    Can I have three different struct vars to take the data, like 123 is assigned to streetno, and then Fake is assigned to streetname and then street, or road, or drive, or crescent is assigned to streettype?

  11. #11
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Theory was correct, thanks for your help. And Quzah, sorry for knocking on you. You've been really helpful to me throughout all my problems with C.

  12. #12
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    She mentioned something about spaces, she did NOT however mention anything about fgets.
    I was never taught fgets in school either.
    Can I have three different struct vars to take the data, like 123 is assigned to streetno, and then Fake is assigned to streetname and then street, or road, or drive, or crescent is assigned to streettype?
    Yes. You can use fgets and store it into one string. Then break it up yourself. Or call scanf or fgets 3 times asking for the information separately.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you can use other specifiers for scanf which will read until any character you specify is entered. But since she hasn't taught you those, I guess you can't use them...

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing chars in a string
    By Hawkin in forum C Programming
    Replies: 6
    Last Post: 02-23-2008, 08:06 PM
  2. traverse a string to look for certain chars
    By cdkiller in forum C++ Programming
    Replies: 7
    Last Post: 09-28-2006, 02:19 PM
  3. Replies: 8
    Last Post: 03-31-2006, 08:15 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM