?
Solved

statistical analysis of a file of char

Posted on 2004-11-30
4
Medium Priority
?
247 Views
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;

    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;
}
0
Comment
Question by:kynphan
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 12713580
Because you  have a simicoln at the end of the if condition line
0
 
LVL 30

Expert Comment

by:Axter
ID: 12713583
if(sameChar);

The above line has a semicoln.
Remove the simicoln and make it the following:
                if(sameChar)
                {
                    data[j].count++;
                    foundSame = true;
                }
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12713599
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!
0
 

Author Comment

by:kynphan
ID: 12713793
wow, i feel stupid! i must be thinking about something else while i was doing this, thanks guys.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

840 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