我遇到了我的程序問題。我已經實際完成了它,但最終的結果並不如預期的那樣工作。我已經寫了兩種方式,一種是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,那麼它會輸出每隔一個字母。最後一部分不起作用。它沒有給出選擇,而是每次都做同樣的事情。我已經調整了其他方式,它會做一個或另一個兩個時間,而不是一個或另一個。我究竟做錯了什麼?
增加警告級別應該會發現此錯誤。 – Jarod42
當你想結束一行時,只需寫入''\ n'。 'newline'沒有意義,'std :: endl'的功能遠遠超出您的需求。 –