1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C-language: quick n00b question

  1. Oct 8, 2011 #1
    Code (Text):


    int main() {
        int n;
        char ch;
        char* str;
        int i;
        printf("Enter the number of values you want to enter in your string: ");
        scanf("%d", &n);
        str = (char*)malloc(sizeof(char)*n);
        printf("Enter the string: ");
        getchar();
        fgets(str, n, stdin);
        printf("%s",str);
        printf("Now enter the character you want to check for: ");
        getchar();
        scanf("%c", &ch);
        printf("ok cool");
        printf("The character '%c' occurs %d times", ch, count_char(str,ch));
        return 0;
    }

     
    why is not working. I dont have much time and I am going to start with next problem. Can anyone who is adept at C help me with thing. Pretty please
     
  2. jcsd
  3. Oct 8, 2011 #2

    Mark44

    Staff: Mentor

    Please be more specific than "is not working." A description of what your program is doing that it shouldn't be doing would be helpful.

    You might be having a problem with these lines:
    Code (Text):

    printf("Now enter the character you want to check for: ");
    getchar();
    scanf("%c", &ch);
     
    Why do you have both getchar() and scanf()? getchar() might be picking up the character that is entered, but it is discarding the character. Something like
    ch = getchar();

    without the scanf() after it might fix things.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C-language: quick n00b question
  1. C language problem ? (Replies: 13)

  2. C language (Replies: 6)

  3. Quick question (Replies: 3)

  4. C language (Replies: 5)

Loading...