My 'Count' aint working...:-(

This is a discussion on My 'Count' aint working...:-( within the C++ Programming forums, part of the General Programming Boards category; I have some data in a txt file, that I read into an array. Then I want to iterate through ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    My 'Count' aint working...:-(

    I have some data in a txt file, that I read into an array. Then I want to iterate through the array, counting the number of occurances EACH value has; eg if the value 50 occurs 6 times in the txtx file, the variable count 'should show this. As each value has been counted, I want to plot that value on a chart, count then set back to zero, the next value counted, plotted etc... ( but that is imaterial for now...).....I have a loop in a loop, and its getting messy...I can prove that the values from the txt files are correctly being read into the array, but the im doing something wrong with the loop, as Count always shows 100, the total number of values in the txt file....anyway, here's the code, and any help would be appreciated:

    Code:
    ifstream InFile; 
    int Index;  
    
    
    InFile.open("TMAData1.dat") ;
    
    for( Index = 0; Index < 100; Index = Index + 1 ) //enter loop
     {
             
    
                    InFile >>  Store1[Index] >> ws  ; 
     };
    
    InFile.close();
    
    
    
                int x, n;
          int Count;
                  for ( x=0; x<100; x++)
    
    
                    {    Count = 0;
                            for ( n=0; n<100; n++)
    
                                    {
                                    if (Store1[x] == Store1[n])
    
                                            {
                                              Count++; Store1[n] = 0;
                                            }
                                    else{}
    
                                        /*the plot for each iteration will be done      here  */                              }
    
                                            }
          }

  2. #2
    Registered User codegirl's Avatar
    Join Date
    Jun 2003
    Posts
    76
    Well, for starters, don't put a semicolon after the closing curly brace of your first for loop:

    Code:
    for( Index = 0; Index < 100; Index = Index + 1 ) //enter loop
     {
             
    
                    InFile >>  Store1[Index] >> ws  ; 
     };  // <--- No semicolon!!
    Also the statement

    else{}

    is unneccessary, you can have an if without an else!

    At first glance that's all I see that's wrong, but it's still very possible there can be something I don't see! Get rid of the semicolon and see if that helps.
    My programs don't have bugs, they just develop random features.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    thx guyz

    .....salem....tested and yur code is spot on....still cant see WHAT EXACTLY it is that is different to mine that makes it work...!! ...can you give a brief explanation of where I was going wrong..??

    THx .....maybe I will take a trip to int main !!

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    ...thx again...and NO, my code aint QUITE that bad in the editor ( although it could be better, I know )

    ...back to the rest of the problem....I might be back soon...!!


  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    5

    More 'help' needed !!!

    ....I knew it wouldnt be long b4 I needed some more help....

    ....if I place the moveTo and LineTo methods in an OnClick method of a button, it draws the line, but by placing the same code in the OnCreate method of a form, it does nothing....subsequently , it thus also does nothing when these methods are used tom plot data from the aray ( that started this thread question )....is it something simple, or am I doing something crazy still....

    ...hopefully the code is cleaner now..!!

    Code:
    Canvas -> MoveTo(0,0);//set initial starting point//
    
    int x, y, count;
    
    for ( x = 0 ; x < 1 ; x=x+1 ) {
        if ( Store1[x] == 0 ) continue;
        count = 1;
        for ( y = x+1 ; y < 100 ; y=y+1 ) {
            if ( Store1[x] == Store1[y] ) {
                Store1[y] = 0;
                count=count+1;
            }
            int n = count * 5;
        Canvas -> MoveTo(ClientWidth - 150 , ClientHeight );
        Canvas -> LineTo (ClientWidth - 150, n);
    
        }
    
    }
    ...please note that the c0-ordinates are not set at the moment, and the drawing element is not set up....but its the principal, that it draws NOTHING.....

    ...using Borland by the way....

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    ...I think that I have sorted it, sorry,.....

    ...basically, I propogate the array when the form loads, but placed the iterating count and drawing properties in the OnClick method of a button that the user has to click to obtain the results and it sort-of-works...

    ....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. Why is this function not working? Sigh...
    By musique in forum C++ Programming
    Replies: 16
    Last Post: 05-03-2009, 04:54 PM
  3. Sorting function not working
    By musique in forum C++ Programming
    Replies: 13
    Last Post: 05-03-2009, 04:38 PM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21