Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Learning Aids/Tools 2 years ago
Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
Turn Your Knowledge into Earnings.
I am trying to learn how to use classes and I figured I'd create some sort of supermarket system to aid me with learning. After I have saved all the values from my text file into the temp variables, how do I then use them to create an object? I assume I want one object per item you can "buy"?
If you have any other tips on how to improve my code, please mention them as I just started with C++ a few days ago.
My text file looks like:
42 68 Apples 35 1 Oranges 70 25 Bananas
And my code is below:
// Classes.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include #include class Products { private: int price; int ID; int quantity; public: void setPrice(int newPrice) { price = newPrice; } void setID(int newID) { ID = newID; } void setQuantity(int newQuantity) { quantity = newQuantity; } int getPrice() { return price; } int getID() { return ID; } int getQuantity() { return quantity; } }; int main() { std::string line; std::string input; std::string temp; std::string temp2; std::string temp3; int counter = 0; while (input != "e" && input != "r") { std::cout << "Do you want to (r)ead the inventory or (e)dit it? (input 'r' or 'e'): "; getline(std::cin, input); } if (input == "r") { std::ifstream pFile ("products.txt"); if (pFile.is_open()) { while (getline(pFile, line)) { std::istringstream iss(line); iss >> temp >> temp2 >> temp3; counter++; } } } return 0; }
Looking at the text file that you are using as an example and trying to populate a set of classes with the information I'd do something like this:
Text File
Looking at the text file you have an int followed by space then another int followed by another space finally followed by a varying size of char[]. We can use this information to create your class or struct and this is how I would create the class based off of the content from the file that is being read in or parsed.
int
space
varying size of char[]
class
struct
Produce.h
#ifndef PRODUCE_H #define PRODUCE_H #include class Produce { private: unsigned price_; // Normally would use float but will use unsigned for simplicity unsigned quantity_; std::string name_; // You could use the int as an id as you were using public: Produce() {} // Default Constructor ~Produce() {} // Default Destructor // User Constructor Produce(const std::string& name, const unsigned& price, const unsigned& qty ) : name_(name), price_(price), quantity_( qty ) {} // Copy By Const Reference Constructor Produce(const Produce& other) { name_ = other.name_; price_ = other.price_; quantity_ = other.quantity_; } // Assignment Operator Produce& operator=(const Produce& other) { name_ = other.name_; price_ = other.price_; quantity_ = other.quantity_; return *this; } // Setters & Getters void setOrChangePrice(const unsigned& priceChange) { price_ = priceChange; } unsigned getPrice() const { return price_; } void setName(const std::string& name) { // Already Has A Name? Return! if (!name_.empty()) return; } std::string getName() const { return name_; } void setOrChangeQuantity(const unsigned& qty) { quantity_ = qty REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.