Help with loops

This is a discussion on Help with loops within the C++ Programming forums, part of the General Programming Boards category; Hey everyone I'm very new to c++ and need some help. I'm trying to write a program that opens a ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,775
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    what is this line
    inFile.open (fileName);

    doing in the last while loop?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    in the first loop you opened the file - before the loop that was reading its content
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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);
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 12:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03: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

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