Thread: Printing an amount of digits related to an int

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    2

    Printing an amount of digits related to an int

    Hey all,
    Very new at programming, working on a short homework assignment.
    Had to modify a basic winsock echo server to give a response based on user input.
    For example, if the user enters "get signals RF 10", the user should get 40 zeros in return. If the user enters "get signals RF 2200", the user should get 8800 zeros in return.

    Now I've made it so the program can get the number that the user enters and multiply it by four, however I'm stuck on getting the program to print out the zeros. I figure I could do this using a for loop.. I'm sure this is basic but again, I'm very new at this. Go easy on me. My program is attached below.

    HandleTCPClient.c

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    This for loop is very wrong.
    Code:
                    for(numSamples >= 1; s < numSamples ; s+1)
    The first and third parts do absolutely nothing.
    I assume you meant:
    Code:
                    for (s = 0; s < numSamples; s++)
                        putchar('0');
    Also, your buffer should probably have one extra byte for the terminating ascii nul.
    Code:
        char echoBuffer[RCVBUFSIZE + 1];        /* Buffer for echo string */
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Your code isn't too long so you should post it in-line using the [code] tag.

    That said, the following code stands out immediately:
    Code:
        int s;
                       for(numSamples >= 1; s < numSamples ; s+1)
                        printf("0");
    I think you need to learn how a for loop works. I'll give a quick breakdown for you here*. The first expression is the initializer. Usually this is used to set the iterator variable to 0, which is s in your code. The second is the test expression, which is usually a comparison between the iterator (s) and the ending value (numSamples). This part looks ok. The third expression is evaluated after each iteration. This is usually used to increment the iterator variable (such as ++s).

    The first problem with your for statement is that the initializer isn't doing anything (the result is ignored by the for statement), and your iterator (s) is left uninitialized. The second problem is that the last expression also does nothing. Its result is ignored by the for statement, just like with the initializer.

    Hopefully you can figure out what you need to change now.


    * Note: The for loop in C is extremely powerful and can do much more than loop a given number of times, but this is one of the most common usages and is what I describe.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    2
    Thanks a bunch guys. Knew it was my for loop that had the problem but wasn't exactly sure where I was going wrong, I get it now. Thanks again for helping a super n00b like myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing the individual digits of an integer variable??
    By 1thoughtMaze1 in forum C Programming
    Replies: 10
    Last Post: 03-04-2012, 05:53 PM
  2. Digits pyramid printing programs needed in C Programming
    By technoexam in forum C Programming
    Replies: 8
    Last Post: 10-01-2011, 04:58 PM
  3. Retaining same amount of digits
    By int3 in forum C Programming
    Replies: 16
    Last Post: 04-29-2007, 03:29 AM
  4. Printing a specific number of digits
    By JizJizJiz in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2006, 08:14 PM
  5. Printing 2 digits
    By sjalesho in forum C Programming
    Replies: 2
    Last Post: 11-02-2003, 03:14 PM