Thread: Plz help me on my assignment

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    35

    Plz help me on my assignment

    here is my coding so far:

    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>

    main()
    {
    ifstream infile;
    ofstream outfile;
    char ch;
    int counter=0;



    infile.open("before.txt", ios:ut);
    while(infile.get(ch))
    {
    cout << ch;

    if(ch == ' ')
    {
    counter ++;
    }
    }


    cout <<endl<<counter<<endl;
    return 0;
    }

    This counts the spaces in a text file, what I need to do is count the spaces just b4 the lines.

    this is the txt file:

    // A short example file

    float singleTax (float income)
    {
    if (income <= 20350.0)
    return (income 8 0.15);
    else
    if (income <= 49300.0)
    return (3052.50 + (0.28 * (income - 20350.0)));
    else
    return (11158.50 + (0.31 * (income - 4900.0)));
    }

    please help me if you can

    Thanks in advance
    Last edited by Agent89; 04-05-2002 at 12:34 PM.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    if(ch == ' ')
    {
    counter ++;
    }
    }


    change this to:

    if(ch == EOF)
    {
    counter ++;
    }
    }

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    35
    that didn't work right, I need it to count the number of spaces b4 the line starts, for every line

  4. #4
    Unregistered
    Guest
    Code:
    	ifstream infile( "test.txt", ios::in );
    	char ch;
    	int counter = 0;
    	while(!infile.eof())
    	{
    		infile.get( ch );
    		if( ch == ' ' )  //line begins with space
    		{
    			do
    			{
    				infile.get( ch );
    				counter++;
    			}while( ch == ' ' );
    			infile.ignore( 80, '\n' );
    		}
    		else
    		{
    			infile.ignore( 80, '\n' );
    		}
    	}
    	cout << "Number if spaces = " << counter << endl;

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    I'm going to explode if I don't see some friggin' code tags

  6. #6
    Unregistered
    Guest
    I used them, look above!!! Aint I clever

    I agree people should use them more often - its so helpful to see properly indented code

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    There's no such function as main(). There is a required function int main().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can any1 plz make this assignment
    By jean in forum C Programming
    Replies: 17
    Last Post: 05-13-2009, 09:19 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  4. Need help with homework assignment plz..
    By RVDFan85 in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2006, 10:33 PM