无论正确的值如何,Switch语句都会直接变为默认值

时间:2016-09-24 19:30:45

标签: switch-statement

    /*
    Generate a Sales Report:
    (+) 1. Use enum to use salespersons' names as values

    2. File format with person's last name

    -each salesperson gets seperate file (.dat extension)

    3. line in file contains item#, last name, and quantity
       -1 file created for each item

    4. Invalid salepersons' names generate error message

    5. Invalid product number generates error message & written to salespersons' file
      -don't compute total 4 item then!!!

    Givens 
    8 products list:
    item #7 @$345.00
    item #8 @$853.00
    item #9 @$471.00
    item #10 @$933.00
    item #11 @$721.00
    item #12 @$663.00
    item #13 @$507.00
    item #14 @$259.00
    */

    #include<iostream>
    #include<fstream>
    #include<string>
    #include<cctype>
    using namespace std;

    enum Salesperson{ Dixon, Bolly, Ki, Kee, Niles, Hamil, Cams, Tyson };   
    string repname[]={"Dixon", "Bolly","Ki", "Kee", "Niles","Hamil","Cams","Tyson"};


    struct Salespeople
    {
    string lname;

    int itemnum;

    int quantity;

    float price;
    }
    ;

    Salespeople salesrep[8];  //for the 8 salesreps


    //Name (string) Verification Functions
    string toUpper(string s)
    {

    for (int i = 0; i < s.size(); i++)
    {
    s[i] = toupper(s[i]);
    }

    return s;
    }


    int placement(string s)
    {
    int verified = -1;  

    for ( int i= 0; i < 8; i++)
    {
    if(s == repname[i])

    verified = i;
    }
    return verified;
    }
    ///////////////////////////////


    int main()
    {

    //File I/O Setup
    ofstream inputfile ("salesheet.txt"); //input file will be called salesheet.txt 
    inputfile <<"Salesperson \t\t"<<"Item # \t\t"<< "Quantity";

    inputfile.close();
    ////////





    cout << "\n These Are SalesRep Names";
    /*
    1. for loop used to loop switch statement
    - looped switch statement allows for programmer and user to have more freedom of how
    information is inputted/outputted
    -for whatever reason wrong information is entered, user/programmer can decide at what point
    to start from
    -for loop used soley for this utility doesn't allow to start from a special point; for
    loops force user through entire loop
    -if forced to re-enter information through entire loop, file could be overwritten and/or
    contain duplicates
    */
    int i;
    for( i = 0; i < 8; i++)
    {

    cout <<"\n\t";  
    cout << repname[i];

    } 
    int rep;
    int go;
    for( go = 0; go < 8; go++)
    {
    string name;
    cout << "\n\n Enter SalesRep Name: ";
    cin >> name;



    //These below functions make use of the enum.
    name = toUpper(name);
    rep  = placement(name);      


    switch(rep)
    {
    case Dixon:
        cout <<"\n Salesperson Dixon, put in Last Name";
        cin >> salesrep[1].lname;

        cout << "\n Enter Item#: ";
        cin >> salesrep[1].itemnum;

        cout <<"Enter Item Quantity";
        cin >>salesrep[1].quantity;

    inputfile <<"\n"<< salesrep[1].lname <<"\t" << salesrep[1].itemnum <<"\t" <<salesrep[1].quantity;
        break;


    case Bolly:
        cout <<"\n Salesperson Bolly, put in Last Name";
        cin >> salesrep[2].lname;

        cout << "\n Enter Item#: ";
        cin >> salesrep[2].itemnum;

        cout <<"\n Enter Item Quantity";
        cin >>salesrep[2].quantity;

        break;



    case Ki:
        cout <<"\n Salesperson Ki, put in Last Name:  ";
        cin >> salesrep[4].lname;

        cout << "\n Enter Item#: ";
        cin >> salesrep[4].itemnum;

        cout <<"\n Enter Item Quantity";
        cin >> salesrep[4].quantity;

        break;


    case Kee: 
        cout <<"\n Salesperon Kee, put in Last Name:  ";
        cin >> salesrep[5].lname;

        cout << "\n Enter Item#: ";
        cin >> salesrep[5].itemnum; 

        cout <<" \n Enter Item Quantity";
        cin >> salesrep[5].quantity;

        break;

    case Niles:
        cout <<"\n Salesperson Niles, put in Last Name";
        cin >> salesrep[3].lname;

        cout << "\n Enter Item#: ";
        cin >> salesrep[3].itemnum;

        cout <<"\n Enter Item Quantity";
        cin >> salesrep[3].quantity;

         break;


    case Hamil: 
        cout << "\n Salesperson Hamil, put in Last Name";
        cin >> salesrep[6].lname;

        cout << "\n Enter Item#: ";
        cin >> salesrep[6].itemnum;

        cout <<"\n Enter Item Quantity";
        cin >> salesrep[6].quantity;

        break;


    case Cams:
        cout <<"\n Salesperson Cams, put in Last Name";
        cin >> salesrep[7].lname;

        cout << "\n Enter Item#: ";
        cin >> salesrep[7].itemnum;

        cout <<"Enter Item Quantity";
        cin >> salesrep[7].quantity;    


        break;


    case Tyson: 
         cout <<"\n Salesperson Tyson, put in Last Name";
         cin >> salesrep[8].lname; 

         cout << "\n Enter Item#: ";
         cin >> salesrep[8].itemnum;

         cout <<"Enter Item Quantity";
         cin >> salesrep[8].quantity;

         break;

    default: cout <<"\n ERROR: Imaginary Salesperson!";
    }
    }

    return 0;
    }

当我选择'Dixon'作为其中一个开关值时,我的开关直接进入默认值。我正在使用toUpper()和placement()来验证输入的名称是否存在。为什么我输入一个现有的名称,它会直接进入默认状态?我输入的名称与enum和repname []中的名称完全相同。

0 个答案:

没有答案