Thread: what condition in the while loop?

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

    what condition in the while loop?

    i need the main body of this to execute, i figured a while loop but i have no idea what would go inside, i've never dealt with inputting files before. if i just run it without the while loop everything comes back as 0, obviously. i don't know what possible condition to use to make this run correctly.
    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    
    using namespace std;
    
    
    
    bool myIsAlpha(char ch)
    {
    if((ch > 64 and ch < 91) or (ch > 96 and ch < 123))
    return true;
    else
    return false;
    }
    
    
    bool myIsDigit( char ch )
    {
    if (ch > 47 and ch < 58)
    return true;
    else
    return false;
    }
    
    
    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,
    countAlpha = 0,
    countUpper = 0,
    countLower = 0,
    countChar = 0,
    countDigit = 0,
    countSpace = 0,
    countWhite = 0;
    char ch; 
    
    int main()
    {
    ifstream inputFile;
    
    
    
    inputFile.open( "input.txt" );
    if( inputFile.fail() )
    {
    cout << "input.txt failed to open";
    exit( -1 );
    }
    
    while ()
    {
    if(ispunct(ch))
    {
    countPunct++;
    }
    
    if(myIsAlpha(ch))
    {
    countAlpha++;
    }
    
    if (myIsDigit(ch))
    {
    countDigit++;
    }
    if (myIsUpper(ch))
    {
    countUpper++;
    }
    if (islower(ch))
    {
    countLower++;
    }
    if (isspace(ch))
    {
    countWhite++;
    }
    }
    
    
    //Close the input file
    
    inputFile.close();
    
    cout<<"\n"<<countPunct<<"\n"<<countAlpha<<"\n"<<countDigit<<"\n"<<countUpper;
    return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You might want to describe what you are trying to do, especially what is the purpose of that loop. In fact, while trying to come up with such an explanation, you might just stumble upon the answer yourself

    By the way, it is good that you posted code in code tags, but please indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i think the usual in this case is while(!EOF) or similar i cant remember C, you are reading the file and want to count the varoius character types right?
    sort out the indentation of your code so it is easier for everyone (and YOU) to read

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    ah yes, sorry. i am trying to count the various characters and spaces in this text file that is read by the program. i shall indent... bad habit of mine to leave everything on the left side.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can a "switch" be inside a loop?
    By gmk0351 in forum C Programming
    Replies: 5
    Last Post: 03-28-2008, 05:47 PM
  2. syntax question
    By cyph1e in forum C Programming
    Replies: 19
    Last Post: 03-31-2006, 12:59 AM
  3. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  4. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM