C ++地图,开关和主菜单问题

时间:2017-07-27 21:38:35

标签: c++ dictionary menu switch-statement

当我运行主菜单时,我能够添加地图所需的数据,但是当我回到菜单并发现当我尝试打印数据时,没有出现问题。如果您想知道,没有错误,当您从菜单中运行打印联系人时,它将不会打印存储在地图中的数据。以下是代码。

主要

#include <iostream>
using namespace std;
#include "AddContact.h"


//PASSWORD IS "Delta" use upper case D and the rest is lower case
int main() {
    AddContact con;
    con.Menu();

    return 0;
}

AddContact.h

#include <iostream>
#include<cstdlib>
#include<map>
using namespace std;

#ifndef ADDCONTACT_H_
#define ADDCONTACT_H_

class AddContact {
public:
    std::map<int, AddContact> people;
private:
    string ContactName;
    long long ContactPhone;
    string Address;
    string Email;
    string Skype;

public:
    AddContact();
    AddContact(string ContactName, long long ContactPhone, string Address, string Email, string Skype);
    void ADD();
    void print()const;
    void view();
    void Menu();
    void Password();
};

#endif /* ADDCONTACT_H_ */

AddContact.cpp

#include "AddContact.h"
AddContact::AddContact(): ContactName(""), ContactPhone(0), Address(""), Email(""), Skype(""){

}
AddContact::AddContact(string ContactName, long long ContactPhone, string Address, string Email, string Skype) {
    this->ContactName = ContactName;
    this->ContactPhone = ContactPhone;
    this->Address = Address;
    this->Email = Email;
    this->Skype = Skype;
}
void  AddContact::Password(){
    cout << "Please Enter Your Password. " << endl;
        string pass;
        cin >> pass;
        if (pass != "Delta") {
            Password();
        }
}
void  AddContact::Menu(){

    cout << "!!MyCircle Contact!!" << endl;
    cout << endl;
    Password();
    int selection;
    do {
        cout << "***************************" << endl;
        cout << "1. Add New Contact         " << endl;
        cout << "2. Display Contact      " << endl;
        cout << "3. Quit                    " << endl;
        cout << "***************************" << endl;
        cin>> selection;
        switch(selection){
        case 1:
            ADD();
            break;
            view();
            break;
        }
    } while (selection != 0);



}
void AddContact::ADD() {
    for (int i = 0; i < 1; i++) {
        string temp1;
        cout << "Please Enter Contact Name: " << endl;
        cin >> temp1;
        long long temp2;
        cout << "Please Enter Contact Phone Number: " << endl;
        cin >> temp2;
        int choice;
        cout<< "Press 1 if you would like to add additional contact info or press 0 to return to main menu "<< endl;
        cin >> choice;
        if (choice == 1) {
            string temp3 = "";
            cout << "Please enter contact Address: " << endl;
            cin>> temp3;
            getline(cin, temp3);
            string temp4= "";
            cout << "Please enter contact Email: " << endl;
            cin >> temp4;
            string temp5 = "";
            cout << "Please enter contact Skype: " << endl;
            cin >> temp5;
            people[i] = AddContact(temp1, temp2, temp3, temp4, temp5);
    } else {

    }
}
}
void AddContact::print()const{
    cout<<ContactName<<" "<<ContactPhone<<" "<<Address<<" "<<Email<<" "<<Skype<<endl;
}
void AddContact::view(){
    for(map<int, AddContact>::iterator it = people.begin(); it != people.end();it++){

            it->second.print();
        }
}

1 个答案:

答案 0 :(得分:0)

看起来您正在切换声明中的default

switch(selection){
    case 1:
        ADD();
        break;
        view();
        break;
}

意味着

switch(selection){
    case 1:
        ADD();
        break;
    default:
        view();
        break;
}