程序不输出任何大于9的选项

时间:2015-12-06 15:08:11

标签: c++ vector output

我对C ++很陌生并且一直试图制作自己的旅行计划。出于某种原因,在将信息推回​​向量之后,根据输出大于9的输出,程序拒绝输出它。例如,该计划中有10个城市,我选择访问城市1 2 3 10.该计划仅输出城市1,2,3。非常感谢帮助。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <fstream>

using namespace std;

struct Cities
{
string name;
double x;
double y;
};

void add_city(string add, Cities temp, vector<Cities>&file_info)
{
cin>>add;

if (add!="Y" && add!="N")
{
    cout<<"Incorrect input, please enter Y or N: "<<endl;
    cin>>add;
}
if(add=="Y")
{
    while (add=="Y")
    {
        cout<<"enter name of destination, x and y coordinates "<<endl;
        cin>>temp.name>>temp.x>>temp.y;                     
        file_info.push_back(temp);
        cout<<"If you would like to add another city, please enter Y, if not, please enter N: "<<endl;
        cin>>add;
    }

}

if(add=="N")
{
    for (int i=0; i<file_info.size();i++)
        cout<<i+1<<'\t'<<file_info[i].name<<endl;
}
}

void Distances_between_cities(vector<double>distances2, double minimum_distance, double maximum_distance, double sum_of_distances)
{
minimum_distance=0;
maximum_distance=0;
sum_of_distances=0;
distances2.erase(std::remove(distances2.begin(), distances2.end(), 0), distances2.end());
minimum_distance=*min_element(distances2.begin(), distances2.end()); //displays 0 as 0 is the dist between same cities so need to change that
cout<<endl<<"Minimum distance you will travel: "<<minimum_distance<<endl;
maximum_distance=*max_element(distances2.begin(), distances2.end()); //sorts through the vector, finds the biggest element in the vector
cout<<"Maximum distance you will travel: "<<maximum_distance<<endl; //displays the biggest element of the vector
for (int n: distances2)
    sum_of_distances +=n;
cout<<"Total distance: "<<sum_of_distances/2<<endl;
}

void password_protection(string password, string password_input)
{
 cin >> password_input;
 while (password_input!=password)
 {
    cout << "Incorrect password, please re-enter password: "<<endl;
    cin>>password_input;
}
if (password_input==password)
    cout<<"Authorised."<<endl;
}

void clear_vectors(vector<Cities>&file_info, vector<Cities>&choices)
{
file_info.swap(std::vector<Cities>());
choices.swap(std::vector<Cities>());

}

void Table(vector<Cities>&choices, double dist, vector<double>&distances2 )
{
for (int i=0; i<choices.size(); i++)
cout<<'\t'<<choices[i].name<<'\t';
                cout<<endl;
for (int a=0; a<choices.size(); a++)
                {
                cout<<choices[a].name<<'\t';
                    for (int b=0; b<choices.size(); b++)
                    {
                        double xi=choices[a].x;
                        double xa=choices[b].x;
                        double yi=choices[a].y;
                        double ya=choices[b].y;
                        dist=sqrt((xi-xa)*(xi-xa)+(yi-ya)*(yi-ya));
                        distances2.push_back(dist);
                        cout<<dist<<'\t'<<'\t';
                    }
                    cout<<endl;
                }
}

void read_from_file(string name_of_file, Cities temp,  vector<Cities>file_info)
{
cout<<"Please enter the name of the file you want to read information from: "<<endl;
        cin>>name_of_file;
         ifstream file;
         file.open(name_of_file);
         if (file.is_open())
        {
            while(file>>temp.name>>temp.x>>temp.y)
            {
                file_info.push_back(temp);
            }

            for (int i=0; i<file_info.size(); i++)
                 cout<<i+1<<'\t'<<file_info[i].name<<endl;

        }
        else 
            cout<<"Error.File not open"<<endl;
}

int main()
{
Cities temp;
ifstream file;
string name_of_file;
vector<Cities> file_info;
vector<Cities>choices;
vector<double> distances;
vector<int>x_coordinate;
vector<int>y_coordinate;
vector<double>distances2;
int sum_of_distances=0;
double minimum_distance;
double maximum_distance;
double b;
double dist;
int user_input;
string password = "anastasia";
string password_input;
string name;
string add;
string Y;
string N;
string add_name;
string input_to_start_program;
string plan;
string Option;
while (1)
{

    cout<<"::::MENU::::"<<'\n'
        <<"::1.Run Program::"<<'\n'
         <<"::2.Display File Contents::"<<'\n'
        <<"::3.Instructions::"<<'\n'
        <<"::4.Quit Program::"<<'\n'
        <<"Your Option: ";
    cin>>Option;

    if (Option=="1")
    {
        cout<<"Welcome to Richardson Travel! Please enter your name: "<<endl;
        cin>>name;
        cout<<"Welcome, "<<name<<endl<< "Please enter your password: ";
        password_protection(password, password_input);
        cout<<"Dear, "<<name<<", if you would like to use Richardson Travel for your holidays, please enter: 'plan'."<<endl;
        cin>>input_to_start_program;
        while (input_to_start_program=="plan")
        {
            cout<<"Please enter the name of the file you want to read information from: "<<endl;
            cin>>name_of_file;
            file.open(name_of_file);
                if (file.is_open())
            {
                    while(file>>temp.name>>temp.x>>temp.y)
                {
                        file_info.push_back(temp);
                }

                for (int i=0; i<file_info.size(); i++)
                    cout<<i+1<<'\t'<<file_info[i].name<<endl;

                cout<<"In Richardson Travel we want you to be happy, and this is why we let you to add a city that is not on our list!"<<endl;
                cout<<"If you would like to use that option, please enter Y. If you are happy with the destinations we have on offer, please press N."<<endl;
                add_city(add, temp, file_info);

                cout<<"Please enter where you would like to go by entering integer values corresponding to the city names in order of when you would like to visit"<<endl<<"Enter 0 when you would like to stop planning your journey"<<endl;

                while(cin>>user_input && user_input!=0 && static_cast<size_t>(user_input) < file_info.size())
                {
                    choices.push_back(file_info[user_input-1]);
                    //cout<<"vector size = "<<choices.size()<<endl; <- tests whether the vector is being pushed back
                }

                for (int i=0; i<choices.size(); i++)
                    cout<<choices[i].name<<endl;

                cout<<"Thank you."<<endl<<"Here is a table of distances between the cities you have chosen: "<<endl;
                Table(choices, dist=0, distances2);
                    cout<<"Your possible route is: "<<endl;
                for(int i=0; i<choices.size(); i++)
                {
                    cout<<choices[i].name<<"  "<<"->"<<distances2[i]; // needs to display different dist
                }
                Distances_between_cities( distances2, maximum_distance=0, minimum_distance=0, sum_of_distances=0);

                cout<<"If you would like to plan another holiday with us, please enter 'plan', if you would like to access the menu, please enter anything else"<<endl;
                clear_vectors(file_info, choices);
                cin>>input_to_start_program;    
                system("cls");

            }
            else
                cout<<"Error. File not open"<<endl;
        }

    }
    else if(Option=="2")
    {
        system("CLS");
        read_from_file(name_of_file, temp, file_info);
        clear_vectors(file_info, choices);
        cout<<"Thank you, after pressing any key you will be redirected to the main menu"<<endl;
        system("pause");
        system("CLS");
    }
    else if (Option=="3")
    {
        system("CLS");
        cout<<"::INSTRUCTIONS::"<<'\n'
            <<"Wecome to Richardson Travel."<<'\n'<<"In order to use our program you have to have a file with the information about destinations stored. The file has to be attached to Resource Files folder in Visual Studio 2012. The file also has to be in the same folder as the source file. When prompted to enter the file name, the name of the file has to be entered accurately (case sensitive) and has to end with the type of file (eg. txt)"<<endl;
        cout<<"Thank you, after pressing any key you will be redirected to the main menu"<<endl;
        system ("PAUSE");
        system ("CLS");
    }
    else if(Option=="4")
    {
        return 0;
    }
    else if (Option>"4" || Option<"1")
    {
        system("CLS");
        cout<<"Invalid input"<<endl;
        system("PAUSE");
        system("CLS");
    }
    else
    {
        system ("CLS");
        cout<<"Invalid input"<<endl;
        system("Pause");
        system ("CLS");
    }

}
system ("pause");
}

在编辑中,我为很多事情添加了单独的功能,这让我想到了另一个问题。如何修改read_from_file函数,以便我可以在main函数中轻松使用(现在我只使用它,如果用户从菜单中选择选项2,因为我似乎无法弄清楚如何修改它不要使用while循环),因为该程序是基于while.of file.is_open循环?

0 个答案:

没有答案