Thread: toupper() and strcpy()...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    toupper() and strcpy()...

    I'm having a problem again with an endless loop... I'm confused with how to use toupper() and strcpy() together. If that can be done. Here's what I have:

    Code:
      puts("Enter employee department:");
      scanf("%s", employee[i].dept);
      strcpy(temp, employee[i].dept);
      *temp = toupper(temp);
      while (strcmp (temp, "SALES") != 0 || strcmp(temp, "PRODUCTION") !=0) {
        puts("Two departments available: Sales or Production.");
        puts("Choose one:");
        scanf("%s", employee[i].dept);
        *temp = toupper(employee[i].dept);
      }
    once I get in the loop, I can't get out even if I typr in the right input (sales or production)

    I tried:
    Code:
      strcpy(temp, toupper(employee[i].dept));
    but that doesn't work, it gives me a "pointer from integer without a cast" error... I tried to place a '*' before temp, ... that doesn't solve the problem!!
    (I guess you got that I'm trying to make sure that the user is only able to type in either sales or production... )
    So now I'm just really confused
    Last edited by Lau; 11-12-2002 at 11:09 AM.

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    if im right u can use this
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;
    
       /* converts string to upper case characters */
       ptr = strupr(string);
       printf("%s\n", ptr);
       return 0;
    }
    or this one by doing strcmpi (wich is not ansi-c i guess)
    with strcmpi its non-case senstiv and i think thats what u want to accomplish
    Code:
    /* strncmpi example */ 
    
    #include <string.h>
    #include <stdio.h>
    
    int main(void)
    {
       char *buf1 = "BBB", *buf2 = "bbb";
       int ptr;
    
       ptr = strcmpi(buf2, buf1);
    
       if (ptr > 0)
          printf("buffer 2 is greater than buffer 1\n");
    
       if (ptr < 0)
          printf("buffer 2 is less than buffer 1\n");
    
       if (ptr == 0)
          printf("buffer 2 equals buffer 1\n");
    
       return 0;
    by the way i was to lazy to make a code myself so i got these from borland c++

    and o yeah dont use scanf("%s",....);
    use fgets(string,SIZE,stdin);
    Last edited by GanglyLamb; 11-12-2002 at 12:07 PM.

  3. #3
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    7.4.2.2 The toupper function
    Synopsis
    1 #include <ctype.h>
    int toupper(int c);
    Description
    2 The toupper function converts a lowercase letter to a corresponding uppercase letter.
    Returns
    3 If the argument is a character for which islower is true and there are one or more
    corresponding characters, as specified by the current locale, for which isupper is true,
    the toupper function returns one of the corresponding characters (always the same one
    for any giv en locale); otherwise, the argument is returned unchanged.
    So, as far as I understand, you must convert every character of your input string separately.
    [edit]
    @GanglyLamp:
    Ah! I forgot about the string function. That makes it a little easier.
    [/edit]

    Better use fgets() instead of scanf().
    Last edited by Sargnagel; 11-12-2002 at 12:10 PM.

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    mayb its even better to use a switch case thing ..
    dunno what the rest of the code is but it could make things easier ...

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Sargnagel: you're right, toupper() converts one character to upper case!! not a whole string...

    GanglyLamb: I'm going to try the strupr() function... That one should work!!
    What do you mean by using a switch case?

    For those insterrested, I found the description of strupr()there... but you guys probably all know that already!!
    Last edited by Lau; 11-12-2002 at 12:43 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strcmpi is not an ANSI standard function. Neither is strupr. The only ANSI compatible way to do this is to convert one character at a time via toupper or tolower.

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

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    60
    Just found that out!!!

    --> undefined reference to strupr when compiling!!

    That would have probably been too easy...

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    sigh!!

    here's what I came up with...
    Code:
    void validateDept(EMP employee[]) {
      char temp[DEPTSIZE];
      char temp2[DEPTSIZE];
      int i=0;
      int j=0;
    
      for (j=0; j<DEPTSIZE; j++)
        temp2[j] = toupper(employee[i].dept[j]);
        strcpy(temp, temp2);
    
      while (strcmp (temp, "SALES") != 0 || strcmp (temp, "PRODUCTION") !=0) {
        puts("Two departments available: Sales or Production.");
        puts("Choose one:");
        fgets(employee[i].dept, DEPTSIZE, stdin);
        for (j=0; j<DEPTSIZE; j++)
          temp2[j] = toupper(employee[i].dept[j]);
          strcpy(temp, temp2);
      }
      return;
    }
    and here's the ouput:

    Enter employee department:
    ÿ¾÷ ÿ;ªÿ:Ø
    TÂÄ
    IJÄÃÀ
    ÅÃÄ ²Á² Ú² Ú
    : S²Ú
    Ä ÄÄ PÄÄ
    ´ Ã ÄÅ.
    ChÄÄÄ
    ÄÅ
    :
    SALES <<-- my input
    TÂÄ
    IJÄÃÀ
    ÅÃÄ ²Á² Ú² Ú
    : S²Ú
    Ä ÄÄ PÄÄ
    ´ Ã ÄÅ.
    ChÄÄÄ
    ÄÅ
    :

    It's printing all weird characters like that even when I get out of running the program... and I have to exit the telnet session I'm in!!
    Can someone explain what's happening here???????
    Last edited by Lau; 11-12-2002 at 01:27 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're copying over to a temp string, but you never do anything with the string once you copy into it. Shouldn't you be doing something with it?

    Furthermore, you should be checking to see if the input still contains something, rather than just converting the whole array:
    Code:
    for (j=0; j<DEPTSIZE; j++)
          temp2[j] = toupper(employee[i].dept[j]);
    Try:
    Code:
    for( j = 0; j < DEPTSIZE; j++ )
        if( employee[i].dept[j] )
              temp2[j] = toupper(employee[i].dept[j]);
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    60

    Another toupper()

    I've got another problem w/ the toupper() function ... (I know, I know... )

    Code:
    char upperCase(const char *str1) {
      int i=0;
      int len=0;
      len = strlen(str1);
      char temp[len];
    
      while (i<len+1) {
        if (str1[i])
          temp[i] = toupper(str1[i]);
        i++;
      }
      return *temp;
    }
    the code is workin fine... except: it returns only the first character. If I put in "hello", it returns H, etc... why is that?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Another toupper()

    Code:
    char upperCase(const char *str1) {
      int i=0;
      int len=0;
      len = strlen(str1);
      char temp[len];
    
      while (i<len+1) {
        if (str1[i])
          temp[i] = toupper(str1[i]);
        i++;
      }
      return *temp;
    }
    This code shouldn't work fine. You're compiling as a C++ program. Otherwise this wouldn't compile. Assignments, when not done on the declaration line, must come after all variable declarations in a scope block. As such, 'char temp[len]' is invalid.

    Furthermore, you shouldn't use variable length arrays. Additionally, your array doesn't take into account the additional required null.

    Finally, 'temp' is not static, as such, you cannot return it.

    Finally finally, you're returning it wrong anyway. You're dereferncing an array, which is incorrect. You use the name of the array as a pointer to the array. Do not dereference it.

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

Popular pages Recent additions subscribe to a feed