Programming
c++ while-loop text-files io-redirection
Updated Sun, 14 Aug 2022 10:28:18 GMT

using input redirection in while loop


I need to create a program that reads in a list of random integers via input redirection and performs various tasks with them. For the purpose of this question I just want to print the sum of each number + 1. Trouble is, I dont know how to get it to perform the operation on anything but the first number in the list. My mini program (program.cpp) is

#include <iostream>
using namespace std;
int main () {
    int input;
    bool arb = true;
    cin >> input;
    while (arb) {
        cout << input + 1 << endl;
    }
    return 0;
}

and a sample textfile (textfile.txt) I'm using contains

9
8
7
6

When I run it it prints out 10 for infinity. What Id like it do is print out

10

9

8

7

or the equivalent for whatever other text file may be used.
I thought maybe I could somehow reference specific lines in the textfile, but from searching how to do that all the similar questions I could find seemed to have solutions that required fstream, which is something I havent learned yet and therefore Im not allowed to use it (this is for a class assignment).
The only compiler directives Ive learned are iostream, string, cmath, iomanip, cstdlib, and ctime. The ways to get input Ive learned are cin >> input, cin.get(input), and cin.getline(input), so if there are other ways of reading in the file besides those 3 that Im unaware of, unfortunately I cant use it. Note that my program HAS to use a while loop. The way the program will be run is

./program < whatevertextfile.txt

which I can't change.

given these restrictions, how can I get my program to read in and use each integer in the text file?




Solution

The idiomatic way to read a sequence of white-space separated integers in C++ would be

#include <iostream>
using namespace std;
int main () {
    int input;
    while (cin >> input) {
        cout << input + 1 << endl;
    }
    return 0;
}

Let's come to why this works the way it does.

cin >> input

discards possible white-space and then reads an integer from the standard input. Moreover, it returns a reference of the cin object used. With this returned reference, you can call the bool conversion operator to check if any of the fail bits is set in the given std::basic_ios object. So

while (cin >> input)

does two things. Read an integer from standard input and check if it successfully completed the operation. As long as this operation succeeds, your while loop continues to iterate and you print the numbers.

See http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt for reference on std::basic_istream::operator>>.

See http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool for reference on operator bool.

Edit1: Move references to bottom so that the answer is more readable.





Comments (2)

  • +0 – THIS IS EXACTLY THE ANSWER I'VE BEEN NEEDING. thank you thank you — Oct 29, 2017 at 07:12  
  • +0 – Agreed, except you shouldn't use using namespace std;. — May 17, 2018 at 12:12