Thread: Help with formatting text

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    Help with formatting text

    Code:
    void formattingText()
    {
        int maxNoChars;
        char inputSentence[maxFormattingTextSize];
            
        printf("Formatting Text\n");
        printf("-------------------------\n\n");
        printf("Enter an integer (maximal no of chars per line) :\n");
        
        scanf("%d", &maxNoChars)/
        printf("Enter some sentences (between 150 - 200 chars) :\n\n");
        inputSentence == fgets(inputSentence,maxFormattingTextSize,stdin);
    inputSentence == fgets(inputSentence,maxFormattingTextSize,stdin);
        /*gets(inputSentence);*/
        printf("Formatted text:\n");
        /*printf("%5d", inputSentence);*/
        printf("%s", inputSentence);
        
        
    }
    What do I have to add to this code to restrict the maximum number of characters per line? Any help would be appreciated.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    What exactly are you trying to do? Do you want to chop the end off lines that are too long?

    Also, lines 13 and 12 are the same. And you don't use == to assign values. The == is for checking equality. Use a single = instead.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You could set maxFormattingTextSize and stop pointlessly comparing the return value of fgets to inputSentence.
    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

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Whoops, thanks xD

    Yeah I want to set the maximum width of the block of text to the integer the user inputs. So if it's say 20:

    This :
    Hello this is a sample paragraph well I'll just type some more words

    Will become something like this:

    Hello this is a samp
    le paragraph well I'
    ll just type some m
    ore words

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Change the slash after the scanf to a semicolon.
    Get rid of the repeated fgets statement.
    Get rid of the inputSentence == part.
    Then post your attempt to do the assignment.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Code:
    void formattingText()
    {
        int maxNoChars;
        char inputSentence[maxFormattingTextSize];
            
        printf("Formatting Text\n");
        printf("-------------------------\n\n");
        printf("Enter an integer (maximal no of chars per line) :\n");
        
        scanf("%d", &maxNoChars);
        printf("Enter some sentences (between 150 - 200 chars) :\n\n");
        inputSentence == fgets(inputSentence,maxFormattingTextSize,stdin);
        
        printf("Formatted text:\n");
        printf("%s", inputSentence);
        
    }
    There's my weird typos fixed and now I'll try and solve this. Do I need to somehow put the int next to the s in "%s"? I don't really understand.

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    You don't need the inputSentence == part, like oogabooga said.
    Code:
    while(!asleep) {
       sheep++;
    }

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C89
    Yeah I want to set the maximum width of the block of text to the integer the user inputs. So if it's say 20
    Oh. That is something of a chicken and egg scenario: you need to know what the user has entered in order to size the array to accommodate exactly that, but you need to have an array in order to read what the user has entered. Variable length arrays won't quite help here since you cannot resize them (i.e., they are variable length at compile time, but once created at run time, the length is fixed).

    However, I note that this function is about formatting text, according to the name. Perhaps you really should be giving it parameters for the string to be formatted, and then you limit the output to the given max number of characters., rather than the input.
    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

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Code:
    void formattingText()
    {
        int maxNoChars;
        char inputSentence[maxFormattingTextSize];
            
        printf("Formatting Text\n");
        printf("-------------------------\n\n");
        printf("Enter an integer (maximal no of chars per line) :\n");
        
        scanf("%d", &maxNoChars);
        printf("Enter some sentences (between 150 - 200 chars) :\n\n");
        gets(inputSentence);
        
        printf("Formatted text:\n");
        printf("%s", inputSentence);
        
    }

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    5
    Quote Originally Posted by laserlight View Post
    Oh. That is something of a chicken and egg scenario: you need to know what the user has entered in order to size the array to accommodate exactly that, but you need to have an array in order to read what the user has entered. Variable length arrays won't quite help here since you cannot resize them (i.e., they are variable length at compile time, but once created at run time, the length is fixed).

    However, I note that this function is about formatting text, according to the name. Perhaps you really should be giving it parameters for the string to be formatted, and then you limit the output to the given max number of characters., rather than the input.
    Ok, I understand conceptually what I have to do now but I am unsure of the syntax I will require. Thank you for your time.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    52
    Quote Originally Posted by C89 View Post

    Do I need to somehow put the int next to the s in "%s"?
    You would need something like "%.20s"!!! But since this 20chars per line is not fixed every time and you can't use any variable (maxNoChars) in the formatting string you need to think otherwise. Like, printing char by char till you print maxNoChars per line or you have already printed all the chars.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help formatting text in c
    By anonymoususer in forum C Programming
    Replies: 3
    Last Post: 11-06-2011, 02:30 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Problems with formatting text.
    By vrek in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2007, 11:44 PM
  4. Text Formatting & various questions.
    By Zeusbwr in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2005, 09:27 AM
  5. need help with formatting text
    By Flikm in forum C++ Programming
    Replies: 1
    Last Post: 09-06-2001, 07:52 PM