每當我的程序循環存儲在int array[]
內部的數據都被清除。在循環中,數組總是被清除
當用戶選擇第一個選項時,我每做一次count和count2檢查,但它正在重置而不是增量。
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class MissionPlan //start of MissionPlan class
{
public:
MissionPlan();
}; //end of MissionPlan class
MissionPlan::MissionPlan()
{
int choice; // to capture what user inputs into menu
int count=0; // to count how many times it save into array and use it later for looping
int count2=0;//for adding x and y coordinates correctly into array
int coor [100]; //storing x and y coordinates
float index[100];//storing the civ index
cout<<"Welcome to Mission Plan program!"<<endl<<endl<<"1) Input statistical data"<<endl<<"2) Compute civ.index value(for all records)"<<endl<<
"3) Print top 5 exploration destinations"<<endl<<"4) Print total travel distance"<<endl<<endl<<"Please enter your choice: ";
cin>>choice;
for(;;)
{
if(choice == 1)
{
cout<<count<<endl;
cout<<count2<<endl;
int x,y; // for reading x and y coordinate
cout<<"Please enter x-ordinate: "; //Display x-ordinate
cin>>x;//reading input from user and put into x
coor[count2] = x;//storing x coordinate into coor[] array
cout<<"Please enter y-ordinate: ";//Display y-ordinate
cin>>y;//reading input from user and put into x
coor[1+count2] = y;//storing y coordinate into coor[] array
cin.clear();//clearing cin
cin.ignore(10000,'\n');//to ignore char to 10000 and a linefeed
count++;
count2 +=2;
cout<<count<<endl;
cout<<count2<<endl;
return;
}
else if(choice == 2)
{
cout<<"choice 2 "<<endl;//to display
return;
}
else if(choice==3)
{
cout<<"choice 3"<<endl;
return;
}
else
cout<<"Please enter number 1 to 4 only!"<<endl;
}//end of while loop
}//end of MissionPlan()
int main()
{
for(;;)
{
MissionPlan();
}
return 0;
}
你是指在函數內聲明的數組嗎?每次調用函數時都會創建並銷燬它們。這就是局部變量應該如何工作。 –
@ bo-persson我的數組是在MissionPlan()構造函數裏面創建的,如果是這樣的話,我該如何分割它以使它成爲全局的?也是我的計數器,我需要它能夠在整個程序中循環,而不需要重新設置。 :( –