5 Replies - 312 Views - Last Post: 07 May 2019 - 04:48 PM Rate Topic: -----

#1 BamBam89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-May 19

Having a rough time with my project for my game session.

Posted 06 May 2019 - 03:13 PM

I am new to coding and have watched alot of turtorials and bought a book on c++ and I am programming through Microsoft Visual Studio 2019. I am trying to do a store program for a game session i run, but i am having a few issues with it.
1. in visual studio every time i change something it will say "cin" and "cout" is to ambigous and throw errors everywhere.
2.My sentinel loop will not work properly.
3.When i go into the menus of the program it gets stuck in them.

Please any advice would be great. Really wanting to get this done before this weeks game.

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

using namespace std;

int main()
{
	//declare variables and file stream
	ofstream outData;
	string name;
	char cont;
	int selection;
	int itemChoice;
	int itemCount=0;
	int price;
	int quantity = 0;
	double averageItem, averageTotal;
	int howMany;
	int total = 0;
	int itemTotal;


	//open the output file.
	outData.open("WWWW-Invoice.dat");

	//Greet the customer.
	cout << "Welcome to Wild Widgets Wonderous Warehouse.\n";
	cout << "Take a look around and find something that catches your eye.\n" << endl;

	//get the name of the customer.
	cout << "First I must ask. What is your name? ";
	cin >> name;
	cout << endl;

	//Start the header of the invoice
	outData << "		Wild Widget's Wonderous Warehouse\n		Invoice of services for: " << name << endl;
	outData << "item" << setfill(" ") << left << setw(25) << "quantity" << setw(10) << "Price Per" << setw(10) << "Total" << endl;
	outData << setfill("_") << setw(45) << endl;

	//Start the selection for the character of the loop.
	cout << "Well " << name << " are you ready to place your order?\nEnter y or Y to continue. ";
	cin.get(cont);
	cout << endl;
	
	while ((cont == 'y') && (cont == 'Y'))
	{		
		cout << "What you would you like to buy today? \n"
			<< "1. Gear \n"
			<< "2. Tools \n"
			<< "3. Magic Items \n"
			<< "4. Leave \n";
		cin >> selection;
		cout << endl;
		if (selection < 1 && selection > 4)
		{
			cout << "Ivalid choice. \n"
				 <<"What you would you like to buy today? \n"
				 <<"1. Gear \n"
				 <<"2. Tools \n"
			   	 <<"3. Magic Items \n"
				 << "4. Leave \n";
			cin >> selection;
			cout << endl;
		}
		while (selection != 4)
		{
			
			if (selection == 1)
			{
				cout << "Gear: \n"
					<< "1. Glass Bottle \n"
					<< "2. Component Pouch \n"
					<< "3. Flask \n"
					<< "4. Healer's Kit \n"
					<< "5. Jug \n"
					<< "6. Vial \n"
					<< "7. Basic Poison \n"
					<< "8. Oil Flask \n"
					<< "9. Potion of Healing \n"
					<< "0. Back to main \n"
					<< endl;
				cin >> itemChoice;

				if (itemChoice < 0 || itemChoice > 9)
				{
					cout << "Invalid item choice. Please try again. \n"
						<< "Gear: \n"
						<< "1. Glass Bottle \n"
						<< "2. Component Pouch \n"
						<< "3. Flask \n"
						<< "4. Healer's Kit \n"
						<< "5. Jug \n"
						<< "6. Vial \n"
						<< "7. Basic Poison \n"
						<< "8. Oil Flask \n"
						<< "9. Potion of Healing \n"
						<< endl;
					cin >> itemChoice;
				}

				
				if (itemChoice == 1)
				{
					price = 2;
					cout << "How many Glass Bottles do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << left << setw(25) << "Glass Bottles " << setfill('_') << right <<setw(10)<< howMany <<right 
							<< setw(10) << right << price << setw(10) << right << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;
					}

				}
				else if (itemChoice == 2)
				{
					price = 25;
					cout << "How many Component Pouches do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Component Pouches		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;

					}
				}
				else if (itemChoice == 3)
				{
					price = 1;
					cout << "How many Flasks do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Flasks		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;

					}
				}
				else if (itemChoice == 4)
				{
					price = 5;
					cout << "How many Healer's Kit do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Healer's Kit		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;

					}
				}
				else if (itemChoice == 5)
				{
					price = 1;
					cout << "How many Jugs do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Jugs		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;

					}
				}
				else if (itemChoice == 6)
				{
					price = 2;
					cout << "How many Vials do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Vials		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;

					}
				}
				else if (itemChoice == 7)
				{
					price = 100;
					cout << "How many Basic Poison do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Glass Bottle		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;

					}
				}
				else if (itemChoice == 8)
				{
					price = 5;
					cout << "How many Oil Flasks do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Oil Flasks		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;

					}
				}
				else if (itemChoice == 9)
				{
					price = 50;
					cout << "How many Potions of Healing do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Potions of Healing		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;

					}
				}
				else if (itemChoice == 0)
				{
					cout << "What you would you like to buy today? \n"
						<< "1. Gear \n"
						<< "2. Tools \n"
						<< "3. Magic Items \n"
						<< "4. Leave \n"
						<< "5. Rob the shop. \n";
					cin >> selection;
					cout << endl;
					if (selection < 1 || selection > 5)
					{
						cout << "Ivalid choice. \n"
							<< "What you would you like to buy today? \n"
							<< "1. Gear \n"
							<< "2. Tools \n"
							<< "3. Magic Items \n"
							<< "4. Leave \n"
							<< "5. Rob the shop. \n";
						cin >> selection;
						cout << endl;
					}
				}
					
				cout << "Gear: \n"
					<< "1. Glass Bottle \n"
					<< "2. Component Pouch \n"
					<< "3. Flask \n"
					<< "4. Healer's Kit \n"
					<< "5. Jug \n"
					<< "6. Vial \n"
					<< "7. Basic Poison \n"
					<< "8. Oil Flask \n"
					<< "9. Potion of Healing \n"
					<< "0. Back to main \n"
					<< endl;
				cin >> itemChoice;
				cout << endl;

					if (itemChoice < 0 || itemChoice > 9)
					{
						cout << "Invalid item choice. Please try again. \n"
							<< "Gear: \n"
							<< "1. Glass Bottle \n"
							<< "2. Component Pouch \n"
							<< "3. Flask \n"
							<< "4. Healer's Kit \n"
							<< "5. Jug \n"
							<< "6. Vial \n"
							<< "7. Basic Poison \n"
							<< "8. Oil Flask \n"
							<< "9. Potion of Healing \n"
							<< endl;
						cin >> itemChoice;
					}
					

				
			}


			if (selection == 2)
			{
				cout << "Tools: \n"
					<< "1. Alchemist's Supplies \n"
					<< "2. Brewer's Supplies \n"
					<< "3. Cook's Utensils \n"
					<< "4. Back to main. \n"
					<< endl;
				cin >> itemChoice;
				cout << endl;

				if (itemChoice < 1 || itemChoice > 4)
				{
					cout<<"Invalid Choice. Please try again. \n"
						<< "Tools: \n"
						<< "1. Alchemist's Supplies \n"
						<< "2. Brewer's Supplies \n"
						<< "3. Cook's Utensils \n"
						<< "4. Back to main. \n"
						<< endl;
					cin >> itemChoice;
					cout << endl;
				}

				if (itemChoice == 1)
				{
					price = 50;
					cout << "How many Alchemist's Supplies do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Alchemist's Supplies		" << howMany << "			$" << price << "		$" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;
					}

				}
				else if (itemChoice == 2)
				{
					price = 20;
					cout << "How many Brewer's Supplies do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Brewer's Supplies		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;
					}
				}
				else if (itemChoice == 3)
				{
					price = 10;
					cout << "How many Cook's Utensils do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Cook's Utensils		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;
					}
				}
				else if (itemChoice == 4)
				{
					cout << "What you would you like to buy today? \n"
						<< "1. Gear \n"
						<< "2. Tools \n"
						<< "3. Magic Items \n"
						<< "4. Leave \n"
						<< "5. Rob the shop. \n";
					cin >> selection;
					cout << endl;
					if (selection < 1 || selection > 5)
					{
						cout << "Ivalid choice. \n"
							<< "What you would you like to buy today? \n"
							<< "1. Gear \n"
							<< "2. Tools \n"
							<< "3. Magic Items \n"
							<< "4. Leave \n"
							<< "5. Rob the shop. \n";
						cin >> selection;
						cout << endl;
					}
				}

				cout << "Tools: \n"
					<< "1. Alchemist's Supplies \n"
					<< "2. Brewer's Supplies \n"
					<< "3. Cook's Utensils \n"
					<< "4. Back to main. \n"
					<< endl;
				cin >> itemChoice;
				cout << endl;

				if (itemChoice < 1 || itemChoice > 4)
				{
					cout << "Invalid Choice. Please try again. \n"
						<< "Tools: \n"
						<< "1. Alchemist's Supplies \n"
						<< "2. Brewer's Supplies \n"
						<< "3. Cook's Utensils \n"
						<< "4. Back to main. \n"
						<< endl;
					cin >> itemChoice;
					cout << endl;
				}
				

				
			}
		
			if (selection == 3)
			{
				cout << "Magic Item: \n"
					<< "1. Common \n"
					<< "2. Uncommon \n"
					<< "3. Rare \n"
					<< "4. Back to main \n"
					<< endl;
				cin >> itemChoice;
				cout << endl;

				if (itemChoice < 1 || itemChoice > 4)
				{
					cout << "Invalid Choice. Please try again. \n"
						<< "Magic Item: \n"
						<< "1. Common \n"
						<< "2. Uncommon \n"
						<< "3. Rare \n"
						<< "4. Back to main. \n"
						<< endl;
					cin >> itemChoice;
					cout << endl;
				}
				if (itemChoice == 1)
				{
					price = 50;
					cout << "How many Common Magic Items do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Common Magic		" << howMany << "			$" << price << "		$" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;
					}

				}
				else if (itemChoice == 2)
				{
					price = 250;
					cout << "How many Uncommon Magic Items do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Uncommon Magic		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;
					}
				}
				else if (itemChoice == 3)
				{
					price = 2500;
					cout << "How many Rare Magic Items do you want to purchase? ";
					cin >> howMany;
					cout << endl;
					quantity += howMany;

					itemTotal = price * howMany;

					if (howMany > 0)
					{
						outData << "Rare Magic		" << howMany << "			" << price << "		" << itemTotal << endl;
						itemCount += 1;
						total += itemTotal;

					}
				}
				else if (itemChoice == 4)
				{
					cout << "What you would you like to buy today? \n"
						<< "1. Gear \n"
						<< "2. Tools \n"
						<< "3. Magic Items \n"
						<< "4. Leave \n"
						<< "5. Rob the shop. \n";
					cin >> selection;
					cout << endl;
					if (selection < 1 || selection > 5)
					{
						cout << "Ivalid choice. \n"
							<< "What you would you like to buy today? \n"
							<< "1. Gear \n"
							<< "2. Tools \n"
							<< "3. Magic Items \n"
							<< "4. Leave \n"
							<< "5. Rob the shop. \n";
						cin >> selection;
						cout << endl;
					}
				}

				cout << "Magic Item: \n"
					<< "1. Common \n"
					<< "2. Uncommon \n"
					<< "3. Rare \n"
					<< "4. Back to main \n"
					<< endl;
				cin >> itemChoice;
				cout << endl;

				if (itemChoice < 1 || itemChoice > 4)
				{
					cout << "Invalid Choice. Please try again. \n"
						<< "Magic Item: \n"
						<< "1. Common \n"
						<< "2. Uncommon \n"
						<< "3. Rare \n"
						<< "4. Back to main. \n"
						<< endl;
					cin >> itemChoice;
					cout << endl;
				}
				
				

				
			}
		}
		
		cout << "Would you like to continue. \nEnter any key except (N) to continue? ";
		cin >> cont;
		cout << endl;
	}



	averageItem = (double)quantity / (double)itemCount;
	averageTotal = (double)total / (double)itemCount;
	outData << "----------------------------------------------------------------------" << endl;
	outData << "Total                                        " << quantity << "                             " << total << endl;
	outData << "Average                                   " << averageItem << "                             " << averageTotal << endl;

	outData.close();
	cout << "Thank You. Come back soon";
	system ("pause");
	return 0;
}



