本文介绍了用户将在他们想要的时间输入产品和数量,并且当他们不希望程序显示总账单时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在制作酒店管理系统的c ++程序。我希望用户输入产品编号和数量。写完后,程序会询问用户是否想要订购任何其他产品,如果他写的是程序应该再次要求输入产品编号和数量。它应该在用户需要时继续。 当用户写'n'而不是'y' 时,程序应该显示总金额。它应该显示我在订单中的订单和数量以及总卢比的哪个产品,最后它应该显示净额。 我尝试了什么: i am making a c++ program of hotel management system.I want that the user will enter product number and quantity.After writing, the program will ask if the user wants to order any other product if he write 'y'the program should again ask to enter product number and quantity.It should continue while user wants.When the user writes 'n'instead of 'y'the program should show the total amount.Also it should show which product i have order and quantity and total rupess for each of them in bill and at last it should show the net amount.What I have tried:#include<iostream.h> #include<conio.h>void main(){clrscr();start:cout<<"--------------------------------HOTEL RED CHILLIES------------------------------" ;cout<<" M.G.ROAD RAIPUR - 492001";cout<<"\n\t\t\t\tPHONE: 0771-4024196";cout<<"\n--------------------------------------------------------------------------------";cout<<"*CATEGORIES*\t\tPrice\t\t *CATEGORIES*\t\tPrice" ;cout<<"\n\nChinese Noodles\t\t\t Pizza";cout<<"\n\n1.Veg Chowmein\t\t 100 \t\t 7.Red Green Pizza\t 160";cout<<"\n2.Chili Garlic Noodles\t 100\t\t 8.Deluxe Margherita\t 180";cout<<"\n3.American Chop Suey\t 110\t\t 9.Tomato Deluxe\t 180";cout<<"\n------------------------------\t\t -----------------------------";cout<<"\n\nPasta\t\t\t\t\t Soup";cout<<"\n\n4.Pasta Red Sauce\t 160\t\t 10.Hot and Sour\t 70";cout<<"\n5.Pasta Mexican\t\t 160\t\t 11.Tomato Soup\t\t 70";cout<<"\n6.Pasta White Sauce\t 170\t\t 12.Manchow Soup\t 70";cout<<"\n-------------------------------\t\t -----------------------------";int num,qt,i=0,t,sum;char pro;do{cout<<"\nEnter the product number(1-12)- ";cin>>num;cout<<"\nEnter the quantity- ";cin>>qt;switch(num){case 1: t=100*qt;case 2: t=100*qt;case 3: t=100*qt;case 4: t=100*qt;case 5: t=100*qt;case 6: t=100*qt;case 7: t=100*qt;case 8: t=100*qt;case 9: t=100*qt;case 10: t=100*qt;case 11: t=100*qt;case 12: t=100*qt;}cout<<"\nDo you want to order any other product(y/n) ";cin>>pro;if(pro=='y'){clrscr();goto start;}}while(pro=='y');do{for(t=1; ;t++)sum=t;}while(pro=='y') ;if(pro=='n')cout<<"total is"<<sum; 推荐答案 不要使用转到,你真的不需要它。 不要使用这么古老的编译器( conio.h 已经很久以前了。) 使用结构化数据。 使用 STL 容器。 A C ++ 98 实施草图 Don't use goto, you don't really need it.Don't use such an ancient compiler (conio.h has gone long time ago).Do use structured data.Do use STL containers.A C++98 implementation sketch#include <iostream>#include <vector>using namespace std;struct Product{ string desc; int price; Product(string desc, int price):desc(desc), price(price){}};void show_menu(const vector<Product> & vp);int main(){ vector <Product> vp; vp.push_back(Product("Veg Chowmein", 100)); vp.push_back(Product("Chili Garlic Noodles", 110)); // add more products... int sum = 0; while (true) { show_menu(vp); int index, qty; cin >> index; if ( index == -1) break; --index; if ( index >= 0 && index < static_cast<int>(vp.size()) ) { cout << "please enter quantity" << endl; cin >> qty; sum += (qty * vp[index].price); } } cout << "the sum is " << sum << endl;}void show_menu( const vector<Product> & vp){ cout << "please choose product (-1 to exit)" << endl; for (size_t n=0; n<vp.size(); ++n) { const Product & p = vp[n]; cout << (n+1) << " " << p.desc << " " << p.price << endl; }} [update] 使用数组而不是 std :: vector [update]Using an array instead of std::vector#include <iostream>using namespace std;struct Product{ string desc; int price; Product(string desc, int price):desc(desc), price(price){}};void show_menu( Product ap[], int ITEMS);int main(){ Product ap[] = { Product("Veg Chowmein", 100), Product("Chili Garlic Noodles", 110) }; // add more products.. const int ITEMS = sizeof(ap)/sizeof(ap[0]); int sum = 0; while (true) { show_menu(ap, ITEMS); int index, qty; cin >> index; if ( index == -1) break; --index; if ( index >= 0 && index <ITEMS ) { cout << "please enter quantity" << endl; cin >> qty; sum += (qty * ap[index].price); } } cout << "the sum is " << sum << endl;}void show_menu( Product ap[], int ITEMS){ cout << "please choose product (-1 to exit)" << endl; for (int n=0; n<ITEMS; ++n) { const Product & p = ap[n]; cout << (n+1) << " " << p.desc << " " << p.price << endl; }} [/ update] cin>>qt;switch(num){case 1: t=100*qt;case 2: t=100*qt;case 3: t=100*qt;case 4: t=100*qt;case 5: t=100*qt;case 6: t=100*qt;case 7: t=100*qt;case 8: t=100*qt;case 9: t=100*qt;case 10: t=100*qt;case 11: t=100*qt;case 12: t=100*qt;} 这个开关块有什么意义,因为每个案例都计算相同的值?由于你没有 break 语句,你只需要重复相同的计算,最多12次。What is the point of this switch block, since every case calculates the same value? And since you have no break statements you just repeat the same calculation up to 12 times. 你需要学习在开关语句中中断。 ----- 学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。 You need to learn the usage of break in a switch statement.-----Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.while(pro=='y');do{ for(t=1; ;t++)sum=t;}while(pro=='y') ;if(pro=='n') cout<<"total is"<<sum; 专业程序员的编辑器具有此功能以及其他功能,例如括号匹配和语法突出显示。 Notepad ++ Home [ ^ ] ultraedit [ ^ ] ----- 有一个工具可以让你看看你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。 当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器。 使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。 调试器 - 维基百科,免费的百科全书 [ ^ ] 掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ] 使用Visual Studio 2010进行基本调试 - YouTube [ ^ ] 调试器在这里向您展示您的代码正在做什么,您的任务是与什么进行比较应该这样做。 调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.Notepad++ Home[^]ultraedit[^]-----There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.When you don't understand what your code is doing or why it does what it does, the answer is debugger.Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.Debugger - Wikipedia, the free encyclopedia[^]Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]Basic Debugging with Visual Studio 2010 - YouTube[^]The debugger is here to show you what your code is doing and your task is to compare with what it should do.There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug. 这篇关于用户将在他们想要的时间输入产品和数量,并且当他们不希望程序显示总账单时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-03 12:15