counting lines , chars from an input file need help

This is a discussion on counting lines , chars from an input file need help within the C++ Programming forums, part of the General Programming Boards category; Problem 1, it does matter if ch is an int or char. It needs to be a char. Problem 2 ...

  1. #16
    Registered User
    Join Date
    Jan 2006
    Location
    Seattle
    Posts
    30
    Problem 1, it does matter if ch is an int or char. It needs to be a char.

    Problem 2 you define your variables twice. The ones you are using in the loop are different than the ones you are outputting. Delete your second declerations of your variables and change ch to a char and you'll start to be on the right track.

    Just so you know if you do something like this...

    Code:
    int i = 5;
    {
         int i = 3;
    
         i += 50;
         i--;
         //
         // whatever else you want to do with I
         //
    }
    cout << i;
    your output will be "5" because you made a new variable I within a new set of brakets (new scope). If you take out that second int i to have something like this....

    Code:
    int i = 5;
    {
         i = 3;
    }
    cout << i;
    The output will be "3" because no new I variable was declared in the scope.
    Last edited by Peter5897; 07-10-2006 at 05:32 PM.

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> Please be specific and not cryptic.
    Sorry, I'm not trying to be cryptic, just trying not to give you the exact answer. At this point the solution is really close, but I can't see that you have come up with anything on your own from your original post, so I hesitate to give you the exact answer.

    >> but it really doesnt matter if ch is assigned char or int.
    You don't need to change to ch to char from int, that was just an explanation of why inFile >> ch didn't work. Did you try something other than inFile >> ch to read in the character? I have given you three ideas, and I have told you that two of them won't work and why. Maybe you can figure out the other idea and try to get it to work.

    BTW, there are still other issues with your code. Think about what variables should be declared and set to 0 inside the loop, and which ones should be declared outside the loop. If you don't fix those issues, you won't be able to know which input method is working.

    >> Problem 1, it does matter if ch is an int or char. It needs to be a char.
    It won't work with inFile >> (because the logic expects to read a newline), so it doesn't need to be a char. The actual solution can work with either a char or an int depending on the version of the function that is called.
    Last edited by Daved; 07-10-2006 at 05:37 PM.

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    took out the varible declarations inside of loop. I see why my output was all zeros.... took out the main declarations added ones back into the loops. Still get zero output. this is only an excercise in a book. Man i am not getting it.

    always is something small that stumps me for awhile.

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> took out the main declarations added ones back into the loops.
    Why? Follow the logic of the program. In the code above, you start your program at the beginning of main. You create a bunch of variables and set their values to zero. Then, inside the loop, you create new variables with the same name. Those new variables are what are used inside the loop. Each time through the loop, you create them again and set them to zero, so nothing is "remembered" each time through. When the loop ends, the code outputs the values of the original variables from the start of main.

    If you remove the variable declarations from inside the loop, then when you start the program the original variables are created and set to zero. Each time through the loop they are updated by the loop code. When the loop finishes, they remember their values and so when they are printed out they show non-zero numbers.

  5. #20
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Here is the latest. There comes a point where hints do not work. I do not understand why my cout statements are printing what i set them to at the beginning. After the loop is done looping it should print the current values recorded. WHY!

    Thanks in advance for your help in advance.

    I need someone to state you went wrong RIGHT here to learn this stuff. I have been wrestling with this problem in an endless loop of my own.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    using namespace std;
    
    int main()
    {
        char ch;
        unsigned int i=0, line=0, minLine=0, minChars=65535, maxLine=0, maxChars=0, totChars=0;
        
    	ifstream inFile("input.txt", ios::in);
    
        if(!inFile)
            {
                cerr << "File could not be opened!" << "\n";
                exit(1);
    		}
    		inFile >> ch;
    
    		while(!inFile.eof())
    		{
    			i++;
    			inFile >> ch;
                          
                if ( ch == '\n')
                    {
                        line++; /* increment when a newline is found*/
                        totChars += i-1; /* Correction for \n (newline) character*/
    
                        if (i<minChars)
                            {
                                minChars = i;
    							minLine = line;
    						}
                            else if (i>maxChars || minChars>maxChars)
                                {
                                    maxChars = i;
                                    maxLine = line;
    							}
    							
    							i=0;
    				}
    
    		}
    		cout << "The input file has   " << line << "\n"; 
            cout << "The average number of characters per line is    " << totChars << "\n";
    		cout << "The line that is the shortest and its characters is  " << minLine << "   " << minChars << "\n";
    		cout << "The line that is the longest and its characters is   " << maxLine << "   " << maxChars << "\n"; 
    		return 0;
    }

  6. #21
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    .....

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Don't use inFile >> ch. It won't work with your code.

  8. #23
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    so what will work. please be more specific. two days in idle mode over a program is enough. Point out where my mistakes are

  9. #24
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I have already given you the answer. Sure, I've been a little bit cryptic, but it only takes about a minute of work on your part to find it. I've mentioned three times not to use inFile >> ch, and I've explained why even if you didn't understand why. Yet it is still in your code.

    I already said I specifically mentioned three different options, and so far we've deduced that two of them won't work with this code. If you can find in my posts the three that I mentioned and tell me what they are, and then perhaps tell me which two we know won't work, then you'll have found the third. If you're not familiar with that function, then you can look up how to use it and ask if you have a specific question about it. At that point I would give you an example that would help in your own code.

    You have done well to fix several of the other issues in your code, and when you change the inFile >> part, you will be much closer. You will get non-zero values, but they won't all be exactly right. At least then you'll be able to work on why they aren't quite right.

  10. #25
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Thanks for your help almost there

    Here is my latest code. I did the get command as you suggested. My book didnt have a good syntax for its usage. I now have the following input file to test the program.

    My minimum line and maximum line is off by one digit , for example line 2 says 17 should be 16.

    Any suggestions on fixing this. Also the average number of characters per line seems high to me.

    Program
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    using namespace std;
    
    int main()
    {
    	
    
        
        unsigned int i=0, line=0, minLine=0, minChars=65535, maxLine=0, maxChars=0, totChars=0;
    	
    	ifstream inStream;
    	inStream.open("input.txt", ios::in);
    
    	assert( inStream.is_open() );
    	
    	
    
    
        if(!inStream)
            {
                cerr << "File could not be opened!" << "\n";
                exit(1);
    		}
    		
    
    		while(!inStream.eof())
    		{
    			i++;
    			char ch;
    			ch = inStream.get();
    						
                          
                if ( ch == '\n')
                    {
                        line++; /* increment when a newline is found*/
                        totChars += i-1; /* Correction for \n (newline) character*/
    
                        if (i<minChars)
                            {
                                minChars = i;
    							minLine = line;
    						}
                            else if (i>maxChars || minChars>maxChars)
                                {
                                    maxChars = i;
                                    maxLine = line;
    							}
    							
    							i=0;
    				}
    
    		}
    		cout << "The input file has   " << line << "\n"; 
            cout << "The average number of characters per line is    " << totChars << "\n";
    		cout << "The line that is the shortest and its characters is  " << minLine << "   " << minChars << "\n";
    		cout << "The line that is the longest and its characters is   " << maxLine << "   " << maxChars << "\n"; 
    		return 0;
    }

    Input.txt
    Code:
    aaaaaaaaaaaaaaaa
    0100111101010010
    01000111
    0100010001000101
    01000011
    0100100001000101
    01011000
    0100010101001110
    01000100

    My output is:

    input file has 9 lines

    average number of characters per line is 112

    The line that is the shortest and its characters 3 9

    The line that is the longest and its characters 2 17

  11. #26
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    I figured it out , -1 on min and max and added a new varible called average -- totchars / line to get my average per line.

    Thanks david for putting up with me.

  12. #27
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Good job. I'm glad you got it working.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Beginner problem with counting lines in a file
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-25-2005, 05:47 PM
  5. Replies: 3
    Last Post: 06-25-2003, 04:29 PM

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