Counting program

This is a discussion on Counting program within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I am in the process of a program that counts integers I can do this by looking for ...

  1. #1
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133

    Counting program

    Hey guys, I am in the process of a program that counts integers I can do this by looking for the int keywork and then incrementing a variable although if an integers are implemented as:

    int j,k,l;

    I am not sure how to count this as 3 ints, I know I will need some form of loop that reads until the next comma and ends at the semicolon but I have no idea what kind of loop would be of use or how to implement it.

    Any advice would be great,

    Thank you.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Ok, what I would do: I would first read in the whole line, after that I would make sure we are dealing 1 or more int-variables. Now after that I would count the number of commas, as you may notice, the number of variables equals the number of commas+1. For that you only need 1 loop that goes something like this (pseudocode)
    Code:
    Read line
    if line is creating variables   // Is it a line creating variables??
      // We want 1 because we know we have at least 1 variable
      variableCount = 1    
      for i = offsetToFirstVarName; i<sizeOfLine || line[i] == ';'; i++
        if line[i] == ','
          variableCount++
    That's how i would do it.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Start off by reading whitespace seperated words until you come across "int". Then, call getline(file,buffer,';'); or the equivalent to read into a string buffer from your file up to the next ';' character. In the example you gave above, this would result in buffer containing " j,k,l". Next, initialize a stringstream with that buffer. Then, use a while loop calling getline from your stringstream buffer into some temp/garbage string with ',' as the character delimiter. The body of the while loop should just a call to increment the counter.

    There can be complications however, consider what might occur if your file contains the following:
    Code:
    cout << "Hello int a,b,c" << endl;
    Or...
    Code:
    #define myvar int
    float foo;
    myvar j,k,l;
    Or...
    Code:
    int foo(int bar1,int bar2)
    {
        float bar4;
        int bar3;
        ...
    }
    Your program can quickly become much more complex if you need to account for all these (and more) situations.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Quote Originally Posted by Shakti
    Ok, what I would do: I would first read in the whole line, after that I would make sure we are dealing 1 or more int-variables. Now after that I would count the number of commas, as you may notice, the number of variables equals the number of commas+1. For that you only need 1 loop that goes something like this (pseudocode)
    Code:
    Read line
    if line is creating variables   // Is it a line creating variables??
      // We want 1 because we know we have at least 1 variable
      variableCount = 1    
      for i = offsetToFirstVarName; i<sizeOfLine || line[i] == ';'; i++
        if line[i] == ','
          variableCount++
    That's how i would do it.
    Hi so would it be something like this?

    Code:
    char array [200];
    ifstream file;
    
    file.open(filel); 
    
    	if (file.is_open()) 
    
    	{
                 while (file.getline( array, sizeof(array) ))
    	          {
                           	if ( strstr ( array, "int" ) != 0 )
    			{
    				fint++;
                              for (i =0, i <sizeof(array) || line [i]==';' ; i++ // not sure what line[] is
    
                                if (line[i]==',')
                                     fint++
                               }
                 }
    This doesn't work but am I getting close?
    Last edited by 182; 02-17-2006 at 06:55 PM.

  5. #5
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Hey guys Im still really having problems with this code could someone point out where I have went wrong? Thanks.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    You're not supposed to bump threads.

    Code:
    for (i =0, i <sizeof(array) || line [i]==';' ; i++ // not sure what line[] is
    ->
    Code:
    for (i =0; i <sizeof(array) || line [i]==';' ; i++) // not sure what line[] is
    Does that help?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    I now have the following code but its still not working, any suggestions?
    Code:
    char array [200];
    ifstream file;
    
    file.open(filel); 
    
    	if (file.is_open()) 
    
    	{
                 while (file.getline( array, sizeof(array) ))
    	          {
                           	if ( strstr ( array, "int" ) != 0 )
    			{
    				fint++;
                              for (i =0; i <sizeof(array)  ; i++)
    
                                if (array[i]==',')
                                     fint++
    				if (array[i]==';')
                                     break;
                               }
                 }
    Last edited by 182; 02-18-2006 at 07:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with people counting program..
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 06-30-2006, 01:31 AM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. Replies: 5
    Last Post: 01-31-2006, 12:54 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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