Каждый раз, когда моя программа выполняет цикл, данные, которые хранятся внутри int array[]
, очищаются.в цикле, массив всегда очищается
Я делал подсчет и счет2 каждый раз, когда пользователь выбирает 1-й вариант, но он получает сброс вместо приращения.
#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() внутри, если да, то как это сделать, чтобы он был глобальным? а также для счетчика счетчиков, мне нужно, чтобы он мог перемещаться по всей программе без его перепродажи. :( –