C++ 为什么打印的值都是垃圾?我们如何解决这个问题?
我只是想知道。 你看,我们为一个叫做魔术数字游戏(3x3)的游戏制作了一个程序。但在我们的程序中,用户将输入所有九个数字,并且程序将检查输入的数字在水平、垂直和对角添加时是否全部合计为15 我们使用simpleC++ 为什么打印的值都是垃圾?我们如何解决这个问题?,c++,C++,我只是想知道。 你看,我们为一个叫做魔术数字游戏(3x3)的游戏制作了一个程序。但在我们的程序中,用户将输入所有九个数字,并且程序将检查输入的数字在水平、垂直和对角添加时是否全部合计为15 我们使用simplecin/cout方法得到了正确的算法,具有正确的输出,但我们需要使用类 当我们这样做的时候,输入是正确的,但是当数字和总和被打印出来时,程序打印的是垃圾存储器,而不是用户输入的内容。这是我们目前掌握的代码 #include <iostream> #include <iom
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!\n\n";
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 sum\n"
<< " should be 15 when the numbers are added horizontally, \n"
<< "vertically, or diagonally.\n\n";
numbers.inputNum(0, 0, 0, 0, 0, 0, 0, 0, 0);
numbers.check();
numbers.displayResults();
numbers.decision();
system("PAUSE");
}
#包括
#包括
#包括
#包括
使用名称空间std;
类magicNumber{
公众:
void inputNum(int,int,int,int,int,int,int,int,int,int);
无效检查();
void displayResults();
无效判决();
magicNumber();
私人:
INTA、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,int,int){
cout>a>>b>>c;
cout>d>>e>>f;
cout>g>>h>>i;
cout您在每个例程中都在本地重新声明整数变量。这不仅隐藏了成员变量,而且将它们初始化为它们之前堆栈上的任何变量。正确的解决方法是删除它们,然后使用成员变量
在实践中,在类成员变量上添加某种前缀/后缀有助于将其与局部变量区分开来。例如:
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;
然后,在检查()
在displayResults()
中:
您使用相同的名称声明局部变量。共享的a
、b
等都不会被任何一种方法查看或触及
删除本地声明,以便使用成员变量
void magicNumber::displayResults()
{
cout<<" = "<<dia1<<"\n";
// etc.
void magicNumber::displayResults()
{
cout对于直接的问题,只需删除与成员变量同名的局部变量即可
既然你被告知要使用一个类,我想我最好对此进行评论
您已经使用类机制实现了一个过程逻辑:这样做,然后这样做,依此类推
但是想一想,如果控制台i/o被GUI取代,那么这个类会有多大用处?就这一点而言,它现在有多大用处?答案是,它不是很有用,只是一个复杂问题
相反,将类的对象视为自动化,具有一些内部状态(私有数据成员)和一些可见按钮(公共成员函数)。按下任何按钮都会使自动化执行某些操作,可能会更改其内部状态。在这种特殊情况下,有用的内部状态是9个值和一个有用的“按钮”可以是通过bool
函数结果报告这些数字是否构成幻方的成员函数
其他功能可能包括设置数字
最后,您可能需要考虑<代码> STD::数组< /COD>用于保存数字,并循环进行检查。
< P>代码中的一些问题:
- 您在该类的方法中声明的变量与类
magicNumber
的成员变量同名。我假设您只想使用成员变量,而不想隐藏(当您在内部作用域中声明一个与外部作用域同名的变量时,后者是隐藏的,前者在内部作用域中可见)
- 您在
decision
方法中遇到了case语句。当您调用main()
时,您总是在稍后执行cout a>>b>>c;
cout>d>>e>>f;
cout>g>>h>>i;
你不能声明与成员变量同名的局部变量。不要。谢谢你的回答。我们将尝试应用你告诉我的所有内容来改进我们的程序。谢谢!我们终于修复了错误,谢谢大家。也谢谢你的提示,先生。我们非常感谢你的帮助。
void magicNumber::displayResults()
{
int a,b,c,d,e,f,g,h,i;
void magicNumber::displayResults()
{
cout<<" = "<<dia1<<"\n";
// etc.
#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!\n\n";
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 sum\n"
<< " should be 15 when the numbers are added horizontally, \n"
<< "vertically, or diagonally.\n\n";
numbers.inputNum(0, 0, 0, 0, 0, 0, 0, 0, 0);
numbers.check();
numbers.displayResults();
numbers.decision();
}
int main() {
system("CLS");
gameplay();
system("PAUSE");
}