Thread: Help with loops

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    10

    Help with loops

    Hey everyone I'm very new to c++ and need some help. I'm trying to write a program that opens a text file, prints it, then prints it again with every 5th word replaced by blanks. I can get the file to open and the header for the 2nd print, but I can't get the file even print out again, or my word count to work. Any help is welcome. here's the code:

    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cstring>
    #include <cctype>
    using namespace std;
    
    int main ()
    {
    	char ch, car;
    	char fileName [20];
    	bool lastWasLetter = false;				
    	bool thisLetter = false;
    	char BLANKS [13] = " __________ ";
    	int  wordCount = 0;
    	ifstream inFile;
    	
    	cout << "Give file name:" << endl;
    	cin >> fileName; 
    
    	inFile.open (fileName);
    	inFile.get (ch);
    
    
    	while (!inFile)
    	{
    		inFile.clear ();
    		cout << "File name invalid. Please re-enter the file name:" << endl;
    		cin >> fileName;
    
    		inFile.open (fileName);
    		inFile.get (ch);
    	}
    
    	cout << "=========================================" << endl;
    	cout << "            ORIGINAL TEXT                " << endl;
    	cout << "=========================================" << endl;
    	
    	cout << ch;
    
    	while (inFile) 
    	{
    		inFile.get (ch);
    		cout << ch;
    	}
    	
    	
    	 inFile.seekg(0L, ios::beg);
    
    	
    
    	 cout << "========================================" << endl;
    	 cout << "              CLOZE TEST                " << endl;
    	 cout << "========================================" << endl;
    		
    		
    	 while (inFile)
    	 {
    
    		inFile.open (fileName);
    		cin.get (ch);
    			 
    		if (isalpha (ch))
    		{
    			lastWasLetter = true;
    			cout << ch;
    			if (lastWasLetter = true)
    			{
    				cin.get (car);
    				if (isalpha (car))
    				{
    					thisLetter = true;
    						cout << car;
    				}
    				else (!isalpha (car));
    				{
    					wordCount++;
    				}
    			}
    		}
    		if (wordCount % 5)
    		{
    			cout << BLANKS;
    		}
    	 }
    
    		
    
    	 	
    	 return (0);
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    else (!isalpha (car));
    Bug: No ; after else should be present.
    And if you need else with a condition, you must use else if, not else.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what is this line
    inFile.open (fileName);

    doing in the last while loop?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    I was trying to reprint the file there, I can't figure out how to do it so I just took what made it print the first time and copied and pasted.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in the first loop you opened the file - before the loop that was reading its content
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    yea that was stupid of me. thanks. but how do I get it to read and print the content again?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by fsufan22 View Post
    but how do I get it to read and print the content again?
    you started right, but forget about eof state to be cleared
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    int main ()
    {
    	std::string filename("test.c");
    	std::cout << "Once: ";
    
    	std::ifstream inFile(filename.c_str());
    
    	char ch;
    	while (inFile.get (ch))
    	{
    		std::cout << ch;
    	}
    
    	std::cout << std::endl;
    
    
    	std::cout << "Twice: ";
    	//need to clear eof flag
    	inFile.clear();
    
    	//now after rewind can read file again
    	inFile.seekg(0L, std::ios::beg);
    
    	while (inFile.get (ch))
    	{
    		std::cout << ch;
    	}
    
    	std::cout << std::endl;
    
    	return (0);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    10
    i got it to reprint it. thanks a lot for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM