简体   繁体   中英

How to store user input in an int array

I need to ask the user to input 8 zip codes and then store them in an array of integers and then to output them one by one , each being in a new line.These two things should be done in separate functions. But when it runs the code first time it shows only menu, then second time in loop when i enter L and input 8 zipcodes it shows this error Enter your choice: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string(in here optionInChar=optionInString.at(0);)

using namespace std;

void DisplayCityZipCodes();
int LoadCityZipCodes(int ZipCodes[],int SIZE);
void DisplayCityZipCodes(int ZipCodes[],int SIZE);
void DisplayMenu();
char GetOption();

int main(){

int const SIZE=8;
int ZipCodes[SIZE]={0};

bool moreWork=true;



option=GetOption();

 while(moreWork) {

  DisplayMenu();

 option=GetOption();

  switch(option){

     case 'L':
    ZipCodes[SIZE]= LoadCityZipCodes(ZipCodes,  SIZE);

     break;

        cout<<"D";
     case 'D': DisplayCityZipCodes(ZipCodes,  SIZE);
     break;

   }
 }
}

void DisplayMenu(){
cout<<"       **********************\n\n";

cout<<"       San Jose City Zip codes\n\n";

cout<<"       **********************\n\n";

cout<<"1. Load City zip codes\n";
cout<<"2. Display all City zip codes\n";
cout<<"3. Search a City zip code\n";
cout<<"4. Reverse the City zip code List\n";
cout<<"5. Quit\n";

}

char GetOption(){
string optionInString="";
char optionInChar='a';
cout<<"\n\nEnter your choice: ";
getline(cin,optionInString);
optionInChar=optionInString.at(0);
cout<<"\n";
return optionInChar;

  }



int LoadCityZipCodes(int  ZipCodes[],int  SIZE){

cout<<"PLease enter 8 city Zip Codes ";
int i=0;
for(;i<8;i=i+1){
cin >>ZipCodes[i];
}
return ZipCodes[i];


}

void DisplayCityZipCodes(int ZipCodes[],int SIZE){
int i=0;
for(;i<8;i=i+1){
cout<<ZipCodes[i]<<endl;
}


}

Now I have not had a hand on C++ for a long time, but this will work. You should look on the variable declarations and some other things though.

#include<iostream>
#include<conio.h>

using namespace std;

void DisplayCityZipCodes();
void LoadCityZipCodes();
void DisplayMenu();
int const SIZE = 8;
int ZipCodes[SIZE];

int main()
{
    DisplayMenu();
    return 0;
}

void DisplayMenu()
{
    int ch;
    cout << "       **********************\n\n";
    cout << "       San Jose City Zip codes\n\n";
    cout << "       **********************\n\n";
    do
    {

        cout << "1. Load City zip codes\n";
        cout << "2. Display all City zip codes\n";
        cout << "3. Search a City zip code\n";
        cout << "4. Reverse the City zip code List\n";
        cout << "5. Quit\n";

        cout << "\nPlease enter your choice:";
        cin >> ch;
        switch (ch)
        {
        case 1:
            LoadCityZipCodes();
            break;
        case 2:
            DisplayCityZipCodes();
            break;
        case 5:
            exit(0);   //You will need to include math.h for this.
        default:
            cout << "please enter a proper choice!";
            break;
        }
    } while (1);

}

void LoadCityZipCodes()
{
    cout << "PLease enter 8 city Zip Codes ";
    for (int i=0; i<8; i++)
    {
        cin >> ZipCodes[i];
    }

}

void DisplayCityZipCodes()
{
    for (int i=0; i<8; i++)
    {
        cout << ZipCodes[i] << endl;
    }
}

You can add remaining of your functions with case 3 and 4. Try to debug the code and see how it work before you post :)

You need to look closer at the statements

return ZipCodes[i];

and

ZipCodes[SIZE]= LoadCityZipCodes(ZipCodes,  SIZE);

In both cases you use ZipCodes[8] which is definitely out of range.

To solve both problems, don't return anything from LoadCityZipCodes (ie make it return void ), because it already sets the values in the array.

您需要记住,在编程时,我们从0开始计数。这意味着对于大小为8的数组,所有有效索引为:0、1、2、3、4、5、6、7。当您尝试访问时使用ZipCodes [8]的数组,您要查询的第9个元素超出范围。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM