将用户输入存储到2D阵列时,值将被覆盖

时间:2015-04-03 09:57:32

标签: c++

我对C ++很陌生,我试图编写一个系统,用户正在提示选择。因此,当用户选择选项1时,他们会提示输入7值并存储到2D数组中。但这是存储部件问题的来源,当用户再次选择选项1输入7值时,数组中的值将被覆盖。当用户选择选项2时,打印出的值是最新输入值的循环。

例如,输入的第一组7值是'1,2,3,4,5,6,7',而当用户再次选择选项1时输入的第二组值是'a,b, c,d,e,f,g',当我打印出阵列时,阵列只显示g而不是7和g。

希望有人能就此问题向我提出建议。

        #include <iostream>     // cin, cout
        #include <fstream>      // ifstream, ofstream
        #include <sstream>      // stringstream
        #include <string>
        #include <stdio.h>
        #include <iomanip>      // std::setw
        #include <stdlib.h>     /* atoi */
        #include "Printer.h"
        #include <termios.h>    //password masking
        #include <unistd.h>
        using namespace std;


        void displayoption(int);
        void GetInput();
        void DisplayInput();
        void writeUserDatabase();

        struct DataPile
        {
            // to record stock information.
            string x;
            string y;
            string suntype;
            string planet;
            string moons;
            string particulate;
            string plasma;

        };



        const int MAX = 50;
        Printer printer;
        DataPile datapile[MAX][7];






        int main()
        {
             system("clear");
              printer.printName();
        printer.printmainheader(); //display



        int option =-1; //initialise int
            do
            {

            printer.printdisplaymainmenu(); //display menu
            cout<< endl<<"        "<<"Enter Option :";

            cin>>option;
            displayoption(option);

            }while(option!=5);


        }





        void displayoption(int option)
        {
        //option control functions
            switch(option)
            {
            case 1:
            GetInput();

            break;
            case 2:
            printer.printcompute();
            DisplayInput();



            //addstock();
            break;
            case 3:

            break;
            case 4:

            break;
            case 5:

            break;
            case 6:

            break;
            case 9:

            break;
            default:break;
            }
        }




        void GetInput()
        {

            string x;
            string y;
            string suntype;
            string planet;
            string moons;
            string particulate;
            string plasma;


            //getting input from user
            cout<<"";
            getline(cin, x);
            cout<<"Please enter x-ordinate :";
            getline(cin, x);

            cout<<"Please enter y-ordinate :";
            getline(cin, y);

            cout<<"Please enter sun type :";
            getline(cin, suntype);

            cout<<"Please enter no. of earth-like planets :";
            getline(cin, planet);

            cout<<"Please enter no. of earth-like moons :";
            getline(cin, moons);

            cout<<"Please enter ave. particulate density (%-tage) :";
            getline(cin, particulate);

            cout<<"Please enter ave. plasma density (%-tage) :";
            getline(cin, plasma);




            //cout<<x<<" "<<y<<suntype<<planet<<moons<<particulate<<plasma<<endl;


            //storing to array
        for(int i = 1; i <=8; i++){
                    for (int j=1 ; j<=7 ; ++j)  {


            datapile[i][j].x = x;
            datapile[i][j].y = y;
            datapile[i][j].suntype = suntype;
            datapile[i][j].moons = moons;
            datapile[i][j].planet = planet;
            datapile[i][j].particulate = particulate;
            datapile[i][j].plasma = plasma;
            }
        }


            cout<<"     \E[1;29mRecord successfully stored. Going back to main menu\E[0m"<<endl;
         printer.printName();

        }


        void DisplayInput()
        {

            string x;
            string y;
            string suntype;
            string planet;
            string moons;
            string particulate;
            string plasma;


            for (int i=0; i<=1; i++)
        {
            for(int j=0; j<=6; j++)
            {
                cout << datapile[i][j].plasma<<endl;

            }
        }

        }

1 个答案:

答案 0 :(得分:0)

在此循环中,使用+ =而不是+。这会将新输入附加到字符串上,因此您同时拥有旧值和新值。

for(int i = 1; i <=8; i++){
    for (int j=1 ; j<=7 ; ++j)  {
        datapile[i][j].x += x;
        datapile[i][j].y += y;
        datapile[i][j].suntype += suntype;
        datapile[i][j].moons += moons;
        datapile[i][j].planet += planet;
        datapile[i][j].particulate += particulate;
        datapile[i][j].plasma += plasma;
        }
    }