c ++将数组元素推向右侧

时间:2017-08-16 10:29:14

标签: c++ arrays elements

我是编程新手并参加了这个c ++学校的测验。

我想将输入的数组元素推到右边。

这是我现有的代码

  

块引用

    // automatac++.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <list>
#include <iterator>
#include <conio.h>
#include <cstring>

//using namespace std;
using std::cout;
using std::cin;
using std::endl;
void menu_sel();
void push_element();
//void rem_element();

int in;
char array[100];

int p_elements;

int main()
{

    menu_sel();

    return 0;
}

void push_element() {
    int p_elements;

    for (int e = 0; e < 10; e++) {


    }

    cout << "Enter number of elements:";
    cin >> p_elements;
    cout << "Enter only  " << p_elements << " elements"<<endl;

    for (int e = 0; e < p_elements; e++  ) {
        cin >> array[e];
    }
    cout << "Pushed elements are :";
    for (int e = 0; e < p_elements; ++e) {
        cout << array[e]<<" ";

    }

    //getch();
    menu_sel();

}
/*
void rem_element() {
    char remove;
    int arr_position;

    cout << "You have selected Pop.\nRemove elements from arrays \n";
    cout << "Enter data to remove";
    cin >>remove;

    for (int ie -) {

    }


    system("pause");



}
*/


void menu_sel() {


    int input;
    cout << "\n****Menu Selection Here****";
    cout << "\n1. Push \n2. Pop \n3. Exit \n";
    cout << "select options here :";
    cin >> input;

    switch (input) {
    case 1:
        cout << "You have selected Push Stack\n";
        push_element();
        break;

    case 2:
        //cout << "You have selected Pop Stack\n";
        //rem_element();
        break;

    default:
        cout << "You have selected Invalid Options";    
        break;
        system("cls");
        return;

    }
}

这是输出

the output shows pushing elements to the left

输出为3 e r 4

我想要显示这样的输出&#34; 4 r e 3&#34;

感谢和问候,

1 个答案:

答案 0 :(得分:0)

如果要以这种方式显示,您可以随后向后读取数组以进行输出。

for (int e = p_elements-1; e >= 0; --e) {
    cout << array[e]<<" ";

}