hey everyone,

I am including my code and was wondering if there is a prefered way to document?

I have comments which describe what does what, but I'm wondering if there is a neater and/or more efficient way to do it...

/*****************************************************
* COSC 230 - Structured Programming
* Chapter 5:  cntChar.sln
* Mar. 2, 2007
*
* Discription:
*    Prompt the user to specify a character to be counted
*    Prompt the user for the sentence which the character should be counted
*    Output the number of times the character was found
*    Ask if the user wants to count a character in another sentence
******************************************************/

//header files
#include <iostream>

using namespace std;

//main function
int main()
{
    //declare chars
    char ch ;
    char y ;
    char z ;

    //declare ints
    int count ;

    //declare bools
    bool cont = true ;
    bool valinput ;

    //while the user wants to continue (cont==true), continue loop
    while ( cont == true )
    {
        //set count = 0 to reset character counter
        count = 0 ;

        //promt user for character to be counted and sentence
        cout << "Enter a character to count:  " << flush ;
        //assign character to 'ch'
        cin >> ch ;
        cout << endl << "Enter a sentence terminated by a period (.) or question mark (?):  " << endl ;

        //do...while 'y' != delemiter '.' or '?'
        do
        {
        //assign each letter to 'y'
        cin >> y ;
        //if 'y' == 'letter', increment 'count'
        if ( y == letter )
        {
            count++ ;
        }
        }
        while ( ( y != '.' ) && ( y != '?' ) ) ;

        cout << "The character " << letter << " appeared " << count << " times in your sentence." << endl ;

        //do... while a valid input has not been provided when asked
        //if the user wants to count another character in another sentence
        do
        {
        cout << endl << "Want to do another? (y/n):  " << flush ;
        cin >> z ;
            if ( z == 'y' )
            {
                cont = true ;
                valinput = true ;
            }
            else if ( z == 'n' )
            {
                cont = false ;
                valinput = true ;
            }
            else
            {
                cout << "!! INVALID INPUT !!" << endl;
                valinput = false ;
            }
        }
        while ( valinput == false ) ;
    }
    return 0 ;
}

Edited 3 Years Ago by Dani: Formatting fixed

interesting thread... i agree with Dave and yourself a lot.

For now though, since I've only been in C++ for almost 2 months... a good amount of commenting will help me to go back and look over previous codes.

thanks

This question has already been answered. Start a new discussion instead.