Hey guys, ive been working on this code for the past couple of hours and I am trying to do the following problem.

Write a program that reads the le generated by randGen.cpp (in Problem 1) and stores the
numbers in an array of type int. You can assume that there are fewer than 100 entries and
your program should determine the actual number of integers in the le (DO NOT USE THE
VALUE OF X FROM PROBLEM 1). The output should be a well-formatted two-column
list. The rst column is a list of distinct array elements; the second column is the count of
the numbers of occurrences of each element. You should also output the total of the count
column (which should be same as the number of integers in the input le).
For example, for the following numbers in the le:
-12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
the output should be:
-12 4
3 3
4 2
1 4
-1 1
2 2
Total = 16

Here is my code

#include <iostream>
#include <fstream>
#include <cstdlib>

using std::ifstream;
using std::ofstream;
using namespace std;

const int MAXNUM = 100;
char filename[16];
int values[MAXNUM];
int frequency[40];

void OpenNCheck( );
int ReadNCount(int values[], int frequency[]);
void Display(int values[], int frequency[], int numberOfValues);



int main( )
   int numberOfValues;
   ifstream inStream;
   OpenNCheck( );
   ReadNCount(values, frequency);
   Display(values, frequency, numberOfValues);
   system ("Pause");
   return 0;
   void OpenNCheck( )
   cout << "Which file? \n";
   cin >> filename;
   if(ifstream.fail( ))
       cout << "Error opening input file. Closing...\n";


int ReadNCount(int values[], int frequency[])
   int ct = 0; //short for Count
   ifstream >> values[ct];       //reads in the number
   frequency[values[ct]+20]++;  //adds one to the position that was inputted, which is 20 higher cuz array position cant be negative

   //IMPORTANT RUDIMENT: this first statement was used to ensure ct records the correct number of values. perform this function in your head and you'll see why it's necessary

   while(!ifstream.eof) //means that while inputting values it is not the End Of File
       ifstream >> values[ct];       
   return ct;


void Display(int values[], int frequency[], int numberOfValues)
   int ct = 0;
   while (ct < numberOfValues)
      cout << values[ct];
      cout.width(7); // puts 7 spaces between values[ct] and what's next...
      cout << frequency[ct] << endl;

I get a few compiler errors...

C:\Users\Preston\Documents\occur.cpp: In function `void OpenNCheck()':
C:\Users\Preston\Documents\occur.cpp:43: error: expected primary-expression before '.' token
C:\Users\Preston\Documents\occur.cpp:44: error: expected primary-expression before '.' token

C:\Users\Preston\Documents\occur.cpp: In function `int ReadNCount(int*, int*)':
C:\Users\Preston\Documents\occur.cpp:62: error: expected primary-expression before '>>' token

C:\Users\Preston\Documents\occur.cpp:67: error: expected primary-expression before '.' token
C:\Users\Preston\Documents\occur.cpp:69: error: expected primary-expression before '>>' token

Can someone help me out here and point me in the right direction?


Edited by theblastedfool: n/a

6 Years
Discussion Span
Last Post by mattjbond

you need to declare stream objects of type ifstream.


ifstream inputfile;


ifstream is a class definition, not a concrete object which you can use. You have to declare an instance of the class to use it.

Looking again I see you have done so in you main. Pass this ifstream instance to your functions as a parameter maybe...

void OpenNCheck( ifstream& is )
   cout << "Which file? \n";
   cin >> filename;
   if(is.fail( ))
       cout << "Error opening input file. Closing...\n";


OpenNCheck( inputfile );

Edited by mattjbond: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.