Like Tree1Likes

Basic Loop

This is a discussion on Basic Loop within the C Programming forums, part of the General Programming Boards category; Hello everyone, In the code below why is it not grabbing a value for x? Code: #include<stdio.h> int main(void) { ...

  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
    21,754
    Quote Originally Posted by DuckCowMooQuack
    In the code below why is it not grabbing a value for x?
    What do you mean?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,754
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,801
    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
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    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
    21,754
    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);
    dennis.cpp likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,754
    Re-read hk_mp5kpdw's post #5. Does it apply to you?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,754
    Quote Originally Posted by DuckCowMooQuack
    This is part of a larger program.
    Okay. Post the smallest and simplest compilable program that demonstrates this problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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...
    Name:  awesome.jpg
Views: 48
Size:  11.2 KB

    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...

Page 1 of 2 12 LastLast
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, 01:12 PM
  3. The Infinit loop that doesn't loop.
    By errigour in forum C Programming
    Replies: 1
    Last Post: 11-09-2010, 10:31 AM
  4. Basic Loop Question
    By demiurge in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2009, 08: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21