Hello World,
Today I want to show you my code for the Carnival Project! I will discuss each part step by step at the end.
Usually with these posts, I try to show pictures of the code primarily because of code simplicity. The program I made today is rather repetitive and therefore it is about 900 lines long. Therefore, I copied and pasted the code below rather than taking 31 pictures. Please feel free to follow along or to
Title: Carnival Transaction Program
Author: Timmy
// Example program
#include <iostream>
#include <string>
#include <exception>
int KidsTotalTickets=400;
int ClerkTotalTickets=0;
std::string kid1 = "Timmy";
std::string kid2 = "Kristen";
std::string kid3 = "Matt";
std::string kid4 = "Jenna";
std::string kid5 = "Joe";
std::string kid6 = "Mark";
std::string prize1 = "wristband";
std::string prize2 = "teddybear";
std::string prize3 = "slinky";
std::string prize4 = "rocketship";
std::string prize5 = "dinosaur";
int prize1price=10;
int prize2price=50;
int prize3price=75;
int prize4price=200;
int prize5price=300;
std::string prizeWon1 = "";
std::string prizeWon2 = "";
std::string prizeWon3 = "";
std::string prizeWon4 = "";
std::string prizeWon5 = "";
int prize1Total=0;
int prize2Total=0;
int prize3Total=0;
int prize4Total=0;
int prize5Total=0;
template<class t>
void swap(t &x,t &y)
{
t temp=x;
x=y;
y=temp;
}
void CarnivalGame()
{
std::string name;
std::string prize;
try
{
std::cout<<"Please enter the name of the child selecting a prize!"<<"\n";
getline (std::cin, name);
std::cout<<"Please enter the prize the child wishes to have"<<"\n";
getline (std::cin, prize);
try
{
if (name==kid1) //Timmy
{
if (prize == prize1) //wristband
{
std::cout<<"Clerk: That will be "<<prize1price<<" tickets please!\n";
swap(prizeWon1, prize1);
std::cout<<name<<" now has acquired "<<prizeWon1<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize1price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize1price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize1Total++;
swap(prizeWon1, prize1);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize2) //teddybear
{
std::cout<<"Clerk: That will be "<<prize2price<<" tickets please!\n";
swap(prizeWon2, prize2);
std::cout<<name<<" now has acquired "<<prizeWon2<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize2price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize2price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize2Total++;
swap(prizeWon2, prize2);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize3) //slinky
{
std::cout<<"Clerk: That will be "<<prize3price<<" tickets please!\n";
swap(prizeWon3, prize3);
std::cout<<name<<" now has acquired "<<prizeWon3<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize3price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize3price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize3Total++;
swap(prizeWon3, prize3);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize4) //rocketship
{
std::cout<<"Clerk: That will be "<<prize4price<<" tickets please!\n";
swap(prizeWon4, prize4);
std::cout<<name<<" now has acquired "<<prizeWon1<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize4price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize4price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize4Total++;
swap(prizeWon4, prize4);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else //dinosaur
{
std::cout<<"Clerk: That will be "<<prize5price<<" tickets please!\n";
swap(prizeWon5, prize5);
std::cout<<name<<" now has acquired "<<prizeWon5<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize5price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize5price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize5Total++;
swap(prizeWon5, prize5);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
}
else if (name == kid2) //Kristen
{
if (prize == prize1) //wristband
{
std::cout<<"Clerk: That will be "<<prize1price<<" tickets please!\n";
swap(prizeWon1, prize1);
std::cout<<name<<" now has acquired "<<prizeWon1<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize1price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize1price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize1Total++;
swap(prizeWon1, prize1);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize2) //teddybear
{
std::cout<<"Clerk: That will be "<<prize2price<<" tickets please!\n";
swap(prizeWon2, prize2);
std::cout<<name<<" now has acquired "<<prizeWon2<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize2price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize2price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize2Total++;
swap(prizeWon2, prize2);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize3) //slinky
{
std::cout<<"Clerk: That will be "<<prize3price<<" tickets please!\n";
swap(prizeWon3, prize3);
std::cout<<name<<" now has acquired "<<prizeWon3<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize3price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize3price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize3Total++;
swap(prizeWon3, prize3);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize4) //rocketship
{
std::cout<<"Clerk: That will be "<<prize4price<<" tickets please!\n";
swap(prizeWon4, prize4);
std::cout<<name<<" now has acquired "<<prizeWon4<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize4price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize4price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize4Total++;
swap(prizeWon4, prize4);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else //dinosaur
{
std::cout<<"Clerk: That will be "<<prize5price<<" tickets please!\n";
swap(prizeWon5, prize5);
std::cout<<name<<" now has acquired "<<prizeWon5<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize5price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize5price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize5Total++;
swap(prizeWon5, prize5);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
}
else if (name == kid3) //Matt
{
if (prize == prize1) //wristband
{
std::cout<<"Clerk: That will be "<<prize1price<<" tickets please!\n";
swap(prizeWon1, prize1);
std::cout<<name<<" now has acquired "<<prizeWon1<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize1price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize1price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize1Total++;
swap(prizeWon1, prize1);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
};
}
else if (prize == prize2) //teddybear
{
std::cout<<"Clerk: That will be "<<prize2price<<" tickets please!\n";
swap(prizeWon2, prize2);
std::cout<<name<<" now has acquired "<<prizeWon2<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize2price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize2price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize2Total++;
swap(prizeWon2, prize2);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize3) //slinky
{
std::cout<<"Clerk: That will be "<<prize3price<<" tickets please!\n";
swap(prizeWon3, prize3);
std::cout<<name<<" now has acquired "<<prizeWon3<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize3price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize3price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize3Total++;
swap(prizeWon3, prize3);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize4) //rocketship
{
std::cout<<"Clerk: That will be "<<prize4price<<" tickets please!\n";
swap(prizeWon4, prize4);
std::cout<<name<<" now has acquired "<<prizeWon4<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize4price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize4price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize4Total++;
swap(prizeWon4, prize4);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else //dinosaur
{
std::cout<<"Clerk: That will be "<<prize5price<<" tickets please!\n";
swap(prizeWon5, prize5);
std::cout<<name<<" now has acquired "<<prizeWon5<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize5price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize5price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize5Total++;
swap(prizeWon5, prize5);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
}
else if (name == kid4) //Jenna
{
if (prize == prize1) //wristband
{
std::cout<<"Clerk: That will be "<<prize1price<<" tickets please!\n";
swap(prizeWon1, prize1);
std::cout<<name<<" now has acquired "<<prizeWon1<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize1price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize1price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize1Total++;
swap(prizeWon1, prize1);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize2) //teddybear
{
std::cout<<"Clerk: That will be "<<prize2price<<" tickets please!\n";
swap(prizeWon2, prize2);
std::cout<<name<<" now has acquired "<<prizeWon2<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize2price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize2price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize2Total++;
swap(prizeWon2, prize2);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize3) //slinky
{
std::cout<<"Clerk: That will be "<<prize3price<<" tickets please!\n";
swap(prizeWon3, prize3);
std::cout<<name<<" now has acquired "<<prizeWon3<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize3price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize3price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize3Total++;
swap(prizeWon3, prize3);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize4) //rocketship
{
std::cout<<"Clerk: That will be "<<prize4price<<" tickets please!\n";
swap(prizeWon4, prize4);
std::cout<<name<<" now has acquired "<<prizeWon4<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize4price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize4price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize4Total++;
swap(prizeWon4, prize4);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else //dinosaur
{
std::cout<<"Clerk: That will be "<<prize5price<<" tickets please!\n";
swap(prizeWon5, prize5);
std::cout<<name<<" now has acquired "<<prizeWon5<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize5price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize5price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize5Total++;
swap(prizeWon5, prize5);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
}
else if (name == kid5) //Joe
{
if (prize == prize1) //wristband
{
std::cout<<"Clerk: That will be "<<prize1price<<" tickets please!\n";
swap(prizeWon1, prize1);
std::cout<<name<<" now has acquired "<<prizeWon1<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize1price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize1price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize1Total++;
swap(prizeWon1, prize1);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize2) //teddybear
{
std::cout<<"Clerk: That will be "<<prize2price<<" tickets please!\n";
swap(prizeWon2, prize2);
std::cout<<name<<" now has acquired "<<prizeWon2<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize2price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize2price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize2Total++;
swap(prizeWon2, prize2);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize3) //slinky
{
std::cout<<"Clerk: That will be "<<prize3price<<" tickets please!\n";
swap(prizeWon3, prize3);
std::cout<<name<<" now has acquired "<<prizeWon3<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize3price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize3price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize3Total++;
swap(prizeWon3, prize3);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize4) //rocketship
{
std::cout<<"Clerk: That will be "<<prize4price<<" tickets please!\n";
swap(prizeWon4, prize4);
std::cout<<name<<" now has acquired "<<prizeWon4<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize4price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize4price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize4Total++;
swap(prizeWon4, prize4);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else //dinosaur
{
std::cout<<"Clerk: That will be "<<prize5price<<" tickets please!\n";
swap(prizeWon5, prize5);
std::cout<<name<<" now has acquired "<<prizeWon5<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize5price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize5price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize5Total++;
swap(prizeWon5, prize5);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
}
else if (name == kid6) //Mark
{
if (prize == prize1) //wristband
{
std::cout<<"Clerk: That will be "<<prize1price<<" tickets please!\n";
swap(prizeWon1, prize1);
std::cout<<name<<" now has acquired "<<prizeWon1<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize1price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize1price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize1Total++;
swap(prizeWon1, prize1);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize2) //teddybear
{
std::cout<<"Clerk: That will be "<<prize2price<<" tickets please!\n";
swap(prizeWon2, prize2);
std::cout<<name<<" now has acquired "<<prizeWon2<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize2price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize2price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize2Total++;
swap(prizeWon2, prize2);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize3) //slinky
{
std::cout<<"Clerk: That will be "<<prize3price<<" tickets please!\n";
swap(prizeWon3, prize3);
std::cout<<name<<" now has acquired "<<prizeWon3<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize3price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize3price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize3Total++;
swap(prizeWon3, prize3);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize4) //rocketship
{
std::cout<<"Clerk: That will be "<<prize4price<<" tickets please!\n";
swap(prizeWon4, prize4);
std::cout<<name<<" now has acquired "<<prizeWon4<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize4price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize4price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize4Total++;
swap(prizeWon4, prize4);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
else if (prize == prize5) //dinosaur
{
std::cout<<"Clerk: That will be "<<prize5price<<" tickets please!\n";
swap(prizeWon5, prize5);
std::cout<<name<<" now has acquired "<<prizeWon5<<"\n";
ClerkTotalTickets=ClerkTotalTickets+prize5price;
std::cout<<"Clerk now has "<<ClerkTotalTickets<<" tickets!\n";
KidsTotalTickets = KidsTotalTickets-prize5price;
std::cout<<"The kids now have "<<KidsTotalTickets<<" tickets\n";
prize5Total++;
swap(prizeWon5, prize5);
if (KidsTotalTickets<=10)
{
std::cout<<"The kids do not have anymore tickets!\n";
std::cout<<"They now have: \n";
std::cout<<prize1Total<<"wristbands\n";
std::cout<<prize2Total<<"teddybears\n";
std::cout<<prize3Total<<"slinkies\n";
std::cout<<prize4Total<<"rocketships\n";
std::cout<<prize5Total<<"dinosaurs\n";
}
else
{
CarnivalGame();
}
}
}
}
catch (const std::string& ex)
{
std::cout<<"Invalid name"<<"\n";
}
}
catch (const std::exception& ex)
{
std::cout<<"Error"<<"\n";
}
}
int main()
{
CarnivalGame();
}
Okay so the program begins by defining some variables. There is an int to show the number of tickets the kids combined. There is also zero tickets that the clerk first has. Next there are 6 strings for the kids. all have different names and ID's. Next there are 5 strings for the prizes. All the prizes have a corresponding price which are int's. Finally there are 5 prizes that could be potentially won. The reason those last 5 strings are blank is because when the template, which occurs later, swaps the prize with the blank prize won, then the kid will now have that prize won.
The template simply swaps the values within each string. For example prize1 is wristbands and prizeWon1 is nothing. By swapping the values, the prizeWon1 is now the wristbands and the prize1 is now nothing.
Now moving on to the CarnivalGame class. It may seem more complicated than it really is because the code is very repetitive. Within the main class is CarnivalGame so the commands in CarnivalGame are only called once in the main. First, it starts by prompting the user to say the name of the child who is exchanging tickets for a prize. The names must correspond with the names above or the try catch block will find an error. If the name you typed in matches one of the names of the people above, the program takes you to that if statement.
Once inside that specific person's if statement, depending on what the user wrote as the prize the child wishes to have, it will take the user to another nested if statement. If the prize the user wrote matches one of the prizes above then specific code will handle each response. If a prize is not the same as a prize above the try catch block will catch the error.
Finally, within each nested if statement is the same code except for values being different. First the Clerk tells the user how much tickets are required for the prize. Then they swap the prize for the prizeWon which is stated above in how the template works. Then it tells the user that that specific child now has acquired that prize and the clerk's total tickets increased. Then it displays how many tickets the Clerk now has. Then it subtracts that amount from the Kid's ticket total. Then it displays how many tickets the kids now have. Once a prize is obtained, the counter of that pirze increases by one to show the user later how many items they have obtained. Then it swaps the items back so that that particular item gained can be won again.
It then either calls itself using recursion. If you don't know what recursion is, it is essentially calling the method within that method. If the kids still have tickets then they keep obtaining items (recursion). If they do not have enough tickets at the end. The program displays how many tickets they now have and the number of items they have received.
I hope everything makes sense.
Please message me with any particular questions about the code.
Thanks!
No comments:
Post a Comment