statistical analysis of a file of char

Posted on 2004-11-30
Last Modified: 2010-04-01
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;;
        cout << "Input file opening failed.\n";
        getline(inFile, dataString);
    cout << dataString << endl;
    char toBeComp = dataString[0];
    for(int i = 0; i < dataString.length(); i++)
        if(numOfUniqueChar == 0)
            dataNode temp;
            temp.charac = toBeComp;
            temp.count = 1;
            data[0] = temp;
            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"
                    foundSame = true;
                dataNode temp2;
                temp2.charac = toBeComp;
                temp2.count = 1;
                data[numOfUniqueChar] = temp2;
        toBeComp = dataString[i + 1];
    for(int i = 0; i < numOfUniqueChar; i++)
        cout << data[i].charac << " " << data[i].count << endl;

    return 1;
Question by:kynphan
    LVL 30

    Accepted Solution

    Because you  have a simicoln at the end of the if condition line
    LVL 30

    Expert Comment


    The above line has a semicoln.
    Remove the simicoln and make it the following:
                        foundSame = true;
    LVL 10

    Expert Comment

    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


    Author Comment

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

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now