• Create Account

## c++ help

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Dalphin  Members

336
Like
0Likes
Like

Posted 08 July 2014 - 11:47 AM

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.

### #2EddieV223  Members

1827
Like
2Likes
Like

Posted 08 July 2014 - 11:57 AM

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

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.

Edited by EddieV223, 08 July 2014 - 12:00 PM.

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

// C++ Video tutorials

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

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

// Excellent 2d physics library Box2D

// SFML 2 book

### #3fastcall22  Moderators

9736
Like
9Likes
Like

Posted 08 July 2014 - 12:34 PM

POPULAR

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.

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;
}


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
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.

Edited by fastcall22, 08 July 2014 - 12:37 PM.

zlib: eJzVVLsSAiEQ6/1qCwoK i7PxA/2S2zMOZljYB1TO ZG7OhUtiduH9egZQCJH9 KcJyo4Wq9t0/RXkKmjx+ cgU4FIMWHhKCU+o/Nx2R LEPgQWLtnfcErbiEl0u4 0UrMghhZewgYcptoEF42 YMj+Z1kg+bVvqxhyo17h nUf+h4b2W4bR4XO01TJ7 qFNzA7jjbxyL71Avh6Tv odnFk4hnxxAf4w6496Kd OgH7/RxC

### #4Dalphin  Members

336
Like
0Likes
Like

Posted 08 July 2014 - 01:04 PM

Okay many thanks i gonna find out if i understand it

### #5TheChubu  Members

8945
Like
1Likes
Like

Posted 08 July 2014 - 05:34 PM

I'll explain.

Upvote for Seinfield.

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

Edited by TheChubu, 08 July 2014 - 06:32 PM.

"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

### #6EddieV223  Members

1827
Like
1Likes
Like

Posted 08 July 2014 - 06:05 PM

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.

Edited by EddieV223, 08 July 2014 - 06:06 PM.

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

// C++ Video tutorials

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

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

// Excellent 2d physics library Box2D

// SFML 2 book