为什么我的if / else语句不能产生预期的结果?

时间:2015-12-24 05:56:50

标签: c++ arrays pointers

我的程序出现问题。我几乎已经完成了它,但最终结果并没有成功。我已经用两种方式编写了它,一种是带有switch语句,另一种是if else语句,但都没有正常工作。它们都为我编译并具有干净的构建但不会按预期运行。

#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完成的最后一部分是:

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开始,然后它将输出每隔一个字母。最后一部分不起作用。它没有给出选项,而是每次都做同样的事情。我已经通过其他方式进行了调整,而且两次都会做一个或另一个。我究竟做错了什么?

2 个答案:

答案 0 :(得分:5)

第二部分改变

if (selection = 0){

if (selection == 0){

表达式selection = 0是一项作业,但您需要进行比较。

此外,为了检查均匀度,您可以使用%运算符。 E.g:

      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:
     ...
}

答案 1 :(得分:0)

请进行更改#

if (selection == 0)

我认为上述变化可以解决问题。在您的代码中,您不是在比较,而是在其中分配0,因此如果执行并且如果不是标准,则永远不会希望进行分配。

希望这会有所帮助。