Thread: My first C code using IF and Else statement with do...while loop.

  1. #1
    Registered User
    Join Date
    Sep 2013
    Location
    Mumbai, India
    Posts
    18

    My first C code using IF and Else statement with do...while loop.

    Hello all,

    Below is my first program in C using 'IF and ELSE' along with 'DO...WHILE' loop in between.

    My program is pretty simple and I understand I could accomplish what I'm trying to do here by only using 'WHILE' loop. However; for understanding and experimenting purposes I would like to have my program run as below.

    It throws an error when I try compiling. Can someone please help in rectifying errors in the below code?

    Code:
    #include <stdio.h>
    int main()
    (
    int a;
    printf( "What is 2 + 2? ");
    scanf ( "%d", &a );
    if ( a == 4 ) {
    printf( "You are correct!\n" );
    }
    else {
    do
    {
    printf ( "You are incorrect.\n" );
    printf ( "Please try again: " );
    scanf ( "%d", &a );
    }while( a == 4 );
    printf( "You are correct!\n" );
    getchar();
    return 0;
    }
    )

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You appear to have mixed up your parentheses with your braces for your main function.

    Also, you need to indent your code properly.
    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

  3. #3
    Registered User
    Join Date
    Sep 2013
    Location
    Mumbai, India
    Posts
    18
    Quote Originally Posted by laserlight View Post
    You appear to have mixed up your parentheses with your braces for your main function.

    Also, you need to indent your code properly.

    Thank you! It works. I didn't know indentation is as important as any other rule in C.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Proper indentation will help you spot problems, although I would not call indentation a "rule" since the compiler doesn't really care. Indentation is only for us humans, but proper indentation really really helps us poor humans read and follow the logic of the program.

    Jim

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    There is also a flaw in your program. In your do while loop you have do...while(a==4). Say i input 3 first time it asks for input the output would be "You are incorrect Try again". if I type 3 again it would say correct because a != 4 and thus it will exit you do...while loop.

    The correct code would simply be:
    Code:
    do {
          printf ( "You are incorrect.\n" );
          printf ( "Please try again: " );
          scanf ( "%d", &a );
    }while( a != 4 );

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by Bharat1987 View Post
    Thank you! It works. I didn't know indentation is as important as any other rule in C.
    imagineyouwantedtobeanauthorandyouwroteyournovelwi thoutusinganypunctuationorspacingbetweenyourwordsh owmanypeopledoyouthinkwillreadit

    Proper formatting of source code is a matter of basic legibility, both for anyone else that looks at the code, and for you as well, especially if you come back to it later.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A for-loop as a while statement?
    By c-da in forum C Programming
    Replies: 3
    Last Post: 11-09-2012, 08:13 AM
  2. Using while loop for if statement.
    By Galens in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2008, 03:14 PM
  3. problem with do loop and if statement
    By nick187 in forum C++ Programming
    Replies: 4
    Last Post: 11-14-2007, 05:21 AM
  4. if statement in while loop
    By C++Noobie in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2006, 11:38 AM
  5. For/loop statement
    By stevedawg85 in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2006, 08:03 PM