为什么我的 if/else 语句没有产生预期的结果?
我的程序有问题.我实际上已经完成了它,但最后并没有像它应该的那样工作.我写了两种方法,一种是用 switch 语句,另一种是用 if else 语句,但都不能正常工作.它们都为我编译并具有干净的构建,但不会按预期运行.
I'm having an issues with my program. I've practically completed it but the very end does not work as it should. I've written it two ways, one with a switch statement and also with an if else statement but neither works right. They both compile for me and have a clean build but will not run as it is supposed to.
#include <iostream>
#include <cstdio>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int i = 0;
const int size = 27;
char newline = '\n';
char *pnumber = 0;
int selection = 0;
cout << "PRINGING CONTENTS OF ARRAY" << endl;
cout << "====================================================" << endl;
char alphabet[size] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'
, 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'
, 'Z' , 0 };
for (i = 0; i < size; i++)
{
cout << alphabet[i] << " ";
}
cout << newline;
cout << newline;
cout << "This is the title to your Program related to the alphabet."
<< endl;
cout << endl;
cout << "Select the index that coincides with the alphabet." << endl;
cout << "For example, the number 7 should display the letter G" << endl;
cout << endl;
cout << "Enter an index between 1 and 26: ";
cin >> i;
cout << "The number you selected: " << i;
cout << newline;
cout << "The letter at this index: " << alphabet[i - 1];
cout << endl;
cout << newline;
cout << newline;
cout << "PRINTING CONTENTS OF ARRAY and adding x to every other element" << endl;
pnumber = &alphabet[1];
for (i = 0; i < size; i += 1)
{
if (i % 2 == 0)
{
cout << alphabet[i] << " ";
}
else
{
cout << "x ";
}
}
cout << newline;
cout << newline;
cout << "PRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl;
cout << "=========================================================" << endl;
cout << "Do you want the letters of the alphabet starting from index 0, A, or index 1, B: ";
cin >> selection;
switch (selection)
{
case 0:
{
for (i = 0; i < size; i += 2)
{
cout << "Even Numbered Elements=" << i << " ";
cout << "Contents of Element within Array is=" << alphabet[i];
break;
}
}
case 1:
{
for (i = 1; i < size; i += 2)
{
cout << "Odd Numbered Elements=" << i << " ";
cout << "Contents of Element within Array is=" << alphabet[i] << endl;
break;
}
}
default: cout << "You entered an invalid esponse.";
}
cout << endl;
return 0;
} //End of Int Main
if else 的最后一部分是这样的:
The last part done with if else is this:
cout << newline;
cout << newline;
cout << "PRINTING CONTENTS OF ARRAY USING THE MOD Option" << endl;
cout << "=========================================================" << endl;
cout << "Do you want the letters of the alphabet starting from index 0, A, or index 1, B: ";
cin >> selection;
if (selection = 0){
for (i = 0; i < size; i += 2)
{
cout << "Even Numbered Elements=" << i << " ";
cout << "Contents of Element within Array is=" << alphabet[i] << endl;
}
}
else{
for (i = 1; i < size; i += 2)
{
cout << "Odd Numbered Elements=" << i << " ";
cout << "Contents of Element within Array is=" << alphabet[i] << endl;
}
}
return 0;
} //End of Int Main
1)第一部分输出字母,2)接下来它提示输入一个数字并应该给出相关的字母,3)接下来程序应该输出每隔一个字母为x,例如A x C xE x........4)最后它应该提示用户选择o从A或1开始字母并从B开始,然后它会每隔一个字母输出.最后一部分不起作用.它没有提供选项,而是每次都做同样的事情.我已经以其他方式对其进行了调整,并且两次都可以这样做,而不是一种或另一种.我究竟做错了什么?
1)The first part outputs the alphabet, 2)next it prompts for a number and is supposed to give the correlating letter, 3)next the program is supposed to output with every other letter being x such as A x C x E x........4)lastly it is supposed to prompt the user to select o to start the alphabet at A or 1 and start it B, then it would output every other letter. The last part doesn't work. It does not give the option but rather does the same each time. I've tweaked it other ways and it will do one or the other both times rather one or the other. What am I doing wrong?
对于第二部分的更改
if (selection = 0){
到
if (selection == 0){
表达式 selection = 0
是赋值,但需要比较.
Expression selection = 0
is an assignment, but you need comparison.
此外,为了检查均匀度,您可以使用 %
运算符.例如:
Moreover, for checking evenness you can use %
operator. E.g.:
if (selection % 2 == 0)
{
// if even
}
else
{
// if odd
}
为了避免将来出现此类错误输入,请训练自己在比较运算符的左侧写入常量值,例如
To avoid such mistypes in the future train yourself to write constant value on the left side of comparison operator, e.g.
if (0 == selection)
如果你输入错误,编译器会显示关于左值的错误,那必须是可变的.
and if you mistype compiler will show you error about l-value, that mast be variable.
编辑 2(关于 switch 和 for):
在下面的代码中
switch (selection)
{
case 0:
{
for (i = 0; i < size; i += 2)
{
cout << i << " ";
break;
}
}
case 1:
...
}
break
语句指的是 for
,而不是 switch
,因此:
break
statement refers to the for
, but not to switch
, and as a result:
1) for
只会执行一次;
1) for
will be executed only once;
2) 而 selection
是 0
case 1
无论如何都会在 case 0
之后工作.
2) while selection
is 0
case 1
will work after case 0
anyway.
正确的代码必须如下
switch (selection)
{
case 0:
{
for (i = 0; i < size; i += 2)
{
cout << i << " ";
}
break;
}
case 1:
...
}