Thread: Basic Loop

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    55

    Basic Loop

    Hello everyone,

    In the code below why is it not grabbing a value for x?

    Code:
    #include<stdio.h>
    
    int main(void)
    
    {
    
    int x = 0;
    int j = 0;
    
    printf("Enter a number: ");
    scanf("%d",&x);
    
    while(j!=1)
    {
     if(x<=40)
      j = 1;
     else
     {
      printf("Enter a number: ");
      scanf("%d",&x);
      j = 0;
     }
    }
    
    
    
    printf("%d",x);
    return 0;
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DuckCowMooQuack
    In the code below why is it not grabbing a value for x?
    What do you mean?
    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
    Jan 2011
    Posts
    55
    Ok so if you look at line 27 I am calling it to print "x" to the screen. What it should do is print whatever number was last entered by the user. (The loop makes sure this number is less than 40).

    This line isn't even called at all though when you compile and then run it. Where am I going wrong?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It works for me. Perhaps you can make it clearer for yourself with:
    Code:
    printf("You entered: %d\n", x);
    Note that if this is actually part of a larger program, the '\n' could make a difference as standard output is typically line buffered, hence the newline will cause output to be written immediately.
    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

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Does the program exit? Maybe you need to pause the program after the print. How are you executing the program? From within an IDE or at the command prompt? Sometimes if you execute within an IDE, it creates the console window for you and runs the program. When the program finishes execution, the window will disappear before you're able to see the output.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    spaghetticode
    Guest
    Except the fact that your loop makes sure the number is greater than 40, not less - the whole thing looks ok to me. What does your program do? Is there some output at all?

    EDIT: Damn, too slow.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    It's odd it's not reading that for me. I'll keep messing with it I guess. Thanks the the fast reply though laserlight!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by dennis.cpp
    Except the fact that your loop makes sure the number is greater than 40, not less - the whole thing looks ok to me.
    No, the loop terminates when x <= 40.

    Admittedly, it would have been simpler to use a do while loop with the negation of that termination condition:
    Code:
    do
    {
        printf("Enter a number: ");
        scanf("%d", &x);
    }
    while (x > 40);
    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
    Jan 2011
    Posts
    55
    Quote Originally Posted by laserlight View Post
    No, the loop terminates when x <= 40.

    Admittedly, it would have been simpler to use a do while loop with the negation of that termination condition:
    Code:
    do
    {
        printf("Enter a number: ");
        scanf("%d", &x);
    }
    while (x > 40);
    It's still not picking up on the printf("%d",x);

    I have no idea why???

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Re-read hk_mp5kpdw's post #5. Does it apply to you?
    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

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    This is part of a larger program. I'm just trying to get this to work.

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Quote Originally Posted by hk_mp5kpdw View Post
    Does the program exit? Maybe you need to pause the program after the print. How are you executing the program? From within an IDE or at the command prompt? Sometimes if you execute within an IDE, it creates the console window for you and runs the program. When the program finishes execution, the window will disappear before you're able to see the output.
    I am executing the program from within Cygwin terminal. I edit the code from within Notepad++, save it, compile it, and then run it.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by DuckCowMooQuack
    This is part of a larger program.
    Okay. Post the smallest and simplest compilable program that demonstrates this problem.
    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

  14. #14
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Here's my complete code as of now...
    Code:
    #include<stdio.h>
    
    int main(void)
    
    {
    
    int x = 0;
    int j = 0;
    
    printf("Enter a number: ");
    scanf("%d",&x);
    
    do
    {    
     printf("Enter a number: ");
     scanf("%d", &x);
     }
     while (x > 40); 
    
    
    
    printf("The number is %d\n",x);
    
    printf("AWESOME");
    return 0;
    
    }
    Here is a sample execution of the program...
    Basic Loop-awesome-jpg

    As you can see, anything after the do while is not being executed.


    NOTE: This is one piece of the whole program.

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    55
    Ok I solved it. All I did was delete the semi colon after
    Code:
     while (x > 40);
    compiled it...
    added the semi colon back
    compiled it
    ran it...It now works

    What the heck...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  2. using for loop and functions to create a basic menu
    By sunandstars in forum C Programming
    Replies: 2
    Last Post: 02-19-2011, 02:12 PM
  3. The Infinit loop that doesn't loop.
    By errigour in forum C Programming
    Replies: 1
    Last Post: 11-09-2010, 11:31 AM
  4. Basic Loop Question
    By demiurge in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2009, 09:10 AM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM