Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,233 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,798 people online right now. Registration is fast and FREE... Join Now!




C++

 
Reply to this topicStart new topic

C++, Array to count the frequency

jasy
19 Mar, 2007 - 11:10 PM
Post #1

New D.I.C Head
*

Joined: 19 Mar, 2007
Posts: 2


My Contributions
This is wut i came up with until now but i m unable to correct the errors i have no clue wut is wrong here

//File: array.cpp
//PURPOSE: To create a program that reads input from a text file
//and counts the frequency of all characters in the file.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
using namespace std;

const int a = 91;
void PrintOccurrences(const int[]); // prototype

int main ()
{
int freq_count[a];
char character, index;
ifstream myinput;
ofstream myoutpt;

myinput.open();
if (myinput.fail())
{
cout << "Input file opening failed. \n" << endl;
exit(1);
}
for(int i = 0; i < a; i++)
{
freq_count[i] = 0;
}
while(myinput)
{
if (isalpha ( ch ) )
{
if ( islower ( ch ) )
ch = toupper ( ch ) ;

freqCount [ ch ] = freqCount [ ch ] + 1 ;
}
dataFile. get ( ch ) ; // get next character
}
PrintOccurrences ( freqCount ) ;
return 0;
}
void PrintOccurrences(/* in */ const int freqCount[])

// Prints each alphabet character and its frequency
// Precondition:
// freqCount [ ‘A’ . . ‘Z’ ] are assigned
// Postcondition:
// freqCount [ ‘A’ . . ‘Z’ ] have been printed
{ char index ;

cout << “File contained “ << endl ;
cout << “LETTER OCCURRENCES” << endl ;
for ( index = ‘A’ ; index < = ‘Z’ ; index ++ )
{
cout << setw ( 4 ) << index << setw ( 10 )
<< freqCount [ index ] << endl ;
}
}



Attached File(s)
Attached File  myinput.txt ( 2.21k ) Number of downloads: 25
User is offlineProfile CardPM
+Quote Post

illello
RE: C++
20 Mar, 2007 - 12:29 AM
Post #2

New D.I.C Head
*

Joined: 9 Sep, 2006
Posts: 7


My Contributions
I see that you are missing variables declariation, incorrect use of some function, incorrect use of variable name declared and so on. You have just easy compile error, start correcting them.
User is offlineProfile CardPM
+Quote Post

jasy
RE: C++
20 Mar, 2007 - 01:12 PM
Post #3

New D.I.C Head
*

Joined: 19 Mar, 2007
Posts: 2


My Contributions
QUOTE(illello @ 20 Mar, 2007 - 01:29 AM) *

I see that you are missing variables declariation, incorrect use of some function, incorrect use of variable name declared and so on. You have just easy compile error, start correcting them.

hey i actually fixed all that too but it is still giving me errorsfor setw i dont understand wut to do with it or probably look at the following i made this code it works fine but i dont know wut i did tht instead of giving me my desired output its giving me the location of my assignment blink.gif crazy.gif
CODE

#include <iostream>
#include <fstream>
#include <map>
#include <algorithm>
using namespace std;

struct print_it
{
  void operator() ( const pair<char,int>& p ) const
  {
      cout << p.first << " occurs " << p.second << " times\n";
  }
};

int main( int argc, char** argv )
{
  if( argc != 2 )
  {
     cout << "usage: " << argv[0] << "file_name\n";
     return 1;
  }
  ifstream myinput( argv[1] );
  if( !myinput)
  {
     cout << "could not open file " << argv[1] << '\n';
     return 2;
  }
  map<char,int> char_count;
  char c;
  while( myinput >> c ) ++char_count[c];
  for_each( char_count.begin(), char_count.end(), print_it() );
  return 0;
}

User is offlineProfile CardPM
+Quote Post

born2c0de
RE: C++
20 Mar, 2007 - 11:13 PM
Post #4

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,914



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
It works fine for me.
But I removed the Command-line parameters and added the Filename directly.

What output are you getting?

If the Assignment path is shown, then the argv[0] is being displayed.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:37PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month