Problem with array
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Problem with array

  1. #1
    Join Date
    Mar 2017
    Posts
    14

    Problem with array

    Hey! I have created two shops one shop has 3 products and the other one has 5 products so I created an array for that. The thing is when I select my shop with only 3 products the last two slots get filled with garbage data because I can't figure out how to stop the array. Also when I enter a shop I buy something it get me back to the opening menu but doesn't give me an option to exit the program and I can't figure out how to do that. I am hoping what I am writing is making sense.



    Main.cpp:
    Code:
    #include <iostream>
    #include "Shop.h"
    #include <iomanip>
    
    
    /* run this program using the console pauser or add your own getch, system("pause") or input loop */
    int main() {
    
    
    
    	Product fish;
    	fish.setName("Fish");
    	fish.setPrice(5);
    	fish.setQuantity(20);
    
    	Product potatoes;
    	potatoes.setName("Potatoes");
    	potatoes.setPrice(2);
    	potatoes.setQuantity(45);
    
    	Product mayo;
    	mayo.setName("Mayo");
    	mayo.setPrice(.5);
    	mayo.setQuantity(100);
    
    	Shop fishAndChips;
    	fishAndChips.setName("Fish&Chips");
    	fishAndChips.setting(fish, 0);
    	fishAndChips.setting(potatoes, 1);
    	fishAndChips.setting(mayo, 2);
    
    	Product whale;
    	whale.setName("Whale");
    	whale.setPrice(100);
    	whale.setQuantity(10);
    
    	Product blubber;
    	blubber.setName("Blubber");
    	blubber.setPrice(50);
    	blubber.setQuantity(10);
    
    	Product jolly;
    	jolly.setName("Jolly");
    	jolly.setPrice(3);
    	jolly.setQuantity(1000);
    
    	Product slupp;
    	slupp.setName("Slupp");
    	slupp.setPrice(2);
    	slupp.setQuantity(5702);
    
    	Product aqua;
    	aqua.setName("Aqua");
    	aqua.setPrice(.25);
    	aqua.setQuantity(999);
    
    	Shop whaleShop;
    	whaleShop.setName("Whale");
    	whaleShop.setting(whale, 0);
    	whaleShop.setting(blubber, 1);
    	whaleShop.setting(jolly, 2);
    	whaleShop.setting(slupp, 3);
    	whaleShop.setting(aqua, 4);
    
    	bool programEnds = false;
    	while (!programEnds) {
    
    		bool validInput = false;
    		int choice;
    		while (!validInput) {
    
    			cout << "Please Select a Shop!" << endl;
    			cout << "1. Fish and Chips!" << endl;
    			cout << "2. Whale!" << endl;
    			cin >> choice;
    
    			if (choice == 1) {
    				fishAndChips.display();
    				validInput = true;
    			}
    			else if (choice == 2) {
    				whaleShop.display();
    				validInput = true;
    			}
    			else {
    				cout << "Invalid choice !" << endl;
    			}
    		}
    
    		validInput = false;
    
    		int secondChoice;
    		int amount;
    		cout << "Select what you want to buy" << endl;
    		cin >> secondChoice;
    		cout << "How many do you want to buy" << endl;
    		cin >> amount;
    
    		float cost;
    		if (choice == 1) {
    			cost = fishAndChips.selling(secondChoice - 1, amount);
    			if (cost < 0) {
    				cout << "There is no sufficient amount of product" << endl;
    			}
    			else {
    				cout << "Total cost is: " << cost << endl;
    			}
    		}
    		else if (choice == 2) {
    			cost = whaleShop.selling(secondChoice - 1, amount);
    			if (cost < 0) {
    				cout << "There is no sufficient amount of product" << endl;
    			}
    			else {
    				cout << "Total cost is: " << cost << endl;
    			}
    		}
    	}
    
    	return 0;
    }
    Product.cpp:
    Code:
    #include "Product.h"
    
    string Product::getName() {
    	return name;
    }
    
    void Product::setName(string newName) {
    	name = newName;
    }
    
    float Product::getPrice() {
    	return price;
    }
    
    void Product::setPrice(float newPrice) {
    	price = newPrice;
    }
    
    int Product::getQuantity() {
    	return quantity;
    }
    
    void Product::setQuantity(int newQuantity) {
    	quantity = newQuantity;
    }
    
    float Product::Calculate(int amount) {
    	float result = amount * price;
    	return result;
    }
    Product.h:
    Code:
    #pragma once
    #include<iostream>
    
    using namespace std;
    
    class Product {
    private:
    	string name;
    	float price;
    	int quantity;
    public:
    	string getName();
    	void setName(string newName);
    	float getPrice();
    	void setPrice(float newPrice);
    	int getQuantity();
    	void setQuantity(int newQuantity);
    	float Calculate(int amount);
    };
    shop.cpp
    Code:
    #include "Shop.h"
    #include <string>
    
    void Shop::setName(string newName) {
    	name = newName;
    }
    
    void Shop::display() {
    	for (int i = 0; i < 5; i++) {
    		if (products[i].getQuantity() != 0) {
    			cout << i + 1 << "." << products[i].getName() << " " << products[i].getPrice() << " dollars " << products[i].getQuantity() << " quantity" << endl;
    		}
    	}
    }
    
    void Shop::setting(Product newProduct, int index) {
    	products[index] = newProduct;
    }
    
    float Shop::selling(int indexOfSoldProduct, int amount) {
    	int avaliableProductCount = products[indexOfSoldProduct].getQuantity();
    	if (avaliableProductCount >= amount) {
    		products[indexOfSoldProduct].setQuantity(avaliableProductCount - amount);
    		return products[indexOfSoldProduct].Calculate(amount);
    	}
    	return -1;
    }
    
    void Shop::restocking() {
    
    }
    shop.h:
    Code:
    #pragma once
    #include <iostream>
    #include "Product.h"
    
    using namespace std;
    
    class Shop {
    private:
    	string name;
    	Product products[5];
    public:
    
    	void setName(string newName);
    	void display();
    	void setting(Product newProduct, int index);
    	float selling(int indexOfSoldProduct, int amount);
    	void restocking();
    };

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,876

    Re: Problem with array

    Is this a homework exercise?

    The thing is when I select my shop with only 3 products the last two slots get filled with garbage data because I can't figure out how to stop the array.
    Assuming

    Code:
    class Shop {
    private:
    	string name;
    	Product products[5];
    then one way is to have another member variable that is set to indicate how many elements of products are used (updated in .setting() if index greater than current value). Then instead of using 5 in the display loop use this variable.

    Note that using an array in cases like this is not good programming practice and consideration should be given to using a more suitable container - such as a vector.

    but doesn't give me an option to exit the program
    Your menu should have an option to quit. eg
    Code:
    cout << "Please Select a Shop!" << endl;
    cout << "1. Fish and Chips!" << endl;
    cout << "2. Whale!" << endl;
    cout << "3. Exit" << endl;
    cin >> choice;
    and then code so that if 3 is entered the outer while loop terminates.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.5.0)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


On-Demand Webinars (sponsored)