Thread: help me

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    6

    help me

    Hi

    please tell me what's the problem with my code ?


    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define MaxChars 100
    void convertToUpperCase( char string[], int stringSize);
    int main()
    {
     char string[MaxChars];
     printf("Enter a string to convert to upper case: ");
    
    
     scanf("%[^\n]s",string);
    convertToUpperCase( string,(MaxChars));
     printf( "The string after conversion is: %s\n", string );
     
    }
     void convertToUpperCase( char string[], int stringSize)
    {
        
    
    
     while ( stringSize != '\0' )
     {
    
    
         stringSize = toupper (stringSize );
     
     ++stringSize;
     }
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Why don't you start by telling us what you think the problem is?"

    What input are you giving it?
    What output are you expecting?
    What output are you actually getting?

    We're not clairvoyant. You need to provide more information, rather than just dumping code and saying "fix it for me."
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Quote Originally Posted by the rockk View Post
    Hi

    please tell me what's the problem with my code ?
    First problem is, that you don't give us adequate informations.
    But i think your function has a big proplem.
    Look clooser:
    Quote Originally Posted by the rockk View Post
    Code:
    void convertToUpperCase( char string[], int stringSize)
    {
        while ( stringSize != '\0' )
        {
            stringSize = toupper (stringSize );
            ++stringSize;
        }
    }
    It's look like you want capitalize the character of the sting, but you capitalize an integer.
    Other have classes, we are class

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by WoodSTokk View Post
    It's look like you want capitalize the character of the sting, but you capitalize an integer.
    I noticed that too, but I didn't want to provide any help until the OP showed some effort to solve the problem.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    I know that you know it
    I have not solve the problem, i only point the OP to it.
    It looks for me as if the OP don't understand how character arrays work, but we will see.
    Other have classes, we are class

  6. #6
    Registered User
    Join Date
    Nov 2014
    Posts
    6
    [QUOTE=Elkvis;1219492]Why don't you start by telling us what you think the problem is?"

    What input are you giving it?
    hello
    What output are you expecting?
    HELLO
    What output are you actually getting?
    nothing

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should indent your code properly, e.g.,
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define MaxChars 100
    
    void convertToUpperCase(char string[], int stringSize);
    
    int main()
    {
        char string[MaxChars];
        printf("Enter a string to convert to upper case: ");
        scanf("%[^\n]s", string);
        convertToUpperCase(string, (MaxChars));
        printf("The string after conversion is: %s\n", string);
    }
    
    void convertToUpperCase(char string[], int stringSize)
    {
        while (stringSize != '\0')
        {
            stringSize = toupper(stringSize);
            ++stringSize;
        }
    }
    In convertToUpperCase, why are you not accessing the string parameter?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2014
    Posts
    6
    Code:
    void convertToUpperCase( char string[], int stringSize)
    {
    	
    	
     while (string != '\0' )
     {
    
    
    	 string = toupper (string);
     
     ++string;
     }
    }
    like this ?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No. Remember, you're converting the string to uppercase by converting the characters of the string to uppercase.

    Or, I can now ask: in convertToUpperCase, why are you not accessing the stringSize parameter?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    Show us how you interate through a string (character array) char by char.
    If you can master this, then the problem becomes absolutly clear.
    Other have classes, we are class

  11. #11
    Registered User
    Join Date
    Dec 2014
    Posts
    8
    I will share my codes about convertToupperCase. Im not good in explaining my codes. that's my problem but I can write codes. What crazy I'am

    Code:
    #include <stdio.h>
    to_upper(char *buffer)
    {
    	char ;
    	while(*buffer!=0x00){
    		if(isalpha(*buffer)){
    			putchar(toupper(*buffer));
    		}else{
    			putchar(*buffer);
    		}	
    		buffer++;
    	}
    	return 0;
    }
    main()
    {
    	char buffer[256];
    	
    	printf("Enter String:");
    	gets(buffer);
    	to_upper(buffer);
    	//printf("%s",buffer)
    }
    enjoy reading my codes

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by marque
    I will share my codes about convertToupperCase.
    Please do not "share (your) codes" before the help seeker has come close to a solution. In this case the rockk appears to be putting in the effort, and perhaps will soon reach a workable solution. At that point, if you want to post a "model answer" go ahead.

    Quote Originally Posted by marque
    Im not good in explaining my codes. that's my problem but I can write codes. What crazy I'am
    That does not mean that you're crazy; assuming you have satisfactory command of English (or whatever language is used for the explanation) that means that you don't fully understand your own code.

    Quote Originally Posted by marque
    enjoy reading my codes
    Your code is a pain to read:
    • You used isalpha and toupper without #include <ctype.h>
    • You declared both to_upper and main relying on the return type defaulting to int. to_upper should have been declared with a return type of void and main should have been declared with return type of int. The useless return statement from to_upper should be removed.
    • to_upper is poorly named: it actually prints the transformed string to standard output, but its name gives no indication of that. Furthermore, since it does not modify the string, its parameter should have been a const char*.
    • You have a useless empty declaration with no name in to_upper:
      Code:
      char ;
    • You used 0x00 when the hexadecimal notation has no benefit. It would have been more canonical to use '\0' or nothing at all (i.e., since the null character has a value of 0 and will thus terminate the loop).
    • The test for isalpha followed by the call of putchar in both branches of the if statement is redundant. Since toupper already caters for characters for which islower is false by returning them as-is, no if statement was needed in the first place.
    • You used gets, which is inherently vulnerable to buffer overflow. Instead, you should have used fgets or some other function that can be called safely.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed