初学者C++难以理解我所有教师函数中的参数

Beginner C++ having trouble understanding a parameter in all my teachers functions

本文关键字:函数 参数 C++ 初学者      更新时间:2023-10-16

问题:我的老师一直使用的字符串提示是什么?

我的老师用工具原型制作了这个头文件:

#include<string>
//using namespace std;
#ifndef TOOLS_LOCK
#define TOOLS_LOCK
namespace tools_namespace
{
extern const int SCREEN_WIDTH;
std::string swab( char value, int howMany );
void    pause( std::string prompt );
void    flush(void);
int     getInt          ( std::string prompt );
float   getFloat        ( std::string prompt );
char    getChar         ( std::string prompt );
std::string getString   ( std::string prompt );
std::string getLine     ( std::string prompt );
bool    getBool         ( std::string prompt );
int     getBoundedInt   ( std::string prompt,
                            int lowerBound,
                            int upperBound );
int getPositiveInt      ( std::string prompt );
int getNonNegativeInt   ( std::string prompt );
void handleInputError   ( std::string message );
int width       ( int number );
int magnitude   ( int value );
int minimum( int a, int b );
int maximum( int a, int b );
bool isOdd( int value );
}
#endif 

现在这里是工具.cpp实际工具与数学。我稍后要进行测试,我们必须使用他在第 123 行找到的 getNonNegativeInt 函数。让我失望的是这个代码中无处不在的"字符串提示"。

#include<string>
#include<iostream>
#include"Tools.h"
#include"CompileSwitches.h"
using namespace std;
namespace tools_namespace
{
const int SCREEN_WIDTH = 80;
string swab( char swabValue, int howMany )
{
    string swabString;
    for ( int charsNeeded = howMany
            ; charsNeeded > 0
            ; --charsNeeded )
        swabString = swabString + swabValue;
    return swabString;
}
void pause( string prompt )
{
    cout << prompt;
    cin.ignore(999,'n');
}
// clear input garbage
void flush( void )
{
    cin.ignore(999,'n');
}
int getInt( string prompt )
{
    int userInput;
    while (true)
    {
        cout << prompt;
        cin >> userInput;
        flush();
        if ( !cin.fail() ) break;
        handleInputError("Non-Numeric input.");
    }
    return userInput;
}
float getFloat( string prompt )
{
    float userInput;
    while (true)
    {
        cout << prompt;
        cin >> userInput;
        flush();
        if ( !cin.fail() ) break;
        handleInputError("Non-Numeric input.");
    }
    return userInput;
}
int magnitude ( int value )
{
    return (value>=0) ? value : -value;
}
int width( int number )
{
    int digitCount = 0;
    if ( number < 0 )
    {
        ++digitCount; // for the sign
        number = -number;
    }
    for ( ; number >= 10 ; number /= 10 )
        ++digitCount;
    ++digitCount; // for the last digit
    return digitCount;
}
int minimum( int a, int b )
{
    return ( a < b ) ? a : b;
    //if ( a < b )
    //  return a;
    //else
    //  return b;
}
int maximum( int a, int b )
{
    return ( a > b ) ? a : b;
    //if ( a < b )
    //  return a;
    //else
    //  return b;
}
int getBoundedInt( string prompt,
                    int lowerBound, int higherBound )
{
    int userInput;
    while (true)
    {
        userInput = getInt( prompt );
        if ( userInput >= lowerBound
                && userInput <= higherBound )
            break;
        cout << "Value must be in the range "
                << lowerBound
                << "..."
                << higherBound
                << ". Try again."
                << endl;
    }
    return userInput;
}
int getPositiveInt( string prompt )
{
    return getBoundedInt( prompt, 1, INT_MAX );
}
int getNonNegativeInt( string prompt )
{
    return getBoundedInt( prompt, 0, INT_MAX );
}
char getChar( string prompt )
{
    char userInput;
    while (true)
    {
        cout << prompt;
        cin >> userInput;
        flush();
        if ( !cin.fail() ) break;
        handleInputError("Input failed.");
    }
    return userInput;
}
string getString( string prompt )
{
    string userInput;
    while (true)
    {
        cout << prompt;
        cin >> userInput;
        flush();
        if ( !cin.fail() ) break;
        handleInputError("Input failed.");
    }
    return userInput;
}
void handleInputError( string message )
{
    cin.clear();
    flush();
    cout << message << " Try again." << endl;
}
string getLine( string prompt )
{
    string userInput;
    while (true)
    {
        cout << prompt;
        getline(cin,userInput);
        if ( !cin.fail() ) break;
        cin.clear();
        flush();
        cout << "Input failed - Try again." << endl;
    }
    return userInput;
}
bool getBool( string prompt )
{
    while (true)
    {
        char userInput = getChar(prompt);
        if ( userInput == 'y' || userInput == 'Y' )
            return true;
        if ( userInput == 'n' || userInput == 'N' )
            return false;
        cout << "Please enter y or n." << endl;
    }
}
bool isOdd( int aNumber )
{
#ifndef GROSS
    return magnitude(aNumber)%2 == 1;
#else
    if ( aNumber < 0 )
        aNumber = -aNumber;
    int remainder = aNumber%2;
    if ( remainder == 1 || remainder == -1)
        return true;
    else if ( remainder != 1 )
        return false;
#endif
}
}

如果问题是什么是字符串提示,它只是一个字符串变量,其中字符串变量的名称是"prompt",在调用函数时传递给函数。 例如:

std::string thisIsAString = "Hello World";
void PrintPromptText(std::string prompt){
   std::cout << prompt;
}
int main(void){
PrintPromptText(thisIsAString);
return 0;
}

此代码的输出将是"Hello World">

您的老师只是使用提示作为字符串变量的变量名称。

提示(我根据名称猜测(是在输入数据之前打印的文本。

例如:

int my_variable = getInt("Enter an integer: ");

参数string prompt由函数使用,这些函数在行cout << prompt中要求程序用户提供输入。

该函数getNonNegativeInt(string prompt)如上所述使用字符串,然后仅接受用户输入,如果它在 0-INT_MAX(即正数(范围内,则通过使用正确的参数调用getBoundedInt(string prompt, int lowerBound, int upperBound)。INT_MAX是一个宏定义的常量,它执行其名称所暗示的操作。