在while循环和Visual Studio中遇到问题

时间:2019-05-06 22:13:41

标签: visual-c++

我是编码的新手,已经看了很多教程并购买了有关c ++的书,并且正在通过Microsoft Visual Studio 2019进行编程。我正在尝试为我运行的游戏会话做一个存储程序,但是一些问题。 1.在Visual Studio中,每次更改内容时,都会说“ cin”和“ cout”是模棱两可的,并且到处都会出错。 2.我的前哨循环无法正常工作。 3.当我进入程序菜单时,它会卡在其中。

请提供任何建议。真的很想在本周比赛之前完成这项工作。

当我尝试更改某些内容然后再次运行我的代码时,它将像没有更改任何内容一样运行它。

............................................... ............................

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
    //declare variables and file stream
    ofstream outData;
    string name;
    char cont;
    int selection;
    int itemChoice;
    int itemCount=0;
    int price;
    int quantity = 0;
    double averageItem, averageTotal;
    int howMany;
    int total = 0;
    int itemTotal;


    //open the output file.
    outData.open("WWWW-Invoice.dat");

    //Greet the customer.
    cout << "Welcome to Wild Widgets Wonderous Warehouse.\n";
    cout << "Take a look around and find something that catches your eye.\n" << endl;

    //get the name of the customer.
    cout << "First I must ask. What is your name? ";
    cin >> name;
    cout << endl;

    //Start the header of the invoice
    outData << "        Wild Widget's Wonderous Warehouse\n     Invoice of services for: " << name << endl;
    outData << "item" << setfill(" ") << left << setw(25) << "quantity" << setw(10) << "Price Per" << setw(10) << "Total" << endl;
    outData << setfill("_") << setw(45) << endl;

    //Start the selection for the character of the loop.
    cout << "Well " << name << " are you ready to place your order?\nEnter y or Y to continue. ";
    cin >> cont;
    cout << endl;
    cin.get(cont);
    while ((cont == 'y') && (cont == 'Y'))
    {       
        cout << "What you would you like to buy today? \n"
            << "1. Gear \n"
            << "2. Tools \n"
            << "3. Magic Items \n"
            << "4. Leave \n";
        cin >> selection;
        cout << endl;
        if (selection < 1 && selection > 4)
        {
            cout << "Ivalid choice. \n"
                 <<"What you would you like to buy today? \n"
                 <<"1. Gear \n"
                 <<"2. Tools \n"
                 <<"3. Magic Items \n"
                 << "4. Leave \n";
            cin >> selection;
            cout << endl;
        }
        while (selection != 4)
        {

            if (selection == 1)
            {
                cout << "Gear: \n"
                    << "1. Glass Bottle \n"
                    << "2. Component Pouch \n"
                    << "3. Flask \n"
                    << "4. Healer's Kit \n"
                    << "5. Jug \n"
                    << "6. Vial \n"
                    << "7. Basic Poison \n"
                    << "8. Oil Flask \n"
                    << "9. Potion of Healing \n"
                    << "0. Back to main \n"
                    << endl;
                cin >> itemChoice;

                if (itemChoice < 0 || itemChoice > 9)
                {
                    cout << "Invalid item choice. Please try again. \n"
                        << "Gear: \n"
                        << "1. Glass Bottle \n"
                        << "2. Component Pouch \n"
                        << "3. Flask \n"
                        << "4. Healer's Kit \n"
                        << "5. Jug \n"
                        << "6. Vial \n"
                        << "7. Basic Poison \n"
                        << "8. Oil Flask \n"
                        << "9. Potion of Healing \n"
                        << endl;
                    cin >> itemChoice;
                }


                if (itemChoice == 1)
                {
                    price = 2;
                    cout << "How many Glass Bottles do you want to purchase? ";
                    cin >> howMany;
                    cout << endl;
                    quantity += howMany;

                    itemTotal = price * howMany;

                    if (howMany > 0)
                    {
                        outData << left << setw(25) << "Glass Bottles " << setfill('_') << right <<setw(10)<< howMany <<right 
                            << setw(10) << right << price << setw(10) << right << itemTotal << endl;
                        itemCount += 1;
                        total += itemTotal;
                    }

                }
                else if (itemChoice == 2)
                {
                    price = 25;
                    cout << "How many Component Pouches do you want to purchase? ";
                    cin >> howMany;
                    cout << endl;
                    quantity += howMany;

                    itemTotal = price * howMany;

                    if (howMany > 0)
                    {
                        outData << "Component Pouches       " << howMany << "           " << price << "     " << itemTotal << endl;
                        itemCount += 1;
                        total += itemTotal;

                    }
                }
                else if (itemChoice == 3)
                {
                    price = 1;
                    cout << "How many Flasks do you want to purchase? ";
                    cin >> howMany;
                    cout << endl;
                    quantity += howMany;

                    itemTotal = price * howMany;

                    if (howMany > 0)
                    {
                        outData << "Flasks      " << howMany << "           " << price << "     " << itemTotal << endl;
                        itemCount += 1;
                        total += itemTotal;

                    }
                }
                else if (itemChoice == 4)
                {
                    price = 5;
                    cout << "How many Healer's Kit do you want to purchase? ";
                    cin >> howMany;
                    cout << endl;
                    quantity += howMany;

                    itemTotal = price * howMany;

                    if (howMany > 0)
                    {
                        outData << "Healer's Kit        " << howMany << "           " << price << "     " << itemTotal << endl;
                        itemCount += 1;
                        total += itemTotal;

                    }
                }
                else if (itemChoice == 5)
                {
                    price = 1;
                    cout << "How many Jugs do you want to purchase? ";
                    cin >> howMany;
                    cout << endl;
                    quantity += howMany;

                    itemTotal = price * howMany;

                    if (howMany > 0)
                    {
                        outData << "Jugs        " << howMany << "           " << price << "     " << itemTotal << endl;
                        itemCount += 1;
                        total += itemTotal;

                    }
                }
                else if (itemChoice == 6)
                {
                    price = 2;
                    cout << "How many Vials do you want to purchase? ";
                    cin >> howMany;
                    cout << endl;
                    quantity += howMany;

                    itemTotal = price * howMany;

                    if (howMany > 0)
                    {
                        outData << "Vials       " << howMany << "           " << price << "     " << itemTotal << endl;
                        itemCount += 1;
                        total += itemTotal;

                    }
                }
                else if (itemChoice == 7)
                {
                    price = 100;
                    cout << "How many Basic Poison do you want to purchase? ";
                    cin >> howMany;
                    cout << endl;
                    quantity += howMany;

                    itemTotal = price * howMany;

                    if (howMany > 0)
                    {
                        outData << "Glass Bottle        " << howMany << "           " << price << "     " << itemTotal << endl;
                        itemCount += 1;
                        total += itemTotal;

                    }
                }
                else if (itemChoice == 8)
                {
                    price = 5;
                    cout << "How many Oil Flasks do you want to purchase? ";
                    cin >> howMany;
                    cout << endl;
                    quantity += howMany;

                    itemTotal = price * howMany;

                    if (howMany > 0)
                    {
                        outData << "Oil Flasks      " << howMany << "           " << price << "     " << itemTotal << endl;
                        itemCount += 1;
                        total += itemTotal;

                    }
                }
                else if (itemChoice == 9)
                {
                    price = 50;
                    cout << "How many Potions of Healing do you want to purchase? ";
                    cin >> howMany;
                    cout << endl;
                    quantity += howMany;

                    itemTotal = price * howMany;

                    if (howMany > 0)
                    {
                        outData << "Potions of Healing      " << howMany << "           " << price << "     " << itemTotal << endl;
                        itemCount += 1;
                        total += itemTotal;

                    }
                }
                else if (itemChoice == 0)
                {
                    cout << "What you would you like to buy today? \n"
                        << "1. Gear \n"
                        << "2. Tools \n"
                        << "3. Magic Items \n"
                        << "4. Leave \n"
                        << "5. Rob the shop. \n";
                    cin >> selection;
                    cout << endl;
                    if (selection < 1 || selection > 5)
                    {
                        cout << "Ivalid choice. \n"
                            << "What you would you like to buy today? \n"
                            << "1. Gear \n"
                            << "2. Tools \n"
                            << "3. Magic Items \n"
                            << "4. Leave \n"
                            << "5. Rob the shop. \n";
                        cin >> selection;
                        cout << endl;
                    }
                }

                cout << "Gear: \n"
                    << "1. Glass Bottle \n"
                    << "2. Component Pouch \n"
                    << "3. Flask \n"
                    << "4. Healer's Kit \n"
                    << "5. Jug \n"
                    << "6. Vial \n"
                    << "7. Basic Poison \n"
                    << "8. Oil Flask \n"
                    << "9. Potion of Healing \n"
                    << "0. Back to main \n"
                    << endl;
                cin >> itemChoice;
                cout << endl;

                    if (itemChoice < 0 || itemChoice > 9)
                    {
                        cout << "Invalid item choice. Please try again. \n"
                            << "Gear: \n"
                            << "1. Glass Bottle \n"
                            << "2. Component Pouch \n"
                            << "3. Flask \n"
                            << "4. Healer's Kit \n"
                            << "5. Jug \n"
                            << "6. Vial \n"
                            << "7. Basic Poison \n"
                            << "8. Oil Flask \n"
                            << "9. Potion of Healing \n"
                            << endl;
                        cin >> itemChoice;
                    }



            }

        }

        cout << "Would you like to continue. \nEnter any key except 'y' to continue. ";
        cin >> cont;
        cout << endl;
    }



    averageItem = (double)quantity / (double)itemCount;
    averageTotal = (double)total / (double)itemCount;
    outData << "----------------------------------------------------------------------" << endl;
    outData << "Total                                        " << quantity << "                             " << total << endl;
    outData << "Average                                   " << averageItem << "                             " << averageTotal << endl;

    outData.close();
    cout << "Thank You. Come back soon";
    system ("pause");
    return 0;
}

............................................... ...........................

0 个答案:

没有答案