Solved

Help commenting a block of code

Posted on 2004-08-24
2
215 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

733 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