Atoi转换串的不同输出 - > INT

时间:2018-05-12 07:48:57

标签: c++ string parsing type-conversion

这是我的输入文件:

Plain Egg
1.45
Bacon and Egg
2.45
Muffin
0.99
French Toast
1.99
Fruit Basket
2.49
Cereal
0.69
Coffee
0.50
Tea
0.75

这是我的代码。当人们订购8件或9件物品时,我没有任何问题,但是一旦他们输入10件或更多件物品,我就会得到非常奇怪的结果,而不是我希望看到的印刷精美的支票。例如,让我们说有人订购了8件物品并输入了#34; 12345678"为了他们的订单。我明白了:

Click Here For Nice Check

但是如果用户输入他们想要的东西,让他们说出15个项目并且他们订购了#34; 123456781234567",我得到了这个:

Click here for an ugly check

即使他们订购了10件商品,我也会得到一张空白支票:/:

Click here for a blank check???

我的计划在世界上发生了什么?是否与我使用c-strings有关?我想了解我的小电脑里面发生了什么,并了解我的电脑的想法。如果你能以一种非常简单的方式向我解释这一点(我真的很新,所以我需要一些非常明确解释你喜欢的词语的定义)以及我能理解的一些方法,因为就像我说的那样,我真的很计算机科学新手。谢谢。

    // Local Restaurant: The 5,000 Fed

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

using namespace std;

struct menuItemType {
    string menuItem;
    double menuPrice;
};

void getData(menuItemType list[]);
string showMenu(menuItemType list[]);
void printCheck(string y, menuItemType list[]);

int main() {
    string y;

    menuItemType menuList[8];
    cout << "Hello and welcome to the 5,000 fed. Your local Long Beach café, "
                    "whose                 name is inspired by Jesus' miraculous manifestation "
                    "of bread to feed the 5k! His disciples helped him in passing out "
                    "the bread, and so I am here, your waiter, to help you and assist "
                    "you with your order. ";

    getData(menuList);
    y = showMenu(menuList);
    printCheck(y, menuList);

    return 0;
}

void getData(menuItemType list[]) {

    string menupricet[8];

    ifstream inFile;
    inFile.open("menudata.txt");
    for (int i = 0; i < 8; i++) {
        getline(inFile, list[i].menuItem);
        getline(inFile, menupricet[i]);
    }

    for (int i = 0; i < 8; i++) {
        list[i].menuPrice = atof(menupricet[i].c_str());
    }
}

string showMenu(menuItemType list[]) {

    int amount;
    char number;
    string reps;
    string response = "y";
    string total = "";

    cout << fixed << setprecision(2);
    cout << endl << endl;
    cout << "Here is our wonderful menu. I do recommend the french toast, "
                    "similar to the bread that Jesus ate."
             << endl
             << endl;

    for (int i = 0; i < 8; i++) {
        cout << i + 1 << ". " << setw(13) << left << list[i].menuItem << setw(8)
                 << right << list[i].menuPrice << endl;
    }
    cout << endl;

    cout << "How to order: Write the number you would like and specify how many "
                    "you want of that item by typing it in multiple times. For instance "
                    "an input of \"1135777\" means that you want two plain eggs, one "
                    "muffin, one fruit basket, and three coffees."
             << endl
             << endl;

    cout << "How many items are you ordering total?: ";
    cin >> amount;
    cout << endl;

    cout << "Please input your order, and press enter after your order: ";

    for (int i = 0; i < amount; i++) {
        cin >> number;
        total += number;
    }

    return total;
}

void printCheck(string y, menuItemType list[]) {

    int k = 0;
    int temp = 0;
    double bill = 0;
    int newone = 0;

    k = atoi(y.c_str());

    while (k != 0) {
        newone = newone * 10;
        newone = newone + k % 10;
        k = k / 10;
    }

    cout << "Thank you for dining at the 5,000 fed. Here's your check:" << endl
             << endl
             << endl;

    cout << "_________________" << endl;

    while (newone > 0) {
        temp = newone % 10;
        newone /= 10;
        bill += list[temp - 1].menuPrice;

        cout << left << setw(15) << list[temp - 1].menuItem << right << setw(5)
                 << "|" << list[temp - 1].menuPrice << endl;
    }

    cout << fixed << setprecision(2);
    cout << left << setw(15) << "Amount Due:" << right << setw(10) << "|" << bill
             << endl
             << endl;
}

编辑: 所以有人要我将输出粘贴为文本而不是图像,所以这里有三个输出:

他们订购了8个项目,例如&#34; 12345678&#34;:

_________________
Plain Egg          |1.45
Bacon and Egg      |2.45
Muffin             |0.99
French Toast       |1.99
Fruit Basket       |2.49
Cereal             |0.69
Coffee             |0.50
Tea                |0.75
Amount Due:             |11.31

他们订购了15件物品,例如&#34; 123456781234567&#34;:

_________________
French Toast       |1.99
\365\277\357\376^ޝ-        
  YW\300\365\277\357\376all\377
\200\367\277\357\376
\370\277\357\376
\227\370\277\357\376
\331\370\277\357\376%\371\277\357\376\262 
   |0.00
    Coffee             |0.50
    Muffin             |0.99
\362\277\357\376x\362\277\357\376x\362\277\357\376x\362\277\357\376Plain Egg33333    |0.00
Plain Egg          |1.45
Cereal             |0.69
\365\277\357\376^ޝ-YW\300\365\277\357\376all\377\200\367\277\357\376\370\277\357\376\227\370\277\357\376\331\370\277\357\376%\371\277\357\376\262    |0.00
Cereal             |0.69
Amount Due:             |6.31

他们订购10件物品,例如&#34; 1234567812&#34;:

_________________
Amount Due:             |0.00

1 个答案:

答案 0 :(得分:1)

代码中的问题与类型int的大小相关联。以下是解决方案,您可以将knew one声明为unsigned long int,以便最多可以保留4,294,967,295,但仍然只包含1​​0个订单。

作为解决方案,您可以原样使用string y,而不是转换为int。这样您就可以根据需要放置任意数量的订单。要遍历订单,您可以使用substr的{​​{1}}和length功能。

让我重新编写string函数,如果我可以

printCheck

}

我跳这个帮助。问候。