开发者

Reading from an input file

开发者 https://www.devze.com 2023-04-12 15:42 出处:网络
#include <iostream> #include <fstream> using namespace std; int main(int argc, char *argv[]) {
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[]) {
    int size = 0;
    int highNum = 0;
    int m;
    string fname;

    cout << "Supply the name of the input file you would like to use." << endl;
    cin >> fname;

    ifstream 开发者_Python百科input;
    input.open(fname.c_str());
    input >> size;
    int numbers[size];

    for (int n = 0; n < size; n++)
        input >> numbers[n];

    for (m = 0 ; m < size ; m++)
    {
        if (numbers[m] > highNum)
            highNum = numbers[m];
    }
    int j;
    int k;
    bool values[] = {false, false, false, false, false, false};

    for (j = highNum; j > 0 ; j--)
    {
        for (k = size - 1 ; k >= 0 ; k--)
        {
            if (j <= numbers[k])
                values[k] = true;
        }

        if (values[0])
            cout << "| xxx";
        else
            cout << "|    ";
        if (values[1])
            cout << "   +++";
        else
            cout << "      ";
        if (values[2])
            cout << "   ***";
        else
            cout << "      ";
        if (values[3])
            cout << "   ---";
        else
            cout << "      ";
        if (values[4])
            cout << "   +++";
        else
            cout << "      ";
        if (values[5])
            cout << "   +++" << endl;
        else
            cout << "      " << endl;

    }

    return 0;
}

I am trying to write a code to print a bar chart by reading integers from a separate text file. I posted the complete code, but I know the bottom half works if you just input numbers. I was wondering what the problem with this is given that there is a data file in the same directory. When I run this program and enter the name of the data file i created to test it, the program runs, but no graph is produced.


Your program works for me. I suppose your input file isn't present in the current working directory of your process. I suggest you add a check to see if input is valid after the call to open.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号