To say as well a problem I am having with visual studio is when i do change something i have to close out of it and reopen a couple times, or my program will run with out the changes i made.

Is This A Good Question/Topic? 0
  • +

Replies To: Having a rough time with my project for my game session.

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6257
  • View blog
  • Posts: 24,025
  • Joined: 23-August 08

Re: Having a rough time with my project for my game session.

Posted 06 May 2019 - 03:21 PM

First thing, get you compiling:

outData << "item" << setfill(' ') << left << setw(25) << "quantity" << setw(10) << "Price Per" << setw(10) << "Total" << endl;
outData << setfill('_') << setw(45) << endl;


setfill takes a char argument, not a string. If you change those double quotes to single quotes, it should compile.

Second, you *really* need to look into using functions.

This post has been edited by JackOfAllTrades: 06 May 2019 - 03:21 PM

Was This Post Helpful? 0
  • +
  • -

#3 BamBam89   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-May 19

Re: Having a rough time with my project for my game session.

Posted 06 May 2019 - 03:45 PM

The book I have and the tutorials that I watched said to us double quotes, and what do you mean start using functions?

This post has been edited by Skydiver: 07 May 2019 - 03:35 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5733
  • View blog
  • Posts: 17,568
  • Joined: 25-December 09

Re: Having a rough time with my project for my game session.

Posted 06 May 2019 - 03:56 PM

Quote

The book I have and the tutorials that I watched said to us double quotes,

Okay, so your book is incorrect and so are the tutorials you happened to find.

Quote

and what do you mean start using functions?

You have over 500 lines of code in main(), much too many. Use functions to simplify things and remove some of the code duplication.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,665
  • Joined: 05-May 12

Re: Having a rough time with my project for my game session.

Posted 07 May 2019 - 03:42 PM

View PostBamBam89, on 06 May 2019 - 06:13 PM, said:

To say as well a problem I am having with visual studio is when i do change something i have to close out of it and reopen a couple times, or my program will run with out the changes i made.

This is likely because of the system("pause"); you have near the end of your main(). You are likely pressing one key to satisfy the pause call, but not pressing another key again to let Visual Studio close the console window.

With VS2017 and higher, the default setting of the IDE is now to hold the console window open for you if you simply press F5 to run (with debugger). This now works like Code::Blocks. With prior versions of Visual Studio, you needed to either press Ctrl-F5 to run (without debugger), or to use some technique to keep the console open like using that system call to run "pause".
Was This Post Helpful? 1
  • +
  • -

#6 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1859
  • View blog
  • Posts: 6,697
  • Joined: 19-February 09

Re: Having a rough time with my project for my game session.

Posted 07 May 2019 - 04:48 PM

A do..while loop can be used to reduce the code for the menus.

do
{
  cout << "What you would you like to buy today? \n"

       << "1. Gear \n"
       << "2. Tools \n"
       << "3. Magic Items \n"
       << "4. Leave \n";

  cin  >> selection;
  cout << endl;

  if (selection < 1 || selection > 4)
    cout << "Invalid choice. \n" << endl;

} while( selection < 1 || selection > 4 ) ;



You could put the loop in a programmer-defined function, that also returned the chosen value.

 selection = mainmenu();


The program can be compartmentalized by using functions that you create, which can also simplify the logic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1