试着只在这段代码中接收奇数? [英] Trying to only receive odd numbers in this bit of code?
本文介绍了试着只在这段代码中接收奇数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
它在奇数的第一个实例中起作用,但是一旦用户输入另一个,它就输出Magic Box#>(它们的数字)<。我这段代码只接收奇数。请帮帮我。
It works in the first instance of an odd number, but as soon as the user inputs another one it outputs Magic Box# >(their number)<. I this bit of code only to receive odd numbers. Please help me.
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
ofstream outfile;
ifstream infile;
int main()
{
outfile.open("output.txt");
infile.open("numbers.txt");
int n, row, column; //intermediate variable and row and column variables
cout << "Welcome to the Magic Square Program!" << endl << endl;
infile >> n;
if (n % 2 == 0)
{
cout << "Odd numbers only, Please enter an odd number:" << endl;
cin >> n;
goto Odd;
}
else
{
Odd:
cout << "Magic Box#: " << " > " << n << " <" << endl << endl;
}
int MagicSquare[3][3];
MagicSquare[0][2] = 1;
for (int x = 0; x < n; x++)
{
for (int y = 0; y < n; y++){
// cout << setw(3) << x * y << MagicSquare[x][y];
}
cout << endl;
}
system("pause");
return 0;
}
推荐答案
1。从来没有在C ++中使用goto
2.用以下代码替换输入代码:
1. never ever use goto in C++
2. Replace your input code with this:
int n;
do {
cout >> "Plase enter an odd number: " << endl;
cin >> n;
} while (n%2 != 0);
3.一旦按预期工作,用一些额外的检查(如负数或非数字输入)充实该代码
3. once it works as intended, flesh out that code with some extra checks, like negative numbers, or non-numeric input
这篇关于试着只在这段代码中接收奇数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文