c++ help

Started by
4 comments, last by EddieV223 9 years, 9 months ago

Hello everyone,

I'm learning c++ and thats why i'm trying to make some little programs messing with c++.

I'm trying to make a simple sort of text-based cash register system and i'm trying to input

a feature like this: you have a main function. In that main function are the steps the computer needs to act

like a cash register, like this:


.........
if(action == "sell"){
    cout << "How many do you want to sell" << endl;
    cin >> sellAmount;
    priceTotal = sellAmount * priceProduct;
    cout << "The price is ";
    cout << priceTotal;
 

Now i want to make a file that i can import into the program. In the file are different products with all an explanation of the price (priceProduct in the above code) quantity of the product and that kind of things, so that i only have to write the universal code above once and it can be used for all the products if.

I think i have to do something with classes, but i'm not sure. I even don't know if this is possible. If it is: could you explain how to do it to.

Thanks

BTW: I find it hard to explain, so if you don't understand it feel free to ask.

Advertisement

Of course it is possible though text processing can be difficult. The simplest way to do this is a text in stream. These classes have very similar syntax to your console streams.

http://en.cppreference.com/w/cpp/io/basic_ifstream

Then you need to come up with a format.

Here is an example

_________

Beer 12 pack

8.99

Cookies

3.99

Beef Jerky

2.49

_________

As you can see there is a repeating format here. Every data field has it's own line ( the delimiter ). Each product has its string name, and its price.

When you parse this you can create a vector of products ( think Products class or struct ) to hold your data.

Bonus points, give each product a uniqueID ( not a string ) that can be used for identification within your app.

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

Now i want to make a file that i can import into the program. In the file are different products with all an explanation of the price (priceProduct in the above code) quantity of the product and that kind of things, so that i only have to write the universal code above once and it can be used for all the products if.

I think i have to do something with classes, but i'm not sure. I even don't know if this is possible. If it is: could you explain how to do it to.


This indeed possible, and it can be made easier with structured data. I'll explain.


I. Organizing your data

First we'd like to know what kind of stuff we'd like to know about our product. You mentioned you'd like to have the name, price, and quantity of the product. So, we'll start there.

struct Product {
	string name;
	double price;
	int qty;
};
Here, we declare a structure called "Product" as having three members: Name, price, and quantity. Any object of type "Product" will have these three properties. So how do we create and use a "Product"? Well, like any another variable, really:

Product milk;
milk.name = "milk";
milk.price = 2.75;
milk.qty = 8;

// or
// Product milk = { "milk", 2.75, 8 };

cout << "Product Name:\t" << milk.name << endl
	<< "Product Price:\t" << milk.price << endl
	<< "Product Quantity:\t" << milk.qty << endl;
Now we need a way to store all of the products together.


II. Managing the data generically

Here we can use arrays. We could use std::vector for a dynamically sized array, but that is outside of the scope of this post. We'll use a static array instead:

const int MAX_PRODUCTS = 100;
Product products[MAX_PRODUCTS];
int numProducts = 0;
Now, we can add and manage the products programmatically:
// Populate the array of products
numProducts = 0;
products[numProducts++] = Product { "Milk", 2.75, 8 };
products[numProducts++] = Product { "Cookies", 4.60, 4 };
products[numProducts++] = Product { "Bread", 3.50, 12 };
products[numProducts++] = Product { "Eggs", 4.25, 16 };

// Display the array's contents.
for ( int idx = 0; idx < numProducts; ++idx ) {
	cout << "Product Name:\t" << products[idx].name << endl
		<< "Product Price:\t" << products[idx].price << endl
		<< "Product Quantity:\t" << products[idx].qty << endl
		<< endl;
}
With the array, we will no longer be able to refer to products by variable name (such as "milk" in the previous example). We'll either need to use pointers or indexes. Here's a sample using an index to find a product in the array:
string queryName;
cout << "Enter product name: ";
getline(cin, queryName);

int idx = 0;
for ( ; idx < numProducts; ++idx ) {
	if ( products[idx].name == queryName )
		break;
}
if ( idx < numProducts ) {
	// do something with products[idx]
} else {
	cout << "Unable to find \"" << queryName << "\"" << endl;
}

III. Structuring your file

Now, let's get to dynamically loading the products. Let's first create a file and create it with tab-separated values:

Milk	2.75	8
Cookies	4.60	4
Bread	3.50	12
Eggs	4.25	16
Next, let's place this file somewhere where your program can see it. Check your project settings for the working directory of your program. Place the file there.

// include fstream, sstream
ifstream fs("products.txt");
if ( !fs ) {
	cout << "Unable to open file";
	return;
}

string line;
numProducts = 0;
while ( getline(fs,line) ) { // Read one line at a time
	istringstream is(line); // Temporary stream for the line

	Product temp;
	if ( getline(is,temp.name,'\t') && is >> temp.price >> temp.qty ) {
		products[numProducts++] = temp;
	} else
		; // Error reading the line
}

So there you go. Hopefully it should be clear how this should work.

Okay many thanks i gonna find out if i understand it :D


I'll explain.

Upvote for Seinfield.

EDIT: Totally unrelated comment I know, give me a break.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator


I'll explain.
Upvote for Seinfield.

Sometimes it's best to lead the student without simply giving the answers.

And no I didn't down vote you.

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20

This topic is closed to new replies.

Advertisement