C++;阵列cin环 我正在努力学习C++,我是新手。 我有一个小数组程序,我的老师让我写。 他需要多个阵列,并提供一个菜单,以选择所需的阵列。 我唯一的问题是我在一些for循环中遇到了问题。 选项一和选项二给我他们的,但我看起来cin不工作或什么的 #include<iostream> #include<string> using namespace std; int main() { int Array1[5]; int Array2[2]; int Array3[4]; int choice1; int choice2; cout << "******************************************" << endl; cout << "* this damn thing has killed me today *" << endl; cout << "******************************************\n\n\n\n" << endl; cout << " Here is some options \n\n" << endl; cout << " 1) start array of 5\n " << endl; cout << " 2) Make another array. Choices are 2/4" << endl; cout << " 3) Exit" << endl; cin >> choice1; if (choice1 == 1) { for (int a = 0; a < 5; ++a) { cout << "Lets start this" << endl; int y = 0; int n = 0; int x; cin >> x; Array1[a] = x; cout << " Wann see the numbers you entered?? Y/N" << endl; int question; cin >> question; if (question == y) { cout << Array1[0]; cout << Array1[1]; cout << Array1[2]; cout << Array1[3]; cout << Array1[4]; cout << "n\n\\n" << endl; cout << "Bye Bye" << endl; } else if (question == n) { cout << "\n\n Bye Bye" << endl; return 0; } } } if (choice1 == 2) { cout << "Which Array Shall We DO??????\n1)2?\n2)4?\n" << endl; cin >> choice2; if (choice2 == 1) for (int c = 0; c < 2; ++c) { int z = 0; Array2[c] = z; cin >> z; cout << "Would you like to see the Array of 2? y/n? " << endl; int q1; int y = 0; int n = 0; cin >> q1; if (q1 == y) { cout << Array2[0]; cout << Array2[1]; cout << "\n\n\nBye Bye" << endl; return 0; } if (q1 = n) { cout << "\n\nBye Bye" << endl; } } if (choice2 = 2) { for (int d = 0; d < 4; ++d) { int y = 0; int n = 0; int w = 0; int q2; Array3[d] = w; cout << "Enter Numbers........\n\n" << endl; cin >> w; cout << "Would You Like to See Array????? y/m\n\n" << endl; cin >> q2; if (q2 == y) { cout << Array3[0]; cout << Array3[1]; cout << Array3[2]; cout << Array3[3]; } if (q2 == n) { cout << "\n\n\nBye Bye! " << endl; return 0; } } } } if (choice1 == 3) { cout << "Bye Bye" << endl; return 0; } } #包括 #包括 使用名称空间std; int main() { 国际阵列1[5]; int Array2[2]; int Array3[4]; 综合选择1; 综合选择2; 首先这里有很多错误。因为你是编程新手,我想帮助你完成这个过程。我不想做你的家庭作业,但我会给你足够的信息让你很快完成

C++;阵列cin环 我正在努力学习C++,我是新手。 我有一个小数组程序,我的老师让我写。 他需要多个阵列,并提供一个菜单,以选择所需的阵列。 我唯一的问题是我在一些for循环中遇到了问题。 选项一和选项二给我他们的,但我看起来cin不工作或什么的 #include<iostream> #include<string> using namespace std; int main() { int Array1[5]; int Array2[2]; int Array3[4]; int choice1; int choice2; cout << "******************************************" << endl; cout << "* this damn thing has killed me today *" << endl; cout << "******************************************\n\n\n\n" << endl; cout << " Here is some options \n\n" << endl; cout << " 1) start array of 5\n " << endl; cout << " 2) Make another array. Choices are 2/4" << endl; cout << " 3) Exit" << endl; cin >> choice1; if (choice1 == 1) { for (int a = 0; a < 5; ++a) { cout << "Lets start this" << endl; int y = 0; int n = 0; int x; cin >> x; Array1[a] = x; cout << " Wann see the numbers you entered?? Y/N" << endl; int question; cin >> question; if (question == y) { cout << Array1[0]; cout << Array1[1]; cout << Array1[2]; cout << Array1[3]; cout << Array1[4]; cout << "n\n\\n" << endl; cout << "Bye Bye" << endl; } else if (question == n) { cout << "\n\n Bye Bye" << endl; return 0; } } } if (choice1 == 2) { cout << "Which Array Shall We DO??????\n1)2?\n2)4?\n" << endl; cin >> choice2; if (choice2 == 1) for (int c = 0; c < 2; ++c) { int z = 0; Array2[c] = z; cin >> z; cout << "Would you like to see the Array of 2? y/n? " << endl; int q1; int y = 0; int n = 0; cin >> q1; if (q1 == y) { cout << Array2[0]; cout << Array2[1]; cout << "\n\n\nBye Bye" << endl; return 0; } if (q1 = n) { cout << "\n\nBye Bye" << endl; } } if (choice2 = 2) { for (int d = 0; d < 4; ++d) { int y = 0; int n = 0; int w = 0; int q2; Array3[d] = w; cout << "Enter Numbers........\n\n" << endl; cin >> w; cout << "Would You Like to See Array????? y/m\n\n" << endl; cin >> q2; if (q2 == y) { cout << Array3[0]; cout << Array3[1]; cout << Array3[2]; cout << Array3[3]; } if (q2 == n) { cout << "\n\n\nBye Bye! " << endl; return 0; } } } } if (choice1 == 3) { cout << "Bye Bye" << endl; return 0; } } #包括 #包括 使用名称空间std; int main() { 国际阵列1[5]; int Array2[2]; int Array3[4]; 综合选择1; 综合选择2; 首先这里有很多错误。因为你是编程新手,我想帮助你完成这个过程。我不想做你的家庭作业,但我会给你足够的信息让你很快完成,c++,arrays,for-loop,C++,Arrays,For Loop,你的老师向你介绍了switch语句了吗?如果是的话,现在是使用它的好时机。无论如何,我们将在这里继续讨论 那么让我们看一下部分中的代码 if (choice1 == 1) { for (int a = 0; a < 5; ++a) { cout << "Lets start this" << endl; int y = 0; int n = 0; int x; cin

你的老师向你介绍了switch语句了吗?如果是的话,现在是使用它的好时机。无论如何,我们将在这里继续讨论

那么让我们看一下部分中的代码

 if (choice1 == 1)
{
    for (int a = 0; a < 5; ++a)
    {
        cout << "Lets start this" << endl;
        int y = 0;
        int n = 0;

        int x;
        cin >> x;
        Array1[a] = x;
用户可能很困惑,你也是。但我知道你想要一个数字,所以我们键入2

下一部分

cout << " Wann see the numbers you entered??  Y/N" << endl;
            int question;
            cin >> question;
            if (question == y)
cout问题;
如果(问题==y)
我在这里看到三个问题。 首先,我会问你字母Y是什么类型的?它是一个字符。不是整数。其次,如果你的用户键入小写字母Y会发生什么?你也应该向用户提问。记住,用户很笨,不知道你在做什么。第三,尝试
if(question='Y')

现在,在我们修复了这个问题之后,让我们重新编译,看看会发生什么?我们不再得到中断,但我们突然陷入了一个循环。我们还打印奇怪的数字。这是为什么?让我们将第8行更改为
int Array1[5]={0,0,0,0,0};
我为什么要这样做?看看你是否注意到打印语句时发生的模式。有一个简单的解决方法,但这就是格式和样式

您遇到的最后一个问题是else语句。为什么要使用else if?为什么不使用else?为什么返回0?如果用户回答“否”,是否希望用户退出该程序?请将整个部分注释掉,看看会发生什么

你的其他问题与上面描述的类似。我强烈建议你做三件事

1) 编写伪代码。我知道这是一个痛苦和烦人的过程,但它不会花费很长时间,并且可以为您节省数小时的调试时间。在这里,它可以解决您的问题

2) 确保您向用户提出的问题是清楚的

3) 你的用户很笨,你的老师在故意测试你的代码时可能会装傻。问一些问题,比如“如果用户用我没有解释的东西回答怎么办?比如把一个数字放在一个字符应该在的地方。这就是if-then-if-then-else语句的作用。确保使用OR语句(| |)考虑资本和小写。如果你想做得很好,就要弄清楚如何通过输入“y”或“Yes”或“Yes”来传递“y”


我希望这一切都有帮助。在我这方面进行了这些修复之后,代码编译并运行良好。如果这还不能解决所有问题,我将给出更多提示。

首先,我修复了缩进,因为它使代码难以阅读。其次,听起来您可能需要学习如何使用调试器逐步完成代码。有了一个好的调试器,您可以执行您的代码逐行编程,看看它偏离了你的预期。如果你要做任何编程,这是一个必不可少的工具。进一步阅读:你应该始终包括提问时遇到的错误。不仅仅是代码。错误帮助我们找出错误。
if(choice2=2)
。将永远是真的。你似乎在我只能假设应该是
=
的第二个
=
中暗示了第二个
=
。如果(q1=n)也一样。将编译器警告设置为最大级别。大多数合理的编译器都会标记这两个警告。我现在没有错误。解决这些问题后,编译器不会拾取for循环。应该输入数组,然后输出数组,但我得到的是一些选项1)启动数组5 2)制作另一个array.选择是2/4 3)退出1让我们开始这个1这是你的数组是什么1-858993460-858993460-858993460-858993460-858993460-858993460\n再见让我们开始这个编译器显示没有错误哦还有一件事。注释注释注释注释。如果你注释了你的代码,你的老师和/或助教会爱死你。将来你也会爱死的。进入我现在习惯了。谢谢。我总是忘记这个和那个。我改变它。我已经把它都修好了。谢谢你。很高兴知道。记住要循序渐进。这会让你的生活变得更轻松。
cout << " Wann see the numbers you entered??  Y/N" << endl;
            int question;
            cin >> question;
            if (question == y)