为什么打印出来的价值都是垃圾?我们怎样才能解决这个问题
Why are the printed values all junk? How can we fix this?
我只是想知道。你看,我们为一个叫做魔术数字游戏(3x3)的游戏制作了一个程序。但在我们的程序中,用户将输入所有九个数字,程序将检查输入的数字在水平、垂直和对角相加时是否全部加起来为15。
我们使用简单的cin/cout
方法得到了正确的算法,具有正确的输出,但我们需要使用类。
当我们这样做时,输入是正确的,但当打印数字和总和时,程序会打印垃圾内存,而不会得到用户输入的内容。以下是我们目前掌握的代码。
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <stdlib.h>
using namespace std;
class magicNumber {
public:
void inputNum(int, int, int, int, int, int, int, int, int);
void check();
void displayResults();
void decision();
magicNumber();
private:
int a, b, c, d, e, f, g, h, i;
int row1, row2, row3, col1, col2, col3, dia1, dia2;
};
magicNumber::magicNumber() {
int a, b, c, d, e, f, g, h, i = 0;
int row1, row2, row3, col1, col2, col3, dia1, dia2 = 0;
}
void magicNumber::inputNum(int, int, int, int, int, int, int, int, int) {
cout << "Enter three numbers for the first row (seperate by space): ";
cin >> a >> b >> c;
cout << "Enter three numbers for the next row (seperate by space): ";
cin >> d >> e >> f;
cout << "Enter three numbers for the last row (seperate by space): ";
cin >> g >> h >> i;
cout << endl;
}
void magicNumber::check() {
int check = 0;
int a, b, c, d, e, f, g, h, i;
int row1, row2, row3, col1, col2, col3, dia1, dia2;
row1 = a + b + c;
if (row1 != 15)
(check++);
row2 = d + e + f;
if (row2 != 15)
(check++);
row3 = g + h + i;
if (row3 != 15)
(check++);
col1 = a + d + g;
if (col1 != 15)
(check++);
col2 = b + e + h;
if (col2 != 15)
(check++);
col3 = c + f + i;
if (col3 != 15)
(check++);
dia1 = c + e + g;
if (dia1 != 15)
(check++);
dia2 = a + e + i;
if (dia2 != 15)
(check++);
}
void magicNumber::displayResults() {
int a, b, c, d, e, f, g, h, i;
int row1, row2, row3, col1, col2, col3, dia1, dia2;
cout << " = " << dia1 << "n";
cout << "+++++++++++++++++++++++++n";
cout << "+ + + +n";
cout << "+ " << a << " + " << b << " + " << c << " + = " << row1
<< "n";
cout << "+ + + +n";
cout << "+++++++++++++++++++++++++n";
cout << "+ + + +n";
cout << "+ " << d << " + " << e << " + " << f << " + = " << row2
<< "n";
cout << "+ + + +n";
cout << "+++++++++++++++++++++++++n";
cout << "+ + + +n";
cout << "+ " << g << " + " << h << " + " << i << " + = " << row3
<< "n";
cout << "+ + + +n";
cout << "+++++++++++++++++++++++++n";
cout << " = " << col1 << " = " << col2 << " = " << col3 << " = "
<< dia2 << "n";
cout << endl;
cout << endl;
}
void magicNumber::decision() {
int check = 0;
char ans;
if (check != 0) {
cout << "YOU FAILED. TRY AGAIN? [Y/N]: ";
cin >> ans;
switch (ans) {
case 'Y':
case 'y':
int main();
case 'N':
case 'n':
cout << "nTHANK YOU, PLAY AGAIN!!";
break;
}
}
else if (check == 0)
cout << "YOU WON!! CONGRATULATIONS!nn";
cout << endl;
}
int main() {
system("CLS");
int check;
char ans;
magicNumber numbers;
cout << "Welcome to Magic Square Number game!n"
<< "Please fill out the 3 x 3 grid with numbers n"
<< "1 to 9 without repeating any digits. The sumn"
<< " should be 15 when the numbers are added horizontally, n"
<< "vertically, or diagonally.nn";
numbers.inputNum(0, 0, 0, 0, 0, 0, 0, 0, 0);
numbers.check();
numbers.displayResults();
numbers.decision();
system("PAUSE");
}
您正在每个例程中本地重新声明整数变量。这不仅隐藏了成员变量,而且将它们初始化为之前堆栈中的任何变量。正确的解决方法是删除它们,转而使用成员变量。
在实践中,在类成员变量上加上某种前缀/后缀有助于将它们与局部变量区分开来。一些例子:
class Foo
{
int fVariable;
int variable_;
int variable_m;
int m_variable;
};
有了这些装饰,就更容易辨别变量的范围和所有权:
void Foo::ClassRoutine()
{
int variable;
variable = 0; // local variable
variable_m = 0; // class member variable
}
您声明了这些成员变量:
class magicNumber
{
//...
int a,b,c,d,e,f,g,h,i;
int row1, row2, row3, col1, col2, col3, dia1, dia2;
然后,在check()
:中
void magicNumber::check()
{
int check = 0;
int a,b,c,d,e,f,g,h,i;
在displayResults()
:中
void magicNumber::displayResults()
{
int a,b,c,d,e,f,g,h,i;
您用相同的名称声明局部变量。共享的a
、b
等从不被任何一种方法查看或触摸。
删除局部声明,以便改用成员变量。
void magicNumber::displayResults()
{
cout<<" = "<<dia1<<"n";
// etc.
对于眼前的问题,只需删除与成员变量同名的局部变量。
既然你被告知要使用类,我想我最好对此发表评论。
您已经使用类机制来实现过程性逻辑:执行那个,然后执行那个,依此类推
但是,想想看,如果控制台i/o被GUI取代,那么这个类会有多有用?就这一点而言,它现在有多有用?答案是,这不是很有用,只是一个复杂的问题。
相反,将类的对象视为自动化,具有一些内部状态(私有数据成员)和一些可见按钮(公共成员函数)。按下任何按钮都会使自动化做一些事情,可能会改变其内部状态。在这种特殊情况下,有用的内部状态是9个值,有用的"按钮"可以是通过bool
函数结果报告这些数字是否构成幻方的成员函数。
其他功能可能包括设置数字。
最后,您可能需要考虑std::array
来保存数字,并循环进行检查。
代码中的一些问题:
- 您声明的变量与该类的方法中
magicNumber
类的成员变量的名称相同。我假设您只想使用成员变量,而不想隐藏(当您在内部作用域中声明一个与其中一个外部作用域同名的变量时,后者是隐藏的,而前者在内部范围中可见) - 您正在浏览
decision
方法中的case语句。当您调用main()
时,您总是稍后执行cout << "nTHANK YOU, PLAY AGAIN!!";
- 调用
main
方法是一个非常非常糟糕的主意。提取需要调用到其他方法的功能
修复了代码(不知道它是否有效,check
方法对我来说是不完美的,但编译OK并修复了所描述的问题):
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <stdlib.h>
void gameplay();
using namespace std;
class magicNumber {
public:
void inputNum(int, int, int, int, int, int, int, int, int);
void check();
void displayResults();
void decision();
magicNumber();
private:
int a, b, c, d, e, f, g, h, i;
int row1, row2, row3, col1, col2, col3, dia1, dia2;
};
magicNumber::magicNumber() {
a = b = c = d = e = f = g = h = i = 0;
row1 = row2 = row3 = col1 = col2 = col3 = dia1 = dia2 = 0;
}
void magicNumber::inputNum(int, int, int, int, int, int, int, int, int) {
cout << "Enter three numbers for the first row (seperate by space): ";
cin >> a >> b >> c;
cout << "Enter three numbers for the next row (seperate by space): ";
cin >> d >> e >> f;
cout << "Enter three numbers for the last row (seperate by space): ";
cin >> g >> h >> i;
cout << endl;
}
void magicNumber::check() {
int check = 0;
row1 = a + b + c;
if (row1 != 15)
(check++);
row2 = d + e + f;
if (row2 != 15)
(check++);
row3 = g + h + i;
if (row3 != 15)
(check++);
col1 = a + d + g;
if (col1 != 15)
(check++);
col2 = b + e + h;
if (col2 != 15)
(check++);
col3 = c + f + i;
if (col3 != 15)
(check++);
dia1 = c + e + g;
if (dia1 != 15)
(check++);
dia2 = a + e + i;
if (dia2 != 15)
(check++);
}
void magicNumber::displayResults() {
cout << " = " << dia1 << "n";
cout << "+++++++++++++++++++++++++n";
cout << "+ + + +n";
cout << "+ " << a << " + " << b << " + " << c << " + = " << row1
<< "n";
cout << "+ + + +n";
cout << "+++++++++++++++++++++++++n";
cout << "+ + + +n";
cout << "+ " << d << " + " << e << " + " << f << " + = " << row2
<< "n";
cout << "+ + + +n";
cout << "+++++++++++++++++++++++++n";
cout << "+ + + +n";
cout << "+ " << g << " + " << h << " + " << i << " + = " << row3
<< "n";
cout << "+ + + +n";
cout << "+++++++++++++++++++++++++n";
cout << " = " << col1 << " = " << col2 << " = " << col3 << " = "
<< dia2 << "n";
cout << endl;
cout << endl;
}
void magicNumber::decision() {
int check = 0;
char ans;
if (check != 0) {
cout << "YOU FAILED. TRY AGAIN? [Y/N]: ";
cin >> ans;
switch (ans) {
case 'Y':
case 'y':
gameplay();
break;
case 'N':
case 'n':
cout << "nTHANK YOU, PLAY AGAIN!!";
break;
}
}
else if (check == 0)
cout << "YOU WON!! CONGRATULATIONS!nn";
cout << endl;
}
void gameplay() {
magicNumber numbers;
cout << "Welcome to Magic Square Number game!n"
<< "Please fill out the 3 x 3 grid with numbers n"
<< "1 to 9 without repeating any digits. The sumn"
<< " should be 15 when the numbers are added horizontally, n"
<< "vertically, or diagonally.nn";
numbers.inputNum(0, 0, 0, 0, 0, 0, 0, 0, 0);
numbers.check();
numbers.displayResults();
numbers.decision();
}
int main() {
system("CLS");
gameplay();
system("PAUSE");
}
其他一些建议:
- 如果用户愿意,您需要使用
decision
方法中的循环来重复游戏
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- Ardunio UNO解决了多个重叠的定时器循环
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 我们可以访问一个不存在的联盟的成员吗
- 如果编译的源代码是特定于它编译的硬件的,我们如何分发它
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当使用透明的std函数对象时,我们还需要写空的尖括号吗
- 两个文件使用彼此的功能-如何解决
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何解决我们必须向前和向后迭代的项链断裂问题
- 在这种情况下,我们可以使用静态而不是朋友吗,还有其他解决方案是什么
- 当我们在以下程序(C )中输入字符时,该如何解决
- 为什么我们需要 RAII 来解决异常安全问题
- 我们可以根据当前的解决方案配置更改exe图标吗?
- 我们如何使用动态规划解决子字符串匹配检查
- 为什么打印出来的价值都是垃圾?我们怎样才能解决这个问题
- 我们可以仅通过openLDAP构建SSO解决方案吗
- 多态性歧义,我们能否用"default"基础来解决它们
- 为什么编译器不帮助我们解决类型特征,而是求助于语言怪癖?