Link to home
Start Free TrialLog in
Avatar of kynphan
kynphan

asked on

statistical analysis of a file of char

I having problem with my program, it is entering a if condition when I do not want it to. Below is my code. so far it's just supposed to count the occurences of each char.

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

void processFile(string str);

struct dataNode
{
    char charac;
    unsigned int count;
};

int main()
{
    string dataIn;
    string statString;
    int numOfUniqueChar = 0;
    ifstream inFile;//input file
    vector <dataNode> data(1);
    cout << "input file to do simple statistical analysis: ";
    cin >> dataIn;
    cout << "a string with no duplicates: ";
    cin >> statString;
    string dataString;

    inFile.open(dataIn.c_str());
    if(inFile.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }
   
    while(!inFile.eof())
        getline(inFile, dataString);
   
    cout << dataString << endl;
   
    char toBeComp = dataString[0];
    for(int i = 0; i < dataString.length(); i++)
    {
        if(numOfUniqueChar == 0)
        {
            numOfUniqueChar++;
            dataNode temp;
            temp.charac = toBeComp;
            temp.count = 1;
            data[0] = temp;
        }
        else
        {
            bool foundSame = false;
            for( int j = 0; j < numOfUniqueChar; j++)
            {
                bool sameChar = (data[j].charac == toBeComp);
                if(sameChar);//<<<<<<<<<<<<<<<<<<why is it entering this loop when my input file is a string "abc"
                {
                    data[j].count++;
                    foundSame = true;
                }
                if(foundSame)
                break;
            }
            if(!foundSame)
            {
                dataNode temp2;
                temp2.charac = toBeComp;
                temp2.count = 1;
                data[numOfUniqueChar] = temp2;
                numOfUniqueChar++;
            }  
        }
        toBeComp = dataString[i + 1];
    }
   
    for(int i = 0; i < numOfUniqueChar; i++)
    {
        cout << data[i].charac << " " << data[i].count << endl;
    }    

    return 1;
}
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if(sameChar);

The above line has a semicoln.
Remove the simicoln and make it the following:
                if(sameChar)
                {
                    data[j].count++;
                    foundSame = true;
                }
Hi kynphan,
>  if(sameChar);//<<<<<<<<<<<<<<<<<<why is it entering this loop when my

The semilcolon at the end of if ends your "if" statement
hence the next statement is always executed
It should be
 if(sameChar)

Cheers!
Avatar of kynphan
kynphan

ASKER

wow, i feel stupid! i must be thinking about something else while i was doing this, thanks guys.