初学者C++难以理解我所有教师函数中的参数
Beginner C++ having trouble understanding a parameter in all my teachers functions
问题:我的老师一直使用的字符串提示是什么?
我的老师用工具原型制作了这个头文件:
#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是一个宏定义的常量,它执行其名称所暗示的操作。
相关文章:
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 当从函数参数中的临时值调用复制构造函数时
- 如何从"decltype()"获取函数参数的数量<funtion>?
- 如何将lambda作为模板类的成员函数参数
- 模板参数推导失败,函数参数/参数不匹配
- 如何在C++中将迭代器作为函数参数传递
- 将函数参数"const char*"转换为"std::string_view"是
- C++ 如何将数组值解压缩为函数参数
- 主函数参数的属性
- 具有两个间接寻址运算符 (C++) 的函数参数的用途
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何定义在用作函数参数时工作的类模板的转换
- 将函数参数完美转发到函数指针:按值传递呢?
- 为什么我不能将引用作为 std::async 的函数参数传递
- 什么..(省略号)作为函数原型中唯一的函数参数,C++?
- 是否可以就地构造一个固定大小的数组作为函数参数?
- 接受模板作为函数参数
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- Arduino 函数参数