reading list of integers from text file

I'm writing a program which first prompts the user to input the name of a text file to open which contains a list of integers. The numbers must then be sorted. I then have to count the occurrences of each number entered and display the number of occurrences of each integer next to the sorted list of numbers.

Here is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{   

cout << "Please enter the name of the file you would like to open: ";    

    char fileName[50];
    
    ifstream input;
    cin.getline(fileName, 50);
    input.open(fileName);    
    
    if (!input.is_open())
    {
       cout << "File cannot be found." << endl;
       system("pause");
       exit(EXIT_FAILURE);
}
    
    char list[50];
    
    input >> list;
    
    while(input.good())
    {
     
    cout << list << " ";
    input >> list;
    
    }

system("pause");
return 0;
}


I'm wondering if I'm off to the right start with this. I know I'm using char as the array and I have a feeling that could be wrong. I only need to use one array with this program. I'm confused on how to use the implemented list of integers with the array I have in the sorting and counting of occurrences. If someone could point me in the right direction on how to use the numbers from the list with a sort and occurrence count function, I think I would be able to do this right.

Much appreciated.
Last edited on
bump
Two main corrections, change char list[50] to int list[50] or else you're going to just be writing literally 50 numbers, with 100 being equal to three. Another thing you should use is either the [] operator with your list, or you need to step the array, which is essentially a pointer, so that you're not just constantly overwriting the same values that you received from your file.
Okay. When I change list to an int I get this error: "no match for 'operator>>' in 'input >> word". Should I create another operator for list as well?

To sort the list of numbers and count them, will I have to create two seperate functions to do so? Will I be using for loops? This is what is really confusing me for some reason. It would be great if I could see some code on how to start these.
Okay. When I change list to an int I get this error: "no match for 'operator>>' in 'input >> word". Should I create another operator for list as well?


That's because you're passing an int pointer. You need to pass an int to cout, which would be indicated by the array accessor operator [], for example:
1
2
3
for (int i = 0; i < 50 && input.good; i ++)
   input >> list[i];
   cout << list[i];


To sort the numbers, you're going to be passing the entire array, you'll also need a sort algorithm to work on the array. Sometimes it's easier to use a second array with the same size to store the sorted elements. Bubble sort is typically the most commonly used one since it's the easiest to implement, and yes, you'll need loops to make sure you're properly sorting it.

Further reading: http://en.wikipedia.org/wiki/Bubble_sort

Once you understand how it works, you can attempt to design a function that will sort it for you. There is also some pseudo code so you can see what it could possibly look like. It's pretty easy to implement, however, it's not the fastest/best sort algorithm
Topic archived. No new replies allowed.