Thread: convert a string to upper case

  1. #1
    Unregistered
    Guest

    Lightbulb convert a string to upper case

    I am trying to convert a string to uppercase using the toupper command. The string is read into the program via a pointer in a seperate function.

    Here is the code:
    #include <stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<ctype.h>

    void main ()
    {
    void menu (char *choice[]);
    char *name[30];
    int *studentnum[4];
    char *address[50];
    char *choice[7];
    int temp;

    menu(choice);
    printf("%s", choice);
    }

    void menu (char *choice[])
    {
    int x=0;

    printf("----------------------MENU----------------------\n");
    printf(" 1. insert --> Enter data for one student\n");
    printf(" 2. display --> Display complete list of\n");
    printf(" students and data.\n");
    printf(" 3. sort --> Sort the data in ascending\n");
    printf(" order.\n");
    printf("------------------------------------------------\n");
    printf("Please type the corresponding command. (ex: sort): ");
    scanf("%[^\n]", choice);
    for (x=0; x,7; x++)
    {
    &choice[x]=toupper(*choice[x]);
    }
    }

    Can someone show me where I am going wrong?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >void main ()
    No no no no no no no no no no. main returns an int, it can't return anything BUT an int. If you return anything but an int your program is undefined and completely wrong.

    >void menu (char *choice[]);
    You don't want to declare a prototype inside your main function.

    >scanf("%[^\n]", choice);
    This is unsafe, do you know exactly what will happen if the user enters more characters than choice can hold? Neither do I, always test for boundaries.

    >Can someone show me where I am going wrong?
    For the actual process of passing the array and then changing it to upper case, your indirection was wacky, here's a stripped down version that works better.
    Code:
    #include <stdio.h> 
    #include <ctype.h> 
    
    static void menu ( char *choice );
    
    int main ( void ) 
    {   
      char choice[7] = {0}; 
      menu ( choice ); 
      printf ( "%s", choice );
      return 0;
    } 
    
    void menu (char *choice) 
    { 
      int x; 
      fgets ( choice, 7, stdin ); 
      for ( x = 0; x < 7; x++ ) { 
        choice[x] = (char)toupper ( choice[x] ); 
      } 
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Code:
    void main() /* Itīs crap, shouldnīt exist!!!! */
    Code:
    int main() /* itīs ok, most common */
    Code:
    int main(void) /* Itīs the standard, please always use this! */
    Code:
    int main(int argc, char *argv[]) /* ok, to use command arguements */
    Code:
    int main(int argc, char **argv) /* ok, nearly the same like above */
    klausi
    Last edited by klausi; 03-14-2002 at 01:37 PM.
    When I close my eyes nobody can see me...

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >int main(int argc, char **argv) /* ok, nearly the same like above */
    Actually, it's exactly the same as the above. The ISO C standard specifies three constructs for main:
    int main ( void )
    int main ( int argc, char *argv[] )
    int main ( int argc, char **argv )
    and anything equivalent because you can choose any names you want, argc and argv are just the most common convention. Anything else besides those three declarations is implementation defined and nonstandard.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 11:32 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM