can someone tell me why nothing happens when I run this(codeblocks)program on my mac?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joeyke
    New Member
    • Feb 2010
    • 15

    can someone tell me why nothing happens when I run this(codeblocks)program on my mac?

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cctype>
    using namespace std;
    
    //function prototypes
    void initialize(int [], int);
    int readData(char [], int, int [], int);
    void printResults(char [], int, int [], int, int);
    
    //constant global variables used for arrays
    const int LIMIT = 27;
    const int ALPHA = 26;
    int main()
    {
        char alphabet[ALPHA];
        int count[LIMIT];
        int total;//holds the total number of characters in the file
    
        //sets each letter in the alphabet to a subscript in the alphabet array
        alphabet[0] = 'A';
        alphabet[1] = 'B';
        alphabet[2] = 'C';
        alphabet[3] = 'D';
        alphabet[4] = 'E';
        alphabet[5] = 'F';
        alphabet[6] = 'G';
        alphabet[7] = 'H';
        alphabet[8] = 'I';
        alphabet[9] = 'J';
        alphabet[10] = 'K';
        alphabet[11] = 'L';
        alphabet[12] = 'M';
        alphabet[13] = 'N';
        alphabet[14] = 'O';
        alphabet[15] = 'P';
        alphabet[16] = 'Q';
        alphabet[17] = 'R';
        alphabet[18] = 'S';
        alphabet[19] = 'T';
        alphabet[20] = 'U';
        alphabet[21] = 'V';
        alphabet[22] = 'W';
        alphabet[23] = 'X';
        alphabet[24] = 'Y';
        alphabet[25] = 'Z';
    
        initialize(count, LIMIT);//call funtion
        total = readData(alphabet, ALPHA, count, LIMIT); //call function
        printResults(alphabet, ALPHA, count, LIMIT, total); // call function
        //system("pause");
        return 0;
    }
    
    //this function initializes the counts in the array to 0. It accpes the count
    //array and the number of elements in the array, which is n.
    void initialize(int count1[], int n)
    {
        for(int i=0; i<n; i++)
           count1[i] = 0;
    }
    
    //this function opens the input file and reads a character at a time using the
    //get() function. It reads from the file until there is no more information to
    //be read, in which case the while loop ends. this function accepts the alphabet
    //array, the number of elements in that array, which is c, the count array, and
    //the number of elements in that array, which is m.
    int readData(char brr[], int c, int count2[], int m)
    {
        char x;
        int counter = 0;
    
        ifstream infile;
        infile.open("jkprogram.txt");//opens the input file
        infile.get(x);//reads a character from the file
        x = toupper(x);
        while(infile) //reads from the file until there is nothing else to be read
          {
             for(int z=0; z<c; z++)//goes through every letter in the array
               {
                  //if the read in character matches a letter in the alphabet
                  if(x == brr[z])
                    count2[z]++; //increments the count if there is a match
               }
             //tests if the same read in character is not a letter or white space
             if(isalpha(x) == false && isspace(x) == false)
               //if not a letter or white space, increments the count for "other",
               //which is the last subscript in the count array. Notice the
               //subscript in the count array is 1 more than the alphabet array.
               count2[m-1]++;
             if(isspace(x) == false)//tests if read in character is a white space
               counter++; //keep track of total number of characters
             infile.get(x);//read in next character
             x = toupper(x);//changes the read in character to uppercase, which in
                            //this case ignores the case of the letter
          }
        infile.close(); //closes the file
        return counter;//returns the total number of characters in the file
    }
    
    //this function displays the letters and their counts.  It accepts the alphabet
    //array, which is crr, the number of elemetns in that array, which is d, the
    //count array, which is count3, the number of elements in that array, which is
    //p, and the total number of characters in the file, which is counttotal.
    void printResults(char crr[], int d, int count3[], int p, int counttotal)
    {
        ofstream outfile;
        outfile.open("histogram.txt"); //opens the output file
        float percent;
    
       
        outfile.setf(ios::fixed, ios::floatfield);
        outfile.precision(2);//sets the number of decimal places to be shown
        outfile.width(15);//sets the width of where to display the information
        outfile << "Letter";
        outfile.width(15);
        outfile << "Occurrences";
        outfile.width(15);
        outfile << "Percentage" << endl;
        outfile << "--------------------------------------------------------------";
        outfile << endl;
        for(int b=0; b<d; b++)//goes through the elements in the array
          {
             outfile.width(15);
             outfile << crr[b];//displays each letter in the alphabet array
             outfile.width(15);
             outfile << count3[b];//displays the count of each letter
             outfile.width(15);
        
             percent = static_cast<float>((count3[b]*100))/counttotal;
             outfile << percent << "%" << endl;
          }
         
          outfile.width(15);
          outfile << "Other";
          outfile.width(15);
          outfile << count3[p-1]; //displays the last count in the count array
          outfile.width(15);
       
          percent = static_cast<float>((count3[p-1]*100))/counttotal;
          outfile << percent << "%" << endl;
    
        outfile.close();//closes the output file
    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Works for me, it is probably not finding your input file after attempting to open a file you should check to see if it has opened and output an error message if it has not.

    Comment

    Working...