Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help commenting a block of code

Posted on 2004-08-24
2
Medium Priority
?
225 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.
Suggested Courses

618 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