简体   繁体   中英

Why am I getting a "1" output in between the other output lines?

I am very new to C++, and I am trying to make a simple number reading program, and it is functional. However, I keep getting a '1' input in between my other output lines. How can I remove these 1s?

Here is my code:

    #include <stdio.h>
    #include <iostream>
    using namespace std;

    int main()
    {
        printf("\nThe following program should enter integer numbers until a negative number.\n");
        printf("The output is the smallest number input as well as the number of numbers.\n\n");
        printf("Please enter a number -----> ");

        int n = 0;
        int num;
        cin >> num;
        int smallest = num;


        while (num >= 0)
        {
            n++;
            if (num < smallest)
            {
                int smallest = num;
            }

            cout << "Please enter another number ----->  " << (cin >> num) << endl;
        }


        while (num < 0)
        {
            cout << "Negative number entered. Calculating Results...\n\n";
            cout << "Of " << n << " numbers read, the smallest number is " << smallest << ".\n";
            return 0;
        }

    }

And the output looks like this (I randomly input some test numbers):

    The following program should enter integer numbers until a negative number.
    The output is the smallest number input as well as the number of numbers.

    Please enter a number -----> 3
    Please enter another number ----->  4
    1
    Please enter another number ----->  8
    1
    Please enter another number ----->  -1
    1
    Negative number entered. Calculating Results...

    Of 3 numbers read, the smallest number is 3.

'''

How do I remove these 1s, and why are they happening?

(cin >> num)

This expression does two things:

  1. The cin input stream waits for user input and puts that value into num .
  2. The operator overload for >> on an istream returns a istream& , a reference to the cin instance.
    (This is why you can repeat the >> operator on one line to get multiple values.)

That expression is in a place where the cout << operator is expecting an argument, so the conversion from istream& to some sort of printable character is resulting in a 1 being added to the cout stream.

The reason the 1 is on a new line is because the terminal/console you're using requires you to use the Enter key (which adds a new line) to enter a value.

I tried this in my c++ console, and had to make a change in line 25. Instead of -

cout << "Please enter another number ----->  " << (cin >> num) << endl;

I used this -

cout << "Please enter another number ----->  ";
cin >> num;

And there were no explicit error messages. Also, no 1s.

I am a bit concerned about the actual code though. The logic is not correct.

How was you able to compile this code?

Doc.cpp:26:60: error: no match for 'operator<<' (operand types are 'std::basic_ostream' and 'std::basic_istream::__istream_type {aka std::basic_istream}') cout << "Please enter another number -----> " << (cin >> num) << endl;

I have to change this line:

    cout << "Please enter another number ----->  " << (cin >> num) << endl;

into this:

    cout << "Please enter another number ----->  ";
    cin >> num;
    cout << endl;

in order to make the code compilable, at least for me.

Also, the last while loop is unnecessary, since if num is not bigger or equal to zero, it could only be a negative number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM