0

私は練習プログラムをほぼ完成させようとしていますが、ユーザー入力を指定されていないサイズの配列に格納することに行き詰まりました..

私のコードを見てください:

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

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 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, addAnother;
    int ctr = 1, quantity = 0;
    string purchased;
    double price = 0, grandTotal = 0, total = 0;

    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";
            }
//rows:
            //for(int r = 0; r < 9; r++) {
            cout << "\n\nPLEASE ENTER " << ctr << numPref << " ITEM:\t";
            cin >> purchased;

            char upp = purchased[0];
            upp = toupper(upp);
            purchased = upp;

            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 = 9;
                        for (int i = 0; i < 9; i++) {
                            string *oneRow = items[i];
                            /**if (oneRow[0] != purchased) {
                                cout << "\n\nNO ITEM FOUND!\n\n";
                                ctr--;
                            } else {
                                matchedRow = oneRow;
                                cout << matchedRow[2];
                                price = atof( matchedRow[2].c_str() );      
                                total = price * quantity;
                                grandTotal += total;    
                            } */

                               if(oneRow[0] == purchased) {
                                    matchedRow = oneRow;
                                    cout << matchedRow[2];
                                    price = atof( matchedRow[2].c_str() );                              
                                    total = price * quantity;
                                    grandTotal += total;

                                    if(oneRow[0] != purchased) {
                                        cout << "NO MATCH FOUND!" << endl;
                                        ctr--;
                                        break;
                                    }

                                }

                        } // End of for-loop for *matchedrow
                        ////////////////////////////////////////////
                        cout << "\n\nADD ANOTHER ITEM? Y/N " << endl;
                        cin >> addAnother;
                        if(addAnother == 'y' || addAnother == 'Y') {
                            ctr++; 
                        } else if(addAnother == 'n' || addAnother == 'N') {
                            // print the receipt here
                            goto receipt;
                             //break; // replace break with goto later
                        } else {
                            cout << "\n\nINVALID INPUT." << endl;
                            break;
                        } // End of if and else for addANother
                    }



                    } // 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 rows for-loop 
    } // End of else if (choice)
receipt:
    cout << "YOUR PURCHASE:" << endl;
    cout << "NET TOTAL: " << grandTotal << endl;
    system("PAUSE");
    //return 0;

}

このサンプル実行のように、購入の概要を出力したいと思います。

WOULD YOU LIKE TO PURCHASE? Y
PLEASE ENTER 1st ITEM: A
HOW MANY? 2
PRICE PER ITEM: 25.00

ADD ANOTHER? Y

PLEASE ENTER 2nd ITEM: B
HOW MANY? 1
PRICE PER ITEM: 20.00

ADD ANOTHER? N

YOUR PURCHASE:
// will display all the ordered item
// sample output
A     BALOT     50.00
B     CANTON    20.00

NET TOTAL: 70.00
4

2 に答える 2