我的程序有问题.我几乎已经完成了它,但最终结果并没有成功.我已经用两种方式编写了它,一种是带有switch语句,另一种是if else语句但是它们都没有用.它们都为我编译并具有干净的构建但不会按预期运行.
#include#include #include 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完成的最后一部分是:
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 x E x. ....... 4)最后它应该提示用户选择o在A或1处开始字母表并从B开始,然后它将输出每隔一个字母.最后一部分不起作用.它没有给出选项,而是每次都做同样的事情.我已经通过其他方式进行了调整,它会在两个时间内完成其中一个或另一个.我究竟做错了什么?
第二部分改变
if (selection = 0){
至
if (selection == 0){
表达式selection = 0
是一项任务,但您需要进行比较.
此外,为了检查均匀性,您可以使用%
操作员.例如:
if (selection % 2 == 0)
{
// if even
}
else
{
// if odd
}
编辑:
为了避免将来出现这种错误,请自己在比较运算符的左侧写一个常量值,例如
if (0 == selection)
如果你错误的编译器将显示关于l值的错误,那么mast是可变的.
编辑2(关于开关和for):
在以下代码中
switch (selection)
{
case 0:
{
for (i = 0; i < size; i += 2)
{
cout << i << " ";
break;
}
}
case 1:
...
}
break
声明是指for
,但不是switch
,因此:
1)for
将只执行一次;
2),同时selection
为0
case 1
之后将工作case 0
反正.
正确的代码必须如下
switch (selection)
{
case 0:
{
for (i = 0; i < size; i += 2)
{
cout << i << " ";
}
break;
}
case 1:
...
}