while loop help

This is a discussion on while loop help within the C++ Programming forums, part of the General Programming Boards category; the purpose of this program is to read a text file and count the total characters, the digits, puncuation, white ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    while loop help

    the purpose of this program is to read a text file and count the total characters, the digits, puncuation, white spaces, etc, and then display the final count. right now i am trying to figure out what to put inside the while loop. it's not executing the current way i have it, and i've tried several other methods. i figure it has to be something along the lines of total number of characters (totalChar) is equal to all of the other characters added up. does anyone have any advice on what the condition inside the loop should be? (also i'm aware the displaying part at the end is not good, i'll fix that when i fix the loop)

    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    
    using namespace std;
    
    /***************************************************************
    Function: myIsAlpha
    
    Use:      Tests if a character is alphabetic
    
    Arguments: It takes a single character as its argument
    
    Returns:   Returns true if the character is alphabetic and false
    		   if the character is not. 
    ***************************************************************/
    
    bool myIsAlpha(char ch)
    {
    if((ch > 64 and ch < 91) or (ch > 96 and ch < 123))
    return true;
    else
    return false;
    }
    /***************************************************************
    Function: myIsDigit
    
    Use:      Tests if a character is a digit
    
    Arguments: It takes a single character as its argument
    
    Returns:   Returns true if the character is a digit and false
    		   if the character is not. 
    ***************************************************************/
    
    
    bool myIsDigit( char ch )
    {
    if (ch > 47 and ch < 58)
    return true;
    else
    return false;
    }
    
    /***************************************************************
    Function: myIsUpper
    
    Use:      Tests if a character is uppercase alphabetic character
    
    Arguments: It takes a single character as its argument
    
    Returns:   Returns true if the character is an uppercase alphabetic 
               character and false if the character is not. 
    ***************************************************************/
    
    bool myIsUpper( char ch )
    {
    if (ch < 64 and ch > 91)
    return true;
    else
    return false;
    }
    
    bool myIsAlpha( char ch );
    bool myIsDigit( char ch );
    bool myIsUpper( char ch );
    int countPunct = 0, //declaring int variables to be used.
    countAlpha = 0,
    countUp = 0,
    countLow = 0,
    countChar = 0,
    countDig = 0,
    countSpace = 0,
    totalChar = countPunct+countAlpha+countUp+countLow+countChar+countDig+countSpace,
    x=0;
    
    char ch; //declaring ch as char
    
    
    int main()
    {
    ifstream inputFile; 
    
    //opening the input file and if it fails to open, displaying and error message to the user and exiting the program
    
    
    
    inputFile.open( "input.txt" );
    if( inputFile.fail() )
    {
    cout << "input.txt failed to open";
    exit( -1 );
    }
    
    while (totalChar==countPunct+countAlpha+countUp+countLow+countChar+countDig+countSpace)
    	{
    	
    	
    	if(ispunct(ch)) //setting if statement so that if the ch character is a punctuation character, it will increment countPunct by 1.
    		{
    		countPunct++;
    		}
    
    	if(myIsAlpha(ch)) //setting if statement so that if the ch character is an alphabetic character, it will increment countAlpha by 1.
    		{
    		countAlpha++;
    		}
    
    	if (myIsDigit(ch)) //setting if statement so that if the ch character is a digit character, it will increment countDig by 1.
    		{
    		countDig++;
    		}
    		
    	if (myIsUpper(ch)) //setting if statement so that if the ch character is an uppercase alphabetic character, it will increment countUp by 1.
    		{
    		countUp++;
    		}
    		
    	if (islower(ch)) //setting if statement so that if the ch character is a lowercase alphabetic character, it will increment countLow by 1.
    		{
    		countLow++;
    		}
    		
    	if (isspace(ch)) //setting if statement so that if the ch character is a white space character, it will increment countSpace by 1.
    		{
    		countSpace++;
    		}
    		
    		totalChar = countPunct+countAlpha+countUp+countLow+countChar+countDig+countSpace;	      	   
    	}
    
    cout<<"\n"<<countPunct<<"\n"<<countAlpha<<"\n"<<countDig<<"\n"<<countUp; //displaying results of the various character counts
    //Closing the input file
    
    inputFile.close();
    
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    242
    won't this work?
    Code:
    while (!inputFile.eof())
    {
       inputFile.get(ch);
       ...
    }

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    when i try that i get an error saying inputfile was not declared. what do i declare it as?

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    whoops nevermind i didn't type it just right. it does work! thank you so much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 05:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 10:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2009, 12:54 AM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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