使用 类、私有、public、构造函数、函数、整数和字符串的程序进一步学习
Program furthering learning using Classes, private, public, constructor, functions, integers, and strings
在我的 c++ class 中,我们的任务是不断将不同方面构建到此代码中。我目前遇到 2 个错误,并且卡在我不知道自己做错了什么的地方。该程序将私家车或字符串作为名称和一个私有整数输入到游戏中,检查是否可被 3、5 以及 3 和 5 整除。我将在 class 获取输入值并输出它们。我基本上已经弄清楚了这个程序,但它不会编译,我真的不确定为什么。这是我的代码:
#include <iostream>
#include <iomanip>
using namespace std;
using std::istream;
// declare the max size the username input can be
const int MAX = 14;
enum FIZZBUZZ { ABORT = 0, FIZZBUZZ, FIZZ, BUZZ };
class CFizzbuzz // Class definition at global scope
{
// make sure our constructor, destructor, plus member functions are
// all public and available from outside of the class.
public:
CFizzbuzz() {} // Default constructor definition
~CFizzbuzz() {} // Default destructor definition
// function members that are public
// get the user's name and their value from the console and
// store those results into the member variables.
void getFizzbuzz()
{
cout << "Please enter your name: " << endl;
cin >> m_myName;
cout << "Please enter your number for the FizzBuzz game: " << endl;
cin >> m_myNum;
}
// return the user's number type entered
int putFizzBuzz()
{
return m_myNum;
}
char* getName()
{
return m_myName;
}
// logic to check to see if the user's number is 0, fizz, buzz, or fizzbuz
int getRecord(int num)
{
if (num == 0)
{
return ABORT;
}
else if (num % 5 == 0 && num % 3 == 0) // fizzbuzz number
{
return FIZZBUZZ;
}
else if (num % 5 == 0) // buzz number
{
return BUZZ;
}
else if (num % 3 == 0) // fizz number
{
return FIZZ;
}
else
return num;
}
// private data members only available inside the class
private:
int m_myNum;
char m_myName[MAX];
};
int main()
{
CFizzbuzz myClass;
cout << "Welcome to my Fizzbuzz game, you are to guess the location of a "
<< "number which if is divisible by 5 and 3 you will win with "
<< "the output of Fizzbuzz. " << endl;
cout << "Please enter an integer value between 0 and 3 "
<< "representing the row location of the number for the game, "
<< "then press the Enter key: " << endl;
for (;;)
{
myClass.getFizzbuzz();
int num = myClass.putFizzBuzz();
switch (myClass.getRecord(num))
{
case ABORT:
cout << myClass.getName() << "\nThank you for playing\n";
system("PAUSE");
return 0; // exit program
case FIZZ:
cout << "Sorry, " << myClass.getName() << ", number is a Fizz, please try again.\n";
break;
case BUZZ:
cout << "Sorry, " << myClass.getName() << ", number is a Buzz, please try again.\n";
break;
case FIZZBUZZ:
cout << "You win you got FizzBuzz!!!" << endl;
break;
default:
cout << "Sorry, " << myClass.getName() << ", number is a not a Fizz, Buzz, or Fizzbuzz\nPlease try again.\n";
break;
}
}
}
这些是我遇到的错误:
LNK2019, LNK1120
根据您在评论中提到的错误 (Unresolved external symbol _WinMain@16
) 我会说您在 Visual Studio 中创建了一个 Win32 项目(一个 GUI 项目),但您的代码本来是一个控制台申请。
您需要将项目类型从 Win32 应用程序更改为控制台应用程序,方法是 re-recreating 或在项目设置中将子系统从 Windows 更改为控制台。有关后者的更多信息,请参阅以下 link:
我会怀疑 else return num。输入 1 或 2 时会发生什么?显然,模数不会提供嘶嘶声或嗡嗡声,但根据您的枚举值,函数 getRecord() returns 会提供。我会将 NONE 枚举值设置为 -1 以指示它既不是嘶嘶声也不是嗡嗡声。
关于枚举值要记住的是,它在编译时解析为实际数字。因此,当您输入 1,并且模数不能证明是 fizz、buzz 或 fizzbuzz 而您 return 1 时,switch case 语句将解析为 fizzbuzz,即使情况并非如此(双关语意)。
就您评论说它没有按预期工作而言,请输入更多详细信息。
在我的 c++ class 中,我们的任务是不断将不同方面构建到此代码中。我目前遇到 2 个错误,并且卡在我不知道自己做错了什么的地方。该程序将私家车或字符串作为名称和一个私有整数输入到游戏中,检查是否可被 3、5 以及 3 和 5 整除。我将在 class 获取输入值并输出它们。我基本上已经弄清楚了这个程序,但它不会编译,我真的不确定为什么。这是我的代码:
#include <iostream>
#include <iomanip>
using namespace std;
using std::istream;
// declare the max size the username input can be
const int MAX = 14;
enum FIZZBUZZ { ABORT = 0, FIZZBUZZ, FIZZ, BUZZ };
class CFizzbuzz // Class definition at global scope
{
// make sure our constructor, destructor, plus member functions are
// all public and available from outside of the class.
public:
CFizzbuzz() {} // Default constructor definition
~CFizzbuzz() {} // Default destructor definition
// function members that are public
// get the user's name and their value from the console and
// store those results into the member variables.
void getFizzbuzz()
{
cout << "Please enter your name: " << endl;
cin >> m_myName;
cout << "Please enter your number for the FizzBuzz game: " << endl;
cin >> m_myNum;
}
// return the user's number type entered
int putFizzBuzz()
{
return m_myNum;
}
char* getName()
{
return m_myName;
}
// logic to check to see if the user's number is 0, fizz, buzz, or fizzbuz
int getRecord(int num)
{
if (num == 0)
{
return ABORT;
}
else if (num % 5 == 0 && num % 3 == 0) // fizzbuzz number
{
return FIZZBUZZ;
}
else if (num % 5 == 0) // buzz number
{
return BUZZ;
}
else if (num % 3 == 0) // fizz number
{
return FIZZ;
}
else
return num;
}
// private data members only available inside the class
private:
int m_myNum;
char m_myName[MAX];
};
int main()
{
CFizzbuzz myClass;
cout << "Welcome to my Fizzbuzz game, you are to guess the location of a "
<< "number which if is divisible by 5 and 3 you will win with "
<< "the output of Fizzbuzz. " << endl;
cout << "Please enter an integer value between 0 and 3 "
<< "representing the row location of the number for the game, "
<< "then press the Enter key: " << endl;
for (;;)
{
myClass.getFizzbuzz();
int num = myClass.putFizzBuzz();
switch (myClass.getRecord(num))
{
case ABORT:
cout << myClass.getName() << "\nThank you for playing\n";
system("PAUSE");
return 0; // exit program
case FIZZ:
cout << "Sorry, " << myClass.getName() << ", number is a Fizz, please try again.\n";
break;
case BUZZ:
cout << "Sorry, " << myClass.getName() << ", number is a Buzz, please try again.\n";
break;
case FIZZBUZZ:
cout << "You win you got FizzBuzz!!!" << endl;
break;
default:
cout << "Sorry, " << myClass.getName() << ", number is a not a Fizz, Buzz, or Fizzbuzz\nPlease try again.\n";
break;
}
}
}
这些是我遇到的错误:
LNK2019, LNK1120
根据您在评论中提到的错误 (Unresolved external symbol _WinMain@16
) 我会说您在 Visual Studio 中创建了一个 Win32 项目(一个 GUI 项目),但您的代码本来是一个控制台申请。
您需要将项目类型从 Win32 应用程序更改为控制台应用程序,方法是 re-recreating 或在项目设置中将子系统从 Windows 更改为控制台。有关后者的更多信息,请参阅以下 link:
我会怀疑 else return num。输入 1 或 2 时会发生什么?显然,模数不会提供嘶嘶声或嗡嗡声,但根据您的枚举值,函数 getRecord() returns 会提供。我会将 NONE 枚举值设置为 -1 以指示它既不是嘶嘶声也不是嗡嗡声。
关于枚举值要记住的是,它在编译时解析为实际数字。因此,当您输入 1,并且模数不能证明是 fizz、buzz 或 fizzbuzz 而您 return 1 时,switch case 语句将解析为 fizzbuzz,即使情况并非如此(双关语意)。
就您评论说它没有按预期工作而言,请输入更多详细信息。