?
Solved

Help commenting a block of code

Posted on 2004-08-24
2
Medium Priority
?
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
Suggested Courses

777 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