c++ - 如果在数组中找到元素,则打印整行

标签 c++ arrays loops

如何在数组中搜索元素并打印整行?我一直在做这样的解决方法,但似乎无法输出我所期望的。

#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;

}

例子: 如果用户在Please enter item上输入A,程序会输出Price per item和数组中对应的价格。

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

最佳答案

使用指针复制一行:

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;

}

关于c++ - 如果在数组中找到元素,则打印整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18546931/

相关文章:

python - 如何在 Numpy 中创建具有屏蔽值的数组的直方图?

c - 正在检查编译器优化的循环中的复杂表达式?

arrays - 将两个 Bash 数组合并到键 :value pairs via a Cartesian product

c++ - 在 C++ 中测试素数

c++ - boost::bind 返回的仿函数是否只有绑定(bind)参数等同于不带参数的函数?

android - 在嵌入式上快速解决许多微小的线性规划问题

javascript - 查找一个数组中与另一个数组中的任何项目相匹配的项目

android - 如何使用 NDK 为 OpenCV 引用 hpp 文件?

c - 提取数组中的唯一元素(来自 K 和 R C ex1-14)

r - 在 R 中创建预测误差最小值的 ARIMA 模型