My C++ program runs however it does not display the proper output

Viewed 34

I am writing a program which reads a set of values, determines the leftmost digit of each number, and outputs how many times each digit appeared along with the percentage. However when I run the program, it works but doesn't keep track of the numbers. Code:

    #include <iostream>
    #include <cmath>
    #include <math.h>
    #include <fstream>
    #include <vector>
    #include <stdio.h>
    #include <algorithm>
    using namespace std;
    using std::vector;
    void benford(int arr[], int &size);
    int main() {
            std::vector<int> x;
            ifstream inputFile("num.txt");
            if (inputFile.good()) {
                    int current_number = 0;
                    while (inputFile >> current_number) {
                    x.push_back(current_number);
                    }
                    inputFile.close();
                    int p = sizeof(x) / sizeof(int);
                    std::vector<int> x;
                    int z[x.size()];
                    benford(z, p);
            }
    }
    void benford(int arr[], int &size) {
            int count1 = 0;
            int count2 = 0;
            int count3 = 0;
            int count4 = 0;
            int count5 = 0;
            int count6 = 0;
            int count7 = 0;
            int count8 = 0;
            int count9 = 0;
            for (int i = 0; i < size; i++) {
                    int j = log10(arr[i])/pow(10,(arr[i]));
                    if (j == 1) {
                            count1++;
                    }
                    else if (j == 2) {
                            count2++;
                    }
                    else if (j == 3) {
                            count3++;
                    }
                    else if (j == 4) {
                            count4++;
                    }
                    else if (j == 5) {
                            count5++;
                    }
                    else if (j == 6) {
                            count6++;
                    }
                    else if (j == 7) {
                            count7++;
                    }
                    else if (j == 8) {
                            count8++;
                    }
                    else {
                            count9++;
                    }
            }
            float perc1 = count1 / size;
            float perc2 = count2 / size;
            float perc3 = count3 / size;
            float perc4 = count4 / size;
            float perc5 = count5 / size;
            float perc6 = count6 / size;
            float perc7 = count7 / size;
            float perc8 = count8 / size;
            float perc9 = count9 / size;
            cout << "Digit 1 appeared " << count1 << " times, or " << perc1 << "%" << endl;
            cout << "Digit 2 appeared " << count2 << " times, or " << perc2 << "%" << endl;
            cout << "Digit 3 appeared " << count3 << " times, or " << perc3 << "%" << endl;
            cout << "Digit 4 appeared " << count4 << " times, or " << perc4 << "%" << endl;
            cout << "Digit 5 appeared " << count5 << " times, or " << perc5 << "%" << endl;
            cout << "Digit 6 appeared " << count6 << " times, or " << perc6 << "%" << endl;
            cout << "Digit 7 appeared " << count7 << " times, or " << perc7 << "%" << endl;
            cout << "Digit 8 appeared " << count8 << " times, or " << perc8 << "%" << endl;
            cout << "Digit 9 appeared " << count9 << " times, or " << perc9 << "%" << endl;
    }

Output:

  • Digit 1 appeared 0 times, or 0%
  • Digit 2 appeared 0 times, or 0%
  • Digit 3 appeared 0 times, or 0%
  • Digit 4 appeared 0 times, or 0%
  • Digit 5 appeared 0 times, or 0%
  • Digit 6 appeared 0 times, or 0%
  • Digit 7 appeared 0 times, or 0%
  • Digit 8 appeared 0 times, or 0%
  • Digit 9 appeared 6 times, or 1%

P.S. I know this code is by no means pretty, feel free to roast, but I have a lot of other assignments and I'm just trying to get it to run properly. Any help is very much appreciated.

0 Answers
Related