0

配列内の要素を検索して、行全体を出力するにはどうすればよいですか? 私はこのような回避策を行ってきましたが、期待したものを出力できないようです。

#include <iostream>
#include <stdlib.h>
#include <string>
#include <ctype.h>
#include <sstream>

using namespace std;

int main()  {

    string items[9][3] = {{"A","BALOT","25.00"},
                            {"B","CANTON","20.00"},
                            {"C","NIDO","100.00"},
                            {"D","KETCHUP","50.00"},
                            {"E","MAGGI","15.00"},
                            {"F","ALASKA","60.00"},
                            {"G","VINEGAR","25.00"},
                            {"H","OIL","70.00"},
                            {"I","COKE","10.00"}};

    // PARA SA MAPRINT YUNG ARRAY.
    cout << "MANG JUAN'S 10-DAHAN\n\n";
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 3; j++)
            cout << items[i][j] << ( (j < 2) ? "-" : "\t" ); 

        if (i < 6) {
            cout << "\t";
            i += 2;
        }
        else if (i != 8) { 
            cout << "\n";
            i -= 6;
        }
    } // END OF ARRAY PRINTING

    char choice;
    int ctr = 1, quantity;
    string purchased;

    cout << "\n\nWOULD YOU LIKE TO PURCHASE? Y/N\n\n";
    cin >> choice;

    if(choice == 'n' || choice == 'N') {
        cout << "Thank you. ";
    }
    else if(choice == 'y' || choice == 'Y') {
        string numPref;
        while (true) {
            if(ctr > 11) {
                cout << "\n\nTHE SYSTEM EXCEEDED ITS LIMIT\n\n";
                break;
            } else {
                if(ctr == 1) numPref = "st";
                else if(ctr == 2) numPref = "nd";
                else if(ctr == 3) numPref = "rd";
                else if(ctr > 3) numPref = "th";
            }
            cout << "\n\nPLEASE ENTER " << ctr << numPref << " ITEM:\t";
            cin >> purchased;
            if(!cin) { 
                cout << "Letters only";
                break;
            } else {
                if(true) {
                    cout << "HOW MANY? ";
                    cin >> quantity;
                    if(!cin) {
                        cout << "Enter number only. ";
                        break;
                    } else {
                        cout << "PRICE PER ITEM: ";

                    ///////// Look for the element and print the entire row /////////////
                        string matchedRow[3];
                        for (int i = 0; i < 3; i++) {
                            string oneRow[] = items[i];
                                if (oneRow[0] == purchased) {
                                    matchedRow = oneRow;
                                    break;
                                }
                        }
                        for (int i = 0; i < matchedRow.length; i++) {
                            cout << matchedRow[i] + "\t\t";
                        }

                    ////////////////////////////////////////////

        }


                        ctr++; 
                    } // end of else - if (!cin) for quantity input check
                } // end of char check

            } // End of else for (!cin)

        } // End of while loop for numPref


    } // End of else if (choice)

    system("PAUSE");
    return 0;

}

例: ユーザーが を入力AするPlease enter itemと、プログラムはPrice per itemと対応する価格を配列に出力します。

Sample Run:
lease enter item: A // user input
How many? 3 // user input
Price per item: 25.00 // not user input
4

3 に答える 3

1
#include <algorithm>
#include <iostream>

int main()  {

    // Keep it sorted!
    std::string items[9][3] = {
        {"A","BALOT","25.00"},
        {"B","CANTON","20.00"},
        {"C","NIDO","100.00"},
        {"D","KETCHUP","50.00"},
        {"E","MAGGI","15.00"},
        {"F","ALASKA","60.00"},
        {"G","VINEGAR","25.00"},
        {"H","OIL","70.00"},
        {"I","COKE","10.00"}};

    typedef std::string Item[3];
    Item* last_item = items + sizeof(items) / sizeof(Item);

    struct Less {
        bool operator () (const Item& a, const std::string& b) const {
            return a[0] < b;
        }
    };
    std::string in;
    std::cin >> in;
    Less less_item;
    Item* item = std::lower_bound(items, last_item, in, less_item);
    if(item == last_item || (*item)[0] != in) std::cout << "Not found" << std::endl;
    else std::cout << "Found: " << (*item)[0] << ": " << (*item)[1] << ", Price = " << (*item)[2] << std::endl;
    return 0;
}
于 2013-08-31T11:08:33.747 に答える
1

ポインターを使用して行をコピーします。

string *oneRow = items[i];

次に、次のように価格にアクセスできます。

oneRow[2]

変更されたプログラム:

#include <iostream>
#include <stdlib.h>
#include <string>
#include <ctype.h>
#include <sstream>

using namespace std;

int main()  {

    string items[9][3] = {{"A","BALOT","25.00"},
                            {"B","CANTON","20.00"},
                            {"C","NIDO","100.00"},
                            {"D","KETCHUP","50.00"},
                            {"E","MAGGI","15.00"},
                            {"F","ALASKA","60.00"},
                            {"G","VINEGAR","25.00"},
                            {"H","OIL","70.00"},
                            {"I","COKE","10.00"}};

    // PARA SA MAPRINT YUNG ARRAY.
    cout << "MANG JUAN'S 10-DAHAN\n\n";
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 3; j++)
            cout << items[i][j] << ( (j < 2) ? "-" : "\t" ); 

        if (i < 6) {
            cout << "\t";
            i += 2;
        }
        else if (i != 8) { 
            cout << "\n";
            i -= 6;
        }
    } // END OF ARRAY PRINTING

    char choice;
    int ctr = 1, quantity;
    string purchased;

    cout << "\n\nWOULD YOU LIKE TO PURCHASE? Y/N\n\n";
    cin >> choice;

    if(choice == 'n' || choice == 'N') {
        cout << "Thank you. ";
    }
    else if(choice == 'y' || choice == 'Y') {
        string numPref;
        while (true) {
            if(ctr > 11) {
                cout << "\n\nTHE SYSTEM EXCEEDED ITS LIMIT\n\n";
                break;
            } else {
                if(ctr == 1) numPref = "st";
                else if(ctr == 2) numPref = "nd";
                else if(ctr == 3) numPref = "rd";
                else if(ctr > 3) numPref = "th";
            }
            cout << "\n\nPLEASE ENTER " << ctr << numPref << " ITEM:\t";
            cin >> purchased;
            if(!cin) { 
                cout << "Letters only";
                break;
            } else {
                if(true) {
                    cout << "HOW MANY? ";
                    cin >> quantity;
                    if(!cin) {
                        cout << "Enter number only. ";
                        break;
                    } else {
                        cout << "PRICE PER ITEM: ";

                    ///////// Look for the element and print the entire row /////////////
                        string *matchedRow;
                        const int length = 3;
                        for (int i = 0; i < 3; i++) {
                            string *oneRow = items[i];
                                if (oneRow[0] == purchased) {
                                    matchedRow = oneRow;
                                    cout << matchedRow[2];
                                    break;
                                }
                        }
                        //you don't need this
                        /*
                        for (int i = 0; i < length; i++) {
                            cout << matchedRow[i] + "\t\t";
                        }
                        */
                    ////////////////////////////////////////////

                    }


                        ctr++; 
                    } // end of else - if (!cin) for quantity input check
                } // end of char check

            //} // End of else for (!cin)     //spare bracket

        } // End of while loop for numPref


    } // End of else if (choice)

    //system("PAUSE");
    //return 0;

}
于 2013-08-31T12:54:41.593 に答える
0

文字列oneRow[]を定義した行で、そうあるべきだと思います

string oneRow[3] = {"Z","WATER","00.00"}; // default initialization
oneRow[0] = items[i][0];
oneRow[1] = items[i][1];
oneRow[2] = items[i][2];
于 2013-08-31T10:56:36.050 に答える