0

I am trying to define a function that will open a text file of numbers and read it all the way through. As the function reads each line, it should read the line as a string and then through conditionals add +1 to each counter for the different possible digits in the FIRST index spot (i.e. value 0).

Here is an example text file:

1245
64356
345
12
863

My program should output something like this:

1: 2
2: 0
3: 1
4: 0
5: 0
6: 1
7: 0
8: 1
9: 0

I think I am stuck at having the text file line be >> to a string variable and then having that string be compared to a character/string value. I tried a while loop using (!filename.good), but switched to a for loop in hopes of getting it to work.

Here is my code as follows - any help or constructive criticism would be greatly appreciated.

void analyzeData(std::string filename)
{
    // declare the local filestreams i will be using in the function.
    std::ifstream any_file_from_Main;

    // assign actual file data to local filestreams
    any_file_from_Main.open(filename.c_str());

    //conditional to check if files opened, output error if they dont
    if (!any_file_from_Main.good())
    {
        std::cout << "File did not open correctly." << std::endl;
        exit(-1);
    }

    double sum;
    std::size_t first_digit = 0;

    //declare all counter variables
    double one;
    double two;
    double three;
    double four;
    double five;
    double six;
    double seven;
    double eight;
    double nine;

    //input the first line into string first_digit
    any_file_from_Main >> first_digit;

    //continue this loop while the file is not at the end
    for (int i = 0; !any_file_from_Main.eof(); sum++)
    {
        if (first_digit[i] == "1") {
            one++;
        } else if (first_digit[i] == "2") {
            two++;
        } else if (first_digit[i] == "3") {
            three++;
        } else if (first_digit[i] == "4") {
            four++;
        } else if (first_digit[i] == "5") {
            two++;
        } else if (first_digit[i] == "6") {
            six++;
        } else if (first_digit[i] == "7") {
            seven++;
        } else if (first_digit[i] == "8") {
            eight++;
        } else if (first_digit[i] == "9") {
            nine++;
        }

        // advances text file to next line and assigns value
        // to string first_digit
        any_file_from_Main >> first_digit;

    }

    // cout value of counter ints and percentages
}
jxh
  • 69,070
  • 8
  • 110
  • 193
  • Is this homework, by any chance? – jwismar Oct 04 '13 at 00:37
  • The counter variables should be `int` values... and you could just put them in array. Anyway, what is your program doing right now? – nhgrif Oct 04 '13 at 00:52
  • i currently have compilation errors for every conditional "Subscripted value is not an array, pointer, or vector" and Xcode has first_digit[i] highlighted. i am confused because as far as my logic takes me first_digit is a string and I am asking about the specific value in the 0 index of it and whether it is == to another string character – UIC Bio Engr Oct 04 '13 at 00:59
  • p.s. jxh how did you edit my post to look so good? thank you. – UIC Bio Engr Oct 04 '13 at 01:04
  • @UICBioEngr: Welcome to StackOverflow! I encourage you to read the [About](http://stackoverflow.com/about) page to understand how you can get the most out of this site. When you edit your question, there are WYSIWYG controls over the edit box. The `{}` button formats a block of text as code. – jxh Oct 04 '13 at 01:07
  • very cool, thank you for the link. – UIC Bio Engr Oct 04 '13 at 01:15

3 Answers3

0
#include <iostream>
#include <fstream>
#include <cstring>

int main ()
{
    char s[10];
    int count[10];
    std::ifstream infile("thefile.txt");

    for ( int i = 0; i < 10; i++)
    {
        count[i] = 0;
    }

    while (infile >> s)
    {
        count[s[0]-'0']++;
    }

    for ( int i = 0; i < 10; i++)
    {
        std::cout << i << ":" << count[i] << "\n";
    }
    getchar();
    return 0;
}

Results in the following output in stdout:

0:0
1:2
2:0
3:1
4:0
5:0
6:1
7:0
8:1
9:0
Phillip Ngan
  • 15,482
  • 8
  • 63
  • 79
  • i think you took my hypothetical example a little too literally. please read my post and try to teach me where my errors are. – UIC Bio Engr Oct 04 '13 at 01:05
0

If you can assume well behaved input, the following should work:

#include <iostream>
#include <string>

int main() {
  int digitCounter[10] = {0};
  std::string number;

  while (std::cin >> number) {
    ++digitCounter[number[0] - '0']; }

  for (int i = 0; i < 10; ++i) {
    std::cout << i << ": " << digitCounter[i] << "\n"; }

  return 0;
}
Adam Burry
  • 1,904
  • 13
  • 20
0

Your problem is you declare first_digit as:

std::size_t first_digit = 0;

Which is an integer type. Then you read it in as:

//input the first line into string first_digit
any_file_from_Main >> first_digit;

Note the comment says you think first_digit is a string. Then you use it as:

    if (first_digit[i] == "1") {
        one++;

But integers are not arrays. You should declare first_digit as a std::string (remove the = 0; or you will get a run-time exception) which has an operator[] defined.

The next thing is that you will have to change "1" to '1'. Otherwise you are comparing char* to char.

Adam Burry
  • 1,904
  • 13
  • 20
  • Woo! thank you so much for coming back to take another look. that makes a lot of sense, but i will have to go back to understand what size_t is exactly. also what char* and char differences are. – UIC Bio Engr Oct 04 '13 at 02:02
  • @UICBioEngr, documentation for `size_t` here: http://en.cppreference.com/w/cpp/types/size_t The difference between `char*` and `char` is that one is a pointer to `char` (a memory address if you like) and the other is an actual `char`, such as `'A'`. – Adam Burry Oct 04 '13 at 02:06