what am i doing wrong?

This is a discussion on what am i doing wrong? within the C++ Programming forums, part of the General Programming Boards category; Here is the assignment i havent gotten to the puttin stuff in the stat txt i am just focused on ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    11

    what am i doing wrong?

    Here is the assignment i havent gotten to the puttin stuff in the stat txt i am just focused on makin output right.

    Design and implement a Visual C++ .NET program that numbers the lines of a text file and keeps some statistics about the text file. Your program should read lines of text from the file source.txt and output each line to the screen and to the file output.txt. Each line of output should be preceded by a line number. Print the line number at the beginning of the line, right justified in a field of three spaces. Follow the line number by a colon, then one space, then the line of text. You should remove any spaces at the beginning of a line (hint: read the input file one character at a time using <your_ifstream>.get(ch) and use <your_ifstream>.putback(ch)). Lines that are longer than 50 characters should be broken into multiple lines. Once you reach 50 characters in a line, insert a newline at the end of the current word (as soon as you read a space). This new line should not get a line number, but should be indented properly. Your program should also keep the following statistics about the text file: the number of lines, the number of words (assume words are separated by spaces and newlines, in other words, punctuation is part of the word), The length of the longest word and the length of the shortest word. These statistics should be output to the file stats.txt.

    And here is the code:

    Code:
    int main ()
    {
    	char i;
    	int count=1, c=0;
    
    	ifstream input;
    	ofstream stats;
    	ofstream output;
    	input.open("source.txt");
    	output.open("output.txt");
    	stats.open("stats.txt");
    	
    // yet to figure out how to loop it right 
    //so it stops after reading entire txt file so i did the count for now
    	while (count < 5)
    	{
    		input.get(i);
    
    		if (c < 1)
    		{
    			cout << "   " << count << ": " << i;
    			output << "   " << count << ": " << i; 
    		}
    		if (c >>1)
    		{
    			cout << i;
    			output << i;
    		}
    		do
    		{
    			if ( c == 50)
    			{
    				cout << '\n' << "     " << i;
    				output << '\n' << "     " << i;
    			}
    			
    			cout << i;
    			output << i;			
                     }while (c >= 50 & i != '\n');
    
    		c = c + 1;
    
    		if (i == '\n')
    		{
    			count = count + 1;
    			c = 0;
    		}
    	}
    
    
    
    	return 0;
    }
    edit: it goes to an infinite loop when reaching the do-while loop
    Last edited by themexican; 10-18-2005 at 02:40 AM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    That c >> 1 is probably not what you wanted that would be a right bitshift.
    Woop?

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    ya i noticed that, but i mean why is it going into an infnite loop when hittin that do-while statement

    Code:
    do
    {
          if ( c == 50)
          {
                 cout << '\n' << "     " << i;
                 output << '\n' << "     " << i;
           }
    			
            cout << i;
            output << i;
            c = c + 1;
    
    }while ((c >= 50) & (i != '\n'));

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    Because nothing in that loop will make either of those conditions false. Nothing in i is changing and c is doing nothing also you have another typo in there it should be && not &. That would be a bitwise and. I am pretty sure that's not what you want.
    Woop?

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    oh duh thank you, but for some reason it is still loopin, why isnt it reading the end of the line and stoppin, that is what needs to go false, i some reason never reaches an end line. but the txt file has an end of line.

    Code:
    do
    		{
    			input.get(i);
    
    			if ( c == 50)
    			{
    				cout << '\n' << "     " << i;
    				output << '\n' << "     " << i;
    			}
    			
    			cout << i;
    			output << i;
    			c = c + 1;
    
            }while ((c >= 50) && (i != '\n'));

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    Well this goes for me
    Code:
    #include <iostream>
    #include <fstream>
    
    int main ()
    {
        using namespace std;
    
        char i;
        int count=1, c=0;
    
        ifstream input;
        ofstream stats;
        ofstream output;
    
        input.open("source.txt");
        //Check to see if our files open
        if(!input)
        {
            cerr<<"Could not open source.txt"<<endl;
            return -1;
        }
        output.open("output.txt");
    
        if(!output)
        {
            cerr<<"Could not open output.txt"<<endl;
            return -1;
        }
    
        stats.open("stats.txt");
    
        if(!stats)
        {
            cerr<<"Could not open stats.txt"<<endl;
            return -1;
        }
    
        while (count < 5)
        {
            input.get(i);
            if (c < 1)
            {
                cout << "   " << count << ": " << i;
                output << "   " << count << ": " << i; 
            }
            if (c > 1)
            {
                cout << i;
                output << i;
            }
            do
            {
                input.get(i);
    	
                if ( c == 50)
                {
                    cout << '\n' << "     " << i;
                    output << '\n' << "     " << i;
                }
    	
                cout << i;
                output << i;
                c++;
        			
            }while (c >= 50 && i != '\n');
    
            c = c + 1;
    
            if (i == '\n')
            {
                count = count + 1;
                c = 0;
            }
        }
    
        cin.get();
        return 0;
    }
    Now the output is wierd but thats your job to fix not mine
    Last edited by prog-bman; 10-18-2005 at 03:21 AM.
    Woop?

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    11
    lol i used your code right, but it also loops infinite when it hits that Do-while statement

    this is the text file

    This is a sample text file.
    It contains sevral lines of text.
    ...The third line of text starts with some spaces.
    Here is a line that will be too long and need to be broken into multiple lines after 50 characters. A single line may need to be broken into multiple lines.
    One more line.
    those periods in third line are really spaces

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    Well than figure out why it is not breaking the loop then. I think I have help you enough for not hash away and come back .
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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