Help me with a program?

This is a discussion on Help me with a program? within the C Programming forums, part of the General Programming Boards category; i know this program whould be easy, but its been giving me a problem (its 8-14 in the deitel & ...

  1. #1
    sayword
    Guest

    Help me with a program?

    i know this program whould be easy, but its been giving me a problem (its 8-14 in the deitel & deitel third edition C how to program book)

    Input a string of the whole phone number in this exact form (555) 555-5555. Using strtok, extract the area code as a token, then convert to an integer variable. Then extract the 1st 3 digits, (save that string), and then the last 4. Concatenate those 7 numbers, then convert to a long variable.

    Print the labeled 3 digit area code as the integer (without any other characters). Then print the labeled 7 numbers as a long (without any other characters).



    and here is the code i have so far:


    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    void main ()
    {
    char phone[15], *tokenptr;
    int phonenumber[10], area[3], numb[7], numb1[4];
    int i;
    long phnum;
    puts("Enter phone number:");
    gets(phone);
    tokenptr = strtok(phone,"()");
    *area = atoi(tokenptr);
    tokenptr = strtok('\0', " -");
    *numb = atoi(tokenptr);
    tokenptr = strtok('\0', " -");
    *numb1 = atoi(tokenptr);
    printf("%d", area);
    strcat(numb, numb1);
    phnum = atol(numb);
    printf("%ld", phnum);
    }



    the errors i get when i try to compile this program are:

    line 19.10: 1506-280 (W) Function argument assignment between types "unsigned char*" and "int*" is not allowed.
    line 19.16: 1506-280 (W) Function argument assignment between types "const unsigned char*" and "int*" is not allowed.
    line 20.16: 1506-280 (W) Function argument assignment between types "const unsigned char*" and "int*" is not allowed.



    _____________________________________-
    IF anyone can help, you would own :-) thanks guys

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    I could be wrong, but I don't think you can treat arrays of int's like they were strings and subject them to strcat. Btw, use code tags, and main returns an int. (i.e. int main, not void main)
    Last edited by Azuth; 02-10-2003 at 07:09 PM.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    yea, u are right, here is what its supposed to be


    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    void main ()
    {
    char phone[15], area[3], numb[7], numb1[4], *tokenptr;
    int acode, i;
    long phnum;
    puts("Enter phone number:");
    gets(phone);
    tokenptr = strtok(phone,"()");
    area = (tokenptr);
    tokenptr = strtok('\0', " -");
    numb = (tokenptr);
    tokenptr = strtok('\0', " -");
    numb1 = (tokenptr);
    acode = atoi(area);
    strcat(numb, numb1);
    phnum = atol(numb);
    printf("%d", area);
    printf("%ld", phnum);
    }




    And now i get these errors


    "test.c", line 12.3: 1506-025 (S) Operand must be a modifiable lvalue.
    "test.c", line 14.3: 1506-025 (S) Operand must be a modifiable lvalue.
    "test.c", line 16.3: 1506-025 (S) Operand must be a modifiable lvalue.

    ______________________________________________


    and my teacher wants us to use void returns

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop using void main. It's wrong. Search the board if you want to know why.

    Never use gets. It's inherantly flawed.

    The reason you're getting errors is because you can't assign strings to arrays. You need to use something like strncpy.

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

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    im just going by the guidelines my teacher set dude


    anyhow


    i have it fixed, somewhat, this is what it is:


    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    void main ()
    {
    char phone[15], area[3], numb[7], numb1[4], *tokenptr;
    int acode;
    long phnum;
    puts("Enter phone number:");
    gets(phone);
    tokenptr = strtok(phone,"( )");
    strcpy(area, tokenptr);
    tokenptr = strtok('\0', " -");
    strcpy(numb, tokenptr);
    tokenptr = strtok('\0', " -");
    strcpy(numb1, tokenptr);
    acode = atoi(area);
    strcat(numb, numb1);
    phnum = atol(numb);
    printf("%d\n", area);
    printf("%ld\n", phnum);
    }




    And Here is how it runs:


    Enter phone number:
    (555) 555-5555
    8043995995555555


    so i know there is a problem with the int area code part, does anyone know whats wrong and how to fix it?

  6. #6
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Smile

    Well, since I have the Im for the text...

    Looks close- you almost have it. try using a strcpy()

    Also, use int main
    Mr. C: Author and Instructor

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    Originally posted by Mister C
    Well, since I have the Im for the text...

    Looks close- you almost have it. try using a strcpy()

    Also, use int main


    hey dude, i posted the update version of my program in the post above yours, if you could help me out and let me know what is wrong with the area code part (int acode) because it gives me a huge number of "804399599" where it should just say "555"

    thanks bro

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Welcome to the land of people who don't want to actually do things right. You'll fit right in with the majority of the people here. Ignore the tightly nit packs of people who do things the Right Way(TM). You won't like what they have to say...

    A string is a series of characters which end in a null terminator. This is your problem. You are not null terminating your strings. Either display your data differently, or null terminate your strings, providing ample space for the null, which you are currently neglecting.

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

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    Originally posted by quzah
    Welcome to the land of people who don't want to actually do things right. You'll fit right in with the majority of the people here. Ignore the tightly nit packs of people who do things the Right Way(TM). You won't like what they have to say...

    A string is a series of characters which end in a null terminator. This is your problem. You are not null terminating your strings. Either display your data differently, or null terminate your strings, providing ample space for the null, which you are currently neglecting.

    Quzah.

    hm, im feeling a sense of angst, anyhow, i tried leaving more room for the null, but, i got the same thing, is it something with the "( )" part in the line:

    tokenptr = strtok(phone,"( )");

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's because you're doing it wrong. The string "()" never appears in what people type in. It is "(xxx)" where xxx is a number. As such, you cannot simply hand over "()" to strtok and expect it to do what you want.

    [edit]
    Actually let me correct myself. Yes, your strtok call should be right. I doubt you're handling your arrays right. Since you haven't been specific on what exactly is happening after your last change, all we can do is guess what's going wrong. More than likely you're still not terminating yoru strings properly.
    [/edit]


    Here is how you dissect a phone number:

    1) Look for (
    2) Pull off the next three digits.
    3) Look for )
    4) Pull off the next three digits.
    5) Look for -
    6) Pull off the next four digits.

    Not that hard.

    Quzah.
    Last edited by quzah; 02-10-2003 at 08:58 PM.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    why does it work for the line:
    tokenptr = strtok('\0', " -");

    then? well ill give it a whirl, thanks man

  12. #12
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    And use int main (void) (or just int main ()), and if you post more code please use code tags. kthx =)
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  13. #13
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    oh lol, i was printing out the wrong element, i tried to print the area code from the string and not the int acode, everything else was fine, thanks tho guys

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Sayword, have a read of this for next time.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    Feb 2003
    Posts
    33
    ha thanks cause i was gonna just make a topic asking how to post a code in a neet fasion, thanks brotha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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