Thread: Skip spaces project

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    Unhappy Skip spaces project

    I am doing a C++ program for school which is supposed to take letters from an input file and output them in a file. the input file has words with various number of spaces between them. The program is supposed to output the words with just one space between them, like a normal sentence. I have it all written up, the main codes are

    infile.get(c)

    while (! infile.eof())

    ( if (c != ' ')
    outfile.put(c);

    else
    outfile.put(c);
    infile.get(c);
    skip(infile, outfile, c);


    void skip(ifstream& infile, ofstream& outfile, char& c)
    {
    while (c == ' ')
    {
    infile.get(c)
    }

    every time I run it, it just ends up skipping all the spaces. If anyone can help me out, I'd really appreciate it, thanks!

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    find a way to only skip or add spaces where you know a gramatically correctly used space would occur... [i find that quite a task...] give us any example of the correct output and your input... plus, i would doubt that your instructor would give such an assignment... maybe i have the wrong idea about it's task...
    hasafraggin shizigishin oppashigger...

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    There are a few syntax errors in your program please post the entire program and I will show you where your problems are.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    program

    this is my whole program thus far

    #include <iostream>
    #include <iomanip>
    #include <cmath>
    #include <fstream>
    #include <cstdlib>
    using namespace std;

    void skip(ifstream& infile, ofstream& outfile, char& c);


    int main ()
    {

    ifstream infile;
    ofstream outfile;

    infile.open("start.txt");
    if (infile.fail())
    {
    cout << " Input file opening failed.\n";
    exit (1);
    }

    outfile.open("end.txt");
    if (outfile.fail())
    {
    cout << " Output file opening failed.\n";
    exit (1);
    }

    char c;


    infile.get(c);
    while (! infile.eof())

    {


    if (c != ' ')
    outfile.put(c);
    else
    outfile.put(c);
    infile.get(c);
    skip(infile, outfile, c);

    infile.get(c);


    }



    infile.close();
    outfile.close();

    return 0;

    }

    void skip(ifstream& infile, ofstream& outfile, char& c)

    {

    while (c == ' ')
    {
    infile.get(c);

    }
    infile.putback(c);
    }

    It's supposed to take an input like

    Hi there this is the file ( more than one space between words)

    and output it so it reads

    Hi there this is the file

    but I can only get
    Hitherethisisthefile

    I've been playing around with it tonight and no matter what I change it either shows all the letters but no spaces or shows the spaces but knocks off the first letter of each word. If you have any ideas please let me know, thanks
    Last edited by raptor2xtreme; 11-04-2001 at 11:49 PM.

  5. #5
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    ooh! i see now... run through your file like this...

    if a space is in the current character, check if a space is in the next character, delete if it is, else do not delete... if it is not a space, go to the next character...
    hasafraggin shizigishin oppashigger...

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Looks like its working now. Please note the changes I've made. If your serious about programming send me an email explaining what I've change. You basically had it.
    Code:
    #include <iostream> 
    #include <iomanip> 
    #include <cmath> 
    #include <fstream> 
    #include <cstdlib> 
    using namespace std; 
    
    void skip(ifstream& infile, ofstream& outfile, char& c); 
    
    
    int main () 
    { 
    
    	ifstream infile; 
    	ofstream outfile; 
    
    	infile.open("c:\\start.txt"); 
    
    	if (infile.fail()) 
    	{ 
    		cout << " Input file opening failed.\n"; 
    		exit (1); 
    	} 
    
    	outfile.open("c:\\end.txt"); 
    	if (outfile.fail()) 
    	{ 
    		cout << " Output file opening failed.\n"; 
    		exit (1); 
    	} 
    
    	char c; 
    
    
    	infile.get(c); 
    	while (! infile.eof()) 
    	{ 
    
    
    		if (c != ' ') 
    		{
    			outfile.put(c); 
    		}
    		else
    		{
    			outfile.put(c); 
    			infile.get(c); 
    			skip(infile, outfile, c); 
    		}
    		
    		
    		infile.get(c); 
    
    	} 
    
    	infile.close(); 
    	outfile.close(); 
    
    	return 0; 
    
    } 
    
    void skip(ifstream& infile, ofstream& outfile, char& c) 
    { 
    
    	while (c == ' ' && ! infile.eof( ) ) 
    	{ 
    		infile.get(c); 
    
    	} 
    	
    	if( !infile.eof( ) )
    	{
    		outfile.put(c);
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  2. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 02:42 PM
  3. application project
    By featherflutter in forum C++ Programming
    Replies: 2
    Last Post: 06-26-2004, 11:12 AM
  4. Please, suggest a project ….
    By Dragon227Slayer in forum C# Programming
    Replies: 1
    Last Post: 06-12-2004, 11:16 AM
  5. Please, suggest a project ….
    By Dragon227Slayer in forum Tech Board
    Replies: 1
    Last Post: 06-12-2004, 10:48 AM