Thread: loop + if + scanf

  1. #1
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74

    Question loop + if + scanf

    Hi folks,

    So I made this program to convert bits to bytes, because I'm so sick of seeing ISP's advertise speeds in megabits, which I consider an intentional attempt to decieve :P And I think I've finally understood how the return value of scanf works since the last time I posted here, so my program can check to see if an integer was entered before processing the input, but I'm stuck on how to make the whole program start over if an integer is not entered. I have a hunch it would involve a loop, but I can't figure out how to make the program start over at "How many mb do you need converted?" if an integer is not entered into scanf... Any ideas?

    Here is the code I have so far:

    Code:
    #include <stdio.h>
    
    int main () {
    
        int b, mb, kb, Byte, kB, mB, gB;
        char term;
    
    
            printf ( "\nHow many mb do you need converted? " );
        
            
            if ( scanf ( "%d%c", &mb, &term ) != 2 || term != '\n' ) {
    
                printf ( "\nThis program requires you enter an integer." );
                printf ( "\n" );
            }
    
    
            else {        
                
                printf ( "\n" );
        
                kb = mb * 1024;    
                b = kb * 1024;
                Byte = b / 8;
                kB = Byte / 1024;
                mB = kB / 1024;
                gB = mb / 1024;
    
                if ( kB < 1024 ) {
    
                printf ( "%i mbps is %i kBps\n", mb, kB );
        
                }
    
                else {
        
                printf ( "%i mbps  is %i mBps\n", mb, mB );
        
                }
        
            }
    
        
        printf ( "\n" );
    
        return 0; 
    
    }
    * oh, and my program makes the assumption for now at least, that mb will be inputted because that's the unit of measurement that i usually see advertised, and i didn't bother making an if statement to print a conversion in terms of gigabytes because i've never heard of a connection that fast :P
    Last edited by SCRIPT_KITTEH; 05-13-2013 at 08:05 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Look up the do-while loop.

    scanf() if it encounters an error reading an int, will leave data that causes the error to be read next time. That will cause the next call of scanf(), if it is reading an int, to return immediately. Doing that in a loop results in an infinite loop.

    It is often considered better to read data from the user as a string (aka array of char) using fgets(), and then interpret that string to see if required data (two ints in your case) is there. sscanf() offers one way to interpret the content of the string.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74
    Quote Originally Posted by grumpy View Post
    Look up the do-while loop.

    scanf() if it encounters an error reading an int, will leave data that causes the error to be read next time. That will cause the next call of scanf(), if it is reading an int, to return immediately. Doing that in a loop results in an infinite loop.

    It is often considered better to read data from the user as a string (aka array of char) using fgets(), and then interpret that string to see if required data (two ints in your case) is there. sscanf() offers one way to interpret the content of the string.
    Ah. I had tried a do loop before I posted it, and indeed it did make an infinite loop. I just assumed I wasn't putting part of the loop in the right part of the program or something XD It's good to know that about scanf, thanks. I've never heard of sscanf or fgets, I'll have to do some reasearch on those. Thanks, grumpy.

  4. #4
    Registered User ApolCor's Avatar
    Join Date
    Apr 2013
    Location
    Philippines
    Posts
    2
    Simply move some of the condition statements around.

    Right after the line "char term;" insert "while (1) {" and then put in "else break; }" right after the "if ( scanf..." block. Then remove the "else {" and its corresponding closing bracket "}" above.

  5. #5
    Registered User SCRIPT_KITTEH's Avatar
    Join Date
    Apr 2013
    Posts
    74
    Quote Originally Posted by ApolCor View Post
    Simply move some of the condition statements around.

    Right after the line "char term;" insert "while (1) {" and then put in "else break; }" right after the "if ( scanf..." block. Then remove the "else {" and its corresponding closing bracket "}" above.
    Ermmm.... I tried to do what I thought you were saying several times, but the program wouldn't even compile. The farthest I got was it compiling but resulting in an infinite loop.

    Is this what you meant?

    Code:
    int main () {
    
        int b, mb, kb, Byte, kB, mB, gB;
        char term;
        
        while(1){
    
            printf ( "\nHow many mb do you need converted? " );
    
            if ( scanf ( "%d%c", &mb, &term ) != 2 || term != '\n' ) {
    
                printf ( "\nThis program requires you enter an integer." );
                printf ( "\n" );
                
            }
            
            else break;
    
        }
    followed by removing the else statement that surrounds the rest of the code?

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Your biggest problem is that scanf() isn't really made for handling user input (the "f" stands for "formatted" which usually doesn't correspond to user input).

    I suggest you read
    FAQ > How do I get a number from the user (C) - Cprogramming.com
    and
    FAQ > Validate user input - Cprogramming.com

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Control loop with scanf
    By sandeep156 in forum C Programming
    Replies: 5
    Last Post: 05-02-2009, 02:06 PM
  2. Using scanf in a for loop
    By agentsmith in forum C Programming
    Replies: 2
    Last Post: 12-18-2007, 09:37 AM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  4. while scanf loop
    By Pyrce in forum C Programming
    Replies: 13
    Last Post: 07-15-2006, 08:37 PM
  5. scanf in a loop
    By santos288 in forum C Programming
    Replies: 3
    Last Post: 10-12-2005, 08:58 PM