Cześć! Wymyśliłem, że napiszę prostą aplikację do obsługi sklepu internetowego. Ma działać na zasadzie "koszyka" na Allegro, gdzie wrzucamy i możemy dalej kontynuować zakupy. I tu postanowiłem użyć nieskończonej pętli for (;;). Czy to jest dobra decyzja? Czy może określić inną pętlę? Wydaje się być prawie dobrze, ale to "prawie" jak zwykle powoduje, że program się sypie.
#include <cstdlib>
#include <windows.h>
#include <stdio.h>
using namespace std;
int choiceType, backToMainStore, ship;
float summaryPrice, amountDVD, amountBluray, amountCD;
string country, code;
int main()
{
cout<<"Welcome to our DVD, Blu-ray and CD store! Be aware, that we add 2% tax to SUMMARY PRICE of your purchase"<<endl;
for(;;)
{
cout<<"Choose the type of items you wanna buy: "<<endl;
cout<<endl<<"DVD - choose '1'"<<endl;
cout<<"Blu-ray - choose '2'"<<endl;
cout<<"CD - choose '3'"<<endl;
cout<<"Cancel - choose '4'"<<endl;
cout<<endl<<"My choice: ";
cin>>choiceType;
switch(choiceType)
{
case 1: cout<<endl<<"DVD price is 12$. How many do you want to order: ";
cin>>amountDVD;
cout<<endl<<"Do you want to buy anything else?"<<endl;
cout<<"Yes - press '1'"<<endl;
cout<<"No - press '2'"<<endl;
cout<<"My choice: ";
cin>>backToMainStore;
case 2: cout<<endl<<"Blu-ray price is 16$. How many do you want to order: ";
cin>>amountBluray;
cout<<endl<<"Do you want to buy anything else?"<<endl;
cout<<"Yes - press '1'"<<endl;
cout<<"No - press '2'"<<endl;
cout<<"My choice: ";
cin>>backToMainStore;
break;
case 3: cout<<endl<<"CD price is 5$. How many do you want to order: ";
cin>>amountCD;
cout<<endl<<"Do you want to buy anything else?"<<endl;
cout<<"Yes - press '1'"<<endl;
cout<<"No - press '2'"<<endl;
cout<<"My choice: ";
cin>>backToMainStore;
break;
case 4: cout<<endl<<"You left the store."<<endl;
exit(0);
default: cout<<endl<<"There's no number "<<choiceType<<". Please, try again.";
Sleep(3000);
break;
}
if(backToMainStore==2)
{
cout<<endl<<"Do you want us to ship you your ordering: "<<endl;
cout<<"Yes - choose '1'"<<endl;
cout<<"No - choose '2'"<<endl;
cout<<"My choice: ";
cin>>ship;
if(ship==1)
{
cout<<endl<<"Shipping costs 10$. If your order costs more than 100$, shipping is for free :)"<<endl;
cout<<endl<<"Set your address: "<<endl;
cout<<"Your country: ";
cin>>country;
cout<<"Your code: ";
cin>>code;
cout<<endl<<"Loading..."<<endl;
Sleep(2000);
summaryPrice=(amountDVD*12+amountBluray*16+amountCD*5)*1.02;
if(summaryPrice>100)
cout<<endl<<"You've just ordered as following: "<<endl;
cout<<endl<<amountDVD<<". of DVD's, which costs "<<amountDVD*12<<"$."<<endl;
cout<<amountBluray<<". of Blu-ray's, which costs "<<amountBluray*16<<"$."<<endl;
cout<<amountCD<<". of CD's, which costs "<<amountCD*5<<"$."<<endl;
cout<<endl<<"Shipping is for free!"<<endl;
cout<<endl<<"Summary price (tax included) is: "<<summaryPrice<<"$."<<endl;
cout<<endl<<"Address of shipping is: "<<code<<", "<<country<<"."<<endl;
if(summaryPrice<=100)
cout<<endl<<"You've just ordered as following: "<<endl;
cout<<endl<<amountDVD<<". of DVD's, which costs "<<amountDVD*12<<"$."<<endl;
cout<<amountBluray<<". of Blu-ray's, which costs "<<amountBluray*16<<"$."<<endl;
cout<<amountCD<<". of CD's, which costs "<<amountCD*5<<"$."<<endl;
cout<<endl<<"Shipping costs 10$."<<endl;
cout<<endl<<"Summary price (tax and shipping included) is: "<<summaryPrice+10<<"$."<<endl;
cout<<endl<<"Address of shipping is: "<<code<<", "<<country<<"."<<endl;
exit(0);
getchar();getchar();
}
if (ship==2)
{
summaryPrice=(amountDVD*12+amountBluray*16+amountCD*5)*1.02;
cout<<endl<<"You've just ordered as following: "<<endl;
cout<<endl<<amountDVD<<". of DVD's, which costs "<<amountDVD*12<<"$."<<endl;
cout<<amountBluray<<". of Blu-ray's, which costs "<<amountBluray*16<<"$."<<endl;
cout<<amountCD<<". of CD's, which costs "<<amountCD*5<<"$."<<endl;
cout<<endl<<"Summary price (tax included) is: "<<summaryPrice<<"$."<<endl;
exit(0);
getchar();getchar();
}
}
}
system("cls");
return 0;
}
Dzięki za każdą odpowiedź! Jutro cały dzień będę pracował i dłubał tak, żeby w końcu się udało.
Miłego wieczoru :)