2015-12-24 54 views
0

我遇到了我的程序问题。我已经实际完成了它,但最终的结果并不如预期的那样工作。我已经写了两种方式,一种是switch语句,另一种是if语句,但都不行。他们都为我编译,并有一个干净的构建,但不会按预期运行。为什么我的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 

最后一部分用,如果别人是这样做:

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

+1

增加警告级别应该会发现此错误。 – Jarod42

+0

当你想结束一行时,只需写入''\ n'。 'newline'没有意义,'std :: endl'的功能远远超出您的需求。 –

回答

5

对于第二部分的变化

if (selection = 0){ 

if (selection == 0){ 

表达selection = 0是赋值,但你需要比较。

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

 if (selection % 2 == 0) 
     { 
      // if even 
     } 
     else 
     { 
      // if odd 
     } 

编辑:

为了避免这样的输入错误在未来训练自己上比较运算符,例如左侧写恒定值

if (0 == selection) 

如果您错误类型的编译器会向您显示有关l值的错误,该桅杆可变。

EDIT 2(约开关和用于):

在下面的代码

switch (selection) 
{ 
    case 0: 
     { 
      for (i = 0; i < size; i += 2) 
      { 
       cout << i << " "; 
       break; 
      } 
     } 
    case 1: 
    ... 
} 

break语句指for,但不switch,并且作为结果:

1)for将只执行一次;

2)而selection0case 1无论如何将在case 0之后工作。

正确的代码必须如下

switch (selection) 
{ 
    case 0: 
     { 
      for (i = 0; i < size; i += 2) 
      { 
       cout << i << " "; 
      } 
      break; 
     } 
    case 1: 
    ... 
} 
+1

Upvoting的提示。 – CinCout

+0

我很惊讶,编译器没有给出'你是不是这个意思?'警告。此外,该提示降低了可读性。 –

+0

加1为提示。 –

0

请不要改变#

if (selection == 0) 

我认为,上述变化可删除的问题。在你的代码中,你不是比较,而是在它中分配0,所以如果得到执行并且将赋值放在if中不是标准的,那么从不希望如此。

希望这会有所帮助。

相关问题