#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    Can't make the printf function work


    hi , i am trying to make a vertical histogram of the number of letters of each word of a text. the problem is that the program doesn't return anything when it should
    here is the code

    #include <stdio.h>

    main()
    {
    int k, i=0, c;
    for (k=0; k<20; ++k){
    while ((c=getchar()!=EOF))
    if (i=k && (c=getchar())!=' ')
    printf('+'), ++i;
    else if ((c=getchar())==' ')
    i=0, printf(' ');
    else if (c=='\n')
    printf('\n');
    }
    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    u need to give string in printf argument character are creating problem.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    OK i've changed what you said and gave printf a string. Still, the program does not work correctly: when y run it, no even ONE "+" sign appears on the screen when i run the program and type "something...\n". i'm lost here
    Here is the new code

    #include <stdio.h>

    //vertical histogram of length of words of input provided that each word is less than 30 characters long
    main()
    {
    int k, i=0, c;
    char s[30];
    for (k=0; k<30; ++k){
    while ((c=getchar()!=EOF)){
    if (i==k && (c=getchar())!=' ')
    s[i]='+', ++i;
    else if (i==k && (c=getchar())==' ')
    s[i]=' ', i=0;
    else if (c=='\n')
    printf("%s\n", s[30]);
    }
    }
    }
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Use
    Code:
    int main
      /* ... */
      return 0;
    }
    The first argument to printf needs to be a pointer. The compiler warnings tell me this. Use putchar(c); instead of printf("%c",c); because it's more efficient.

    In my opinion, you need to separate gathering input from displaying the histogram. I would be hard pressed to create a program that could generate input that could produce a useful histogram. (Yes, "a useful" not "an useful" sounds correct to me, although I'd go with "an histogram".)

    $ hard_pressed | your_histogram_program

    Let's skip this foolishness.

    In the absence of ungetc, getchar() reads a new character every time you call it. But with a buffered input device it won't read anything until you press Enter or present a super-duper long line. In essence you wrote

    if some_character do something
    else if a_different_character do thing2
    else if still_another_char do thing3

    20 of the conditions need to be end of file,
    some characters should be spaces, and others new line.

    And you've mixed what the input looks like among the carefully formatted histogram bars. That's why I showed the input coming from a pipe---that or redirection hide the input making it perhaps possible to get your code to work. [edit]I'd have to test the program to see if it works without reopening stdin. I think so---but I'm not sure so.[/edit]

    Use this for your main program and try again:
    Code:
    int main()
    {
      char buffer[BIG_ENOUGH];
      get_input(buffer, BIG_ENOUGH);
      display_histogram(buffer, BIG_ENOUGH);
      return 0;
    }
    Last edited by b49P23TIvg; March 18th, 2013 at 12:59 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    what about this?


    shouldn't this work??
    or at least return something as output?
    when i run the program and enter a text followed by '\n' nothing happens. absolutely nothing (the cursor just goes to the next line)

    main()
    {
    int k, i=0, c, l;
    char s[30];
    for (k=0; k<30; ++k){
    while ((c=getchar()!=EOF) && (c=getchar()!='\n')){
    if (i==k && (c=getchar())!=' ')
    s[i]='+', ++i;
    else if ((c=getchar())==' ')
    i=0;
    // else if (i!=k && c==' ') do nothing
    else if (i!=k && c!=' ')
    ++i;
    else if (i!=k && c=='\n')
    printf("%s\n", s[30]);
    for (l=0; l<30; ++l)
    s[l]=' ';
    }
    }
    return 0;
    }
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,220
    Rep Power
    2222
    Do you mean this line?
    printf("%s\n", s[30]);
    You're feeding as a string a single character. Furthermore, that single character is not in s, but rather is the character immediately after s -- the characters in s are s[0] through s[29]; there is no s[30].

    How does printf interpret that? I don't know. There's a chance that it thinks that s[30] is a pointer, in which I'm surprised that your program didn't crash.

    Try this instead (assuming you had stuffed s properly, which I doubt):
    printf("%s\n", s);
    Read it and understand what it says.
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    Under improbable circumstances your program might produce some output.

    Your program is completely wrong. You're nowhere near being able to produce an histogram.

    Please write some practice programs.

    Task 1: design, write, and test a program that displays `hello world' .

    Task 2: design, write, and test a program that reads a character and prints that character 7 times.

    Task 3: Thought experiment. What does this program print?
    Code:
    #define BIG_ENOUGH 3
    int main() {
      int printf(char*,...);
      int array[BIG_ENOUGH];
      int i;
      for (i = 0; i < BIG_ENOUGH; ++i) {
        printf("setting index %d of array to %d\n", i, i);
        array[i] = i;
      }
      printf("What is the value of i?  It holds %d .\n", i);
      return 0;
    }
    Task 4: study, then run this program. Explain why it surprised you.
    Code:
    #include <stdio.h>
    
    int main() {
      fputs("Please enter a character: ", stdout);
      getchar();
      fputs("Please enter another character: ", stdout);
      getchar();
      puts("Thank you.  Press `Enter' to exit.");
      puts("Think about this.");
      getchar();
      return 0;
    }
    (I'm not surprised that your program didn't crash only because it's so difficult to actually get to the line of code that dwise1_aol addressed, and is Task 3 here.)
    Last edited by b49P23TIvg; March 18th, 2013 at 05:32 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    OK. i can do what you are telling me, but still i haven´t studied the stdou and puts functions.

    i've stumbled with still more problems while trying to make at least a small part of my program to run

    for example, i've tried reducing the program to

    #include <stdio.h>

    main()
    {
    int k, i=0, c;
    char s[30];
    for (k=0; k<30; ++k){
    while ((c=getchar()!=EOF) && (c=getchar())!='\n'){
    if (i==k && (c=getchar())!=' ')
    s[i]='+', ++i;
    printf("%s\n", s[30]);
    }
    }
    return 0;
    }

    which, supposedly should return me
    "+
    +
    +
    +
    +"

    when i type in "hello".
    But it doesn´t do anything!
    i know i must got something very wrong, but i can´t see what it is.
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    "you can do" but didn't, and don't know what happens.


    Here's your code. I've inserted comments to identify the various getchar()
    Code:
    #include <stdio.h>
    
    main()
    {
      int k, i=0, c;
      char s[30];
      for (k=0; k<30; ++k){
        while ((c=/*#1*/getchar()!=EOF) && (c=/*#2*/getchar())!='\n'){
          if (i==k && (c=/*#3*/getchar())!=' ') 
    	s[i]='+', ++i;
          printf("%s\n", s[30]);
        }
      }
      return 0;
    }
    Let's pretend you typed hello\n
    #1 reads 'h' which is not EOF thus assigns 1 to c.
    && must evaluate the right hand term.
    #2 reads 'e' which is assigned to c.
    Since 'e' unequal to ' ' the body of the while loop executes.
    i and k are both 0, so they are equal. Therefor
    #3 reads 'l' and assigns it to c.
    Not a space character, s[0] gets '+' and i increments to 1.
    The printf statement evaluates, only it's invalid and anything could happen, see dwise1_aol earlier advice.

    No further predictions are reasonable. If you've predicted stress in a material, and then a crack develops, your stress estimates become invalid. [wrp]


    This version of your code does what you think it ought for input of hello\n
    Code:
    #include <stdio.h>
    
    #define MIN(A,B) ((A) < (B) ? (A) : (B))
    
    int main() {
      int k, i=0, c;
      char s[30];
      for (k = 0; k < 30; ++k) {
        while (((c = getchar()) != EOF) && (c != '\n')) { /* use getchar() once */
          if ((i == k) && (c != ' '))
    	s[i] = '+', ++i;
          s[MIN(i,29)] = 0;		/* nul termination of string */
          puts(s);			/* printf("%s",s); */
        }
      }
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0

    my corrections


    hello again!
    i've been reading the things you have corrected me, and also kept on studying a little bit of C programming.
    Still, y cant create the histogram y want.
    i leave the code, maybe someone can help again.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    #define LINEMAX 30
    
    //histograma vertical que a cada palabra del input le hace una barrita que cuenta cuántas letras tiene esa palabra
    
    void read(char s[], int j);
    
    main()
    {
        int i=0;
        char c, x[LINEMAX];
        
        while (c=getchar() != EOF && c!='\n'){
            x[i++]=c;
        }
        read(x, 1);
        system("pause");
    }
    
    void read(char s[], int j)
    {
     int i, k=0;
     int win;
     char c;
                
               for (i=0; i<=LINEMAX; ++i){
                    win=1;
                    c=s[i];
                    if (c!=' ' && c!='\n' && win==j) {
                        printf("+"), win=win+1;
                    }
                    if (c!=' ' && c!='\n' && win!=j) {
                        win=win+1, printf(" ");
                    }
                    if (c==' '){
                        win=1, printf(" ");
                    }
                    if (c=='\0' && j<=LINEMAX){
                        read(s, j+1);
                    }
               }          
    }

IMN logo majestic logo threadwatch logo seochat tools logo