Solved

Help commenting a block of code

Posted on 2004-08-24
2
204 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now