Solved

Help commenting a block of code

Posted on 2004-08-24
2
211 Views
Last Modified: 2010-04-01
Hello experts,

I have the follwoing code,


#ifndef __INPUT_H
#define __INPUT_H

#include <string.h>
#include <iostream>
using namespace std;




// This class handles user input from keyboard
class Input
{
public:
      void getString(char *string, unsigned int length);
      char getCharacter();
      int getNumber();
      void WaitForEnter();


private:
      char tmpString[255];
};

extern Input gInput;



// gets string from keyboard queue and puts it into array that "*string" points to
// maximum allowed length of input is "length"
void Input::getString(char *string, unsigned int length)
{
      int len;

      if (length > sizeof(tmpString))
            length = sizeof(tmpString); // requested string too long - make it shorter

      do
      {
            cin.getline(tmpString, sizeof(tmpString));
      }while (strlen(tmpString) == 0);

      strupr(tmpString);

      strncpy(string, tmpString, length-1);

      len = strlen(tmpString);
      if (len < length)
            string[len] = 0;
}


// gets string from keyboard queue converts it into integer
// and returns it as a result
int Input::getNumber()
{
      cin.getline(tmpString, sizeof(tmpString));
      return ::atoi(tmpString);
}


// returns a character as a result of input from keyboard
// (user must press enter after character)
char Input::getCharacter()
{
      cin.getline(tmpString, 2);
      return ::toupper(tmpString[0]);
}


// waits for user to press enter
void Input::WaitForEnter()
{
      getCharacter();
}

#endif __INPUT_H


please explain in details exactly what each line of code does.

thank you
0
Comment
Question by:claracruz
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 11884776
// gets string from keyboard queue and puts it into array that "*string" points to
// maximum allowed length of input is "length"
void Input::getString(char *string, unsigned int length)
{
    int len;

    if (length > sizeof(tmpString))  // check whether the buffer passed in is longer than
                                                     // the temporary buffer space
         length = sizeof(tmpString); // requested string too long - make it shorter

    do // read data from 'cin' as long as an empty line is encountered -- probably incorrect :o)
    {
         cin.getline(tmpString, sizeof(tmpString));
    }while (strlen(tmpString) == 0);

    strupr(tmpString); // make that uppercase

    strncpy(string, tmpString, length-1); // copy that to the buffer that was supplied when calling this fuction

    len = strlen(tmpString);
    // terminate the string correctly
    if (len < length)
         string[len] = 0;
}


// gets string from keyboard queue converts it into integer
// and returns it as a result
int Input::getNumber()
{
    cin.getline(tmpString, sizeof(tmpString)); // read input from 'cin'
    return ::atoi(tmpString); // and return that as a number without checking for errors
}


// returns a character as a result of input from keyboard
// (user must press enter after character)
char Input::getCharacter()
{
    cin.getline(tmpString, 2); // read 2 bytes of input from 'cin'
    return ::toupper(tmpString[0]); // return that as uppercase
}


// waits for user to press enter
void Input::WaitForEnter()
{
    getCharacter(); // get a character, regardless what.
}
0
 
LVL 2

Expert Comment

by:pingu7931
ID: 11887409
Hmmm - This looks like a homework question.

I'd add the obvious for a programmer:

// Tell the preprocessor to only process this code once - even if it is included by many files
#ifndef __INPUT_H
#define __INPUT_H

// Use the standard C++ libraries for string storage and manipulation and I/O streaming
#include <string.h>
#include <iostream>
// Ensure that only the standard C++ classes are used
using namespace std;

...

// End the preprocessor conditional directive for this code section/file
#endif __INPUT_H

...

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question