Netbeans and while loop

This is a discussion on Netbeans and while loop within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am doing an exercise in Netbeans from the C++ Primer book. The questions is: "Write a program ...

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

    Netbeans and while loop

    Hi all,

    I am doing an exercise in Netbeans from the C++ Primer book. The questions is: "Write a program to ask the user to enter a series of numbers. Print a message saying how many of the numbers are negative numbers."

    I wrote the below code:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main () {
        std::cout << "Enter a series of numbers separated by spaces:" << std::endl;
        int a, negativeCounter = 0;
        while (std::cin >> a) {
            if (a < 0) {
                ++negativeCounter;
            }
        }
        std::cout << "You gave me " << negativeCounter << " negative numbers." << std::endl;
        return 0;
    However, when I run the program in Netbeans, I input a series of numbers and hit Enter. Nothing happens as if the program is waiting for more. Is there something Netbeans-specific that's causing the issue? The answer given on the following site (for question 1.17) is similar to mine: 1.4.3 - CppPrimer

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    No worries, I found out myself. I needed to execute the end-of-file command, which is CTRL+D for Netbeans.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Your loop executes as long as the std::cin >> a condition evaluates to true... that is, for an integer variable a, the loop will continue to process input as long as an integer value can be read from the stream. Simply pressing the enter key does not terminate this loop, it only makes the code loop through all the numbers that have been entered up to that point (input is line-buffered) at which point the program then waits for more input from the user. You could also have entered a non-numeric character and pressed enter and the loop condition would then evaluate to false although that would be a potential issue had your program then attempted to perform user input after that point instead of just a simple output operation.
    Last edited by hk_mp5kpdw; 04-03-2012 at 06:45 AM.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting C Compiler for NetBeans
    By Soulzityr in forum C Programming
    Replies: 3
    Last Post: 03-30-2010, 10:00 AM
  2. compiling C++ with NetBeans
    By Furious5k in forum C++ Programming
    Replies: 6
    Last Post: 02-14-2010, 01:38 PM
  3. Compiling C++ with Netbeans for Windows
    By Noise in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2009, 02:39 AM
  4. from dev-c++ mingw to netbeans cygwin
    By yahn in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 08:18 AM

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