本文介绍了帮助完成任务C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很担心,因为我需要在星期一之前完成这件事。



这是任务:

用函数main()创建一个程序,菜单包含以下函数:

)将几个数字从键盘插入一维数组(数字应小于20);

B)将第一个数组覆盖到第二个数组中,其中元素反向排列c。)将第二个数组覆盖到第三个数组中,其中元素按降序排列。

D)显示有关数组的信息。 (我猜这是A,B和C)



更新:*我读过解决方案1和2并更正了我的程序但是当我输入数字时我会得到一些随机的数组的数字...另一个问题是,在我写完数字后,我也不能回到2,3或4 ......我该怎么办? *



以下是代码:

I''m really worried because I need this done until Monday.

This is the task:
Create a program with function main() and menu with functions for:
A ) Inserting several numbers from the keyboard into one-dimensional array (the numbers should be less than 20);
B ) Overwriting of the 1st array into second array in which the elements are put in reverse order.
C ) Overwriting of the second array into third array in which the elements are put in descending order.
D)Displaying information about the array. (I guess this is A,B and C)

UPDATE: *I read Solution 1 and 2 and corrected my program but when I type the numbers I get some random number for an array...The other problem is that after I''ve written the numbers I also can''t get back to 2,3, or 4...What should I do? *

Here is the code:

#include <iostream>
using namespace std;
int main ()
{
char choice;
cout << "1. Insert 5 numbers" << endl;
cout << "2. Reverse order of the numbers" << endl;
cout << "3. Ascending order"<< endl;
cout << "4. Display 1, 2 i 3"<<endl;
cout << "\n Your choice is:" <<endl;
	cin >> choice;
	switch (choice)
		{
	case '1':
	int array[5];
	cout << "Write the numbers here:" << endl;
        int i;
        for (i=0; i<5;i++)
        cin >> array[i];
        cout << "Array : " << array[i] << endl; break;

    case '2':
	    int reverseorder[5];
	    for (int i=4; i>=0 ;i--)
    	cout << reverseorder [i]; break;

	case '3':
	    int ascendingorder[5];
	    for (i=0; i<5; i++);
	    cout << ascendingorder [i];	break;

	case '4':
	     cout << "Array"<< array[i] << endl;
         cout << "Reverse order" << reverseorder[i] << endl;
         cout << "Ascending order" << ascendingorder[i] << endl;break;

		default: cout << "Wrong data" << endl ; break;

		};

	system ("pause");
		}

推荐答案

if (choice < 5 && choice > 1)

并运行您的代码。当你输入你的价值时,它应该停止,你可以看看发生了什么。



如果你输入1,那么选择将获得值1:那么if的作用是什么?是不到5?是。精细。它大于1吗?不 - 它等于1.所以如果失败,你的程序结束......

尝试用以下代码替换行:

And run your code. When you enter your value, it should stop, and you can look at what is happening.

If you enter "1", then choice will get the value 1: so what shoudl the "if" do with it? Is it less than 5? Yes. Fine. Is it greater than 1? No - it''s equal to 1. So the if fails, and your program ends...
Try replacing the line with:

if (choice < 5 && choice >= 1)

它可能会更进一步 - 但使用调试器!这并不困难,它为您节省了大量时间和猜测......:笑:

and it might get a bit further - but use the debugger! It''s not difficult, and it saves you a lot of time and guesswork... :laugh:



这篇关于帮助完成任务C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:11