Jump to content

[C++] Reading Single Integers from Input Stream


Gighalen

Recommended Posts

#include <iostream>

#include <iomanip>

#include <string>

#include <fstream>

 

using namespace std;

 

int main(){

    string inputFile;

    ifstream inFile;

    int count = 0;          // keep track of number of integers read

    int numbers[19];        // array to store our integers

 

    cout << "Enter the name of the data file now: " << endl;

    cin >> inputFile;

        inFile.open(inputFile.c_str());

       

        while(!inFile){

                cout << "==> Error opening file: " << inputFile << endl;

                cout << "==> Try Again..." << endl;

               

                inFile.clear();

               

                cout << fixed << "Enter the name of the data file now: ";

                cin >> inputFile;

                cout << inputFile << endl << endl << endl;

       

                inFile.open(inputFile.c_str());

        }

        while(count <= 19 && inFile >> numbers[count]){

                cout << numbers[count] << endl;

                count++;

        }

 

    system("pause");

    return 0;

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.