#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
const int N = 20;
void instructions()
{cout << "* To quit or print a bill, enter 0 when asked to Enter an item number." << endl;
cout << endl;
cout << "* To cancel orders, enter the item number of the item to cancel" << endl;
cout << " and then enter the negative of the number of orders to cancel." << endl;
cout << " (e.g., -3 to cancel 3 orders of the selected item)." << endl;
cout << endl;
cout << "--- MENU --------------------" << endl;
}
int main(int argc, char *argv[])
{
ifstream infile; // init input file
string foods; // food name
float costs; // food cost
string food[N]; // food array
float cost[N]; // cost array
int cnt; // counter
char tab = '\t';
cout << setprecision(2) << fixed; // shows decimal two places
// below if file argument isn't found return error message
if(argc != 2)
{
cerr << "File not found." << endl;
return 1;
}
infile.open(argv[1]); // opens the user inputed file
cnt = 0;// sets up counter for while loop
infile >> foods >> costs; // initalizes the while loop
instructions();
// While loop reads in information from input file and uses a count.
while(infile && cnt < N)
{
food[cnt]= foods;
cost[cnt] = costs;
cout << cnt << ": " << food[cnt] << tab << cost[cnt] << endl;
++cnt;
infile >> foods >> costs;
}
cout << "Enter item number (0 to print bill): ";
cout << endl << cnt;
return 0;
}
对象:程序读入菜单文件。食物的名字进入阵列,成本进入阵列。我遇到的问题是,当文字中有空格时,它会抛出所有的东西。我知道我需要使用getline,但我不确定如何将getline分解为名称/成本。有人可以帮助或显示某种类型的示例。有没有办法打破getline?
Hamburger 1.89
Cheeseburger 2.29
French Fries 1.59
Onion Rings 1.59
Soda 1.29
Iced Tea 1.29
什么是菜单文件是什么样子?物品和价格的名称分开了什么? – harmic
汉堡\t 1.89 汉堡\t 2.29 炸薯条\t 1.59 洋葱圈\t 1.59 纯碱\t \t 1.29 冰茶\t 1.29 –
那不是格式化的汉堡包1.89是它自己的行,以便每个项目的价格有自己的行 –