Thread: help with this code please

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    help with this code please

    hello,
    this code give me the same output not OH
    Code:
    #include<stdio.h>
    
    int main(){
    
    char arr[100];
    int i;
    int judge=0;
    printf("please input ");
    scanf("%c",&arr);
    
    for(i=0;i<100;i++){
    
        if((arr[i]== 'o'|| arr[i]=='O')&&(arr[i+1]=='h' ||arr[i+1]=='H'))
            judge++;
    
    }
    
    if(judge>0)
        printf("OH");
    else
        printf("not OH");
    
    return 0;
    
    }
    Last edited by tost; 03-07-2013 at 01:13 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    How can arr[i] be 'o' or 'O' AND 'h' or 'H' at the same time?

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Check your warnings:

    Code:
    main.c||In function 'main':|
    main.c|9|warning: format '%c' expects type 'char *', but argument 2 has type 'char (*)[100]'|
    ||=== Build finished: 0 errors, 1 warnings ===|
    If you're trying to read a string with "scanf()", you need to make it:

    Code:
    scanf("%s",arr);
    --------

    Code:
    if((arr[i]== 'o'|| arr[i]=='O')&&(arr[i]=='h' ||arr[i]=='H'))
    Check your logic. A character cannot be 'O' and 'H' at the same time.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    Quote Originally Posted by Tclausex View Post
    How can arr[i] be 'o' or 'O' AND 'h' or 'H' at the same time?
    yes i edited the code after i post it ,you posted too fast

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    Quote Originally Posted by Matticus View Post
    Check your warnings:

    Code:
    main.c||In function 'main':|
    main.c|9|warning: format '%c' expects type 'char *', but argument 2 has type 'char (*)[100]'|
    ||=== Build finished: 0 errors, 1 warnings ===|
    If you're trying to read a string with "scanf()", you need to make it:

    Code:
    scanf("%s",arr);
    --------

    Code:
    if((arr[i]== 'o'|| arr[i]=='O')&&(arr[i]=='h' ||arr[i]=='H'))
    Check your logic. A character cannot be 'O' and 'H' at the same time.
    that's right this is an array i edited the code

    Code:
    #include<stdio.h>
    
     
    
    int main(){
     
    char arr[100];
    int i;
    int judge=0;
    printf("please input ");
    scanf("%s",arr);
    
     
    for(i=0;i<100;i++){
     
        if((arr[i]== 'o'|| arr[i]=='O')&&(arr[i+1]=='h' ||arr[i+1]=='H'))
            judge++;
     
    }
     
    if(judge>0)
        printf("OH");
    else
        printf("not OH");
     
    return 0;
     
    }
    Last edited by tost; 03-07-2013 at 01:22 PM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    for(i=0;i<100;i++){
    
        if((arr[i]== 'o'|| arr[i]=='O')&&(arr[i+1]=='h' ||arr[i+1]=='H'))
            judge++;
    
    }
    Be careful - you're overrunning the bounds of your array here (i+1)

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    Quote Originally Posted by Matticus View Post
    Code:
    for(i=0;i<100;i++){
    
        if((arr[i]== 'o'|| arr[i]=='O')&&(arr[i+1]=='h' ||arr[i+1]=='H'))
            judge++;
    
    }
    Be careful - you're overrunning the bounds of your array here (i+1)
    i changed the code

    Code:
    #include<stdio.h>
    
    int main(){
    
    char arr[100],OH[]={'O','o','H','h'};
    int i,x=0;
    printf("please input ");
    scanf("%s",arr);
    
    for(i = 0; i<100; i++){
                int judge = 0;
                int j;
                for(j = 0; j < 3; j++)
                    if(arr[i] == OH[j])
                       judge = 1;
                    if(judge){
                            x++;
                       continue;}
    }
    if(x>0)
        printf("OH");
    else
        printf("not OH");
    
    return 0;
    
    
    }
    output OH with any input
    Last edited by tost; 03-07-2013 at 01:39 PM. Reason: removing stdbool lib

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Was this to change the functionality? To fix the overrun, you only had to:

    Code:
    for(i=0;i<99;i++)
    P.S. It's good practice to avoid "magic numbers" - consider using something like this instead of the fixed value of 100:

    Code:
    #define MAX_LEN 100

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    i changed the code to avid overrun and you said better solution and more easy
    [CODE]
    Code:
    #include<stdio.h>
    
    int main(){
    int const MAX_LEN =100;
    char arr[MAX_LEN];
    int i;
    int judge=0;
    printf("please input ");
    scanf("%s",arr);
    
    
    for(i=0;i<99;i++){
    
        if((arr[i]== 'o'|| arr[i]=='O')&&(arr[i+1]=='h' ||arr[i+1]=='H'))
            judge++;
    
    }
    
    if(judge>0)
        printf("OH");
    else
        printf("not OH");
    
    return 0;
    
    }
    thank you it is working

    could you check this topic here
    when it is better to use define and constant ??
    i checked the topic and i think it is better to use define for the thing that i will use alot in code

    constant will take place in memory
    define will just replace

    so what you think sir ?

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'd recommend that you use "#define" for now.

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    ok thank you for help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  3. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  4. Having trouble translating psudeo-code to real-code.
    By Lithorien in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2004, 07:51 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM