Im trying to add a subtotal to my script but i cant think of a way , i tried adding another function to it so it would calculate the subtotal as i go but it wont happen ... .
Also i tried returnig the value of this function to another funtion
calculating what was need it and then returning to the main function to show the total but again wont happen
please help

#include <iostream> 
#include <string> 
using namespace std;
struct CodeInfo 
{ 
	string  productCode; 
	unsigned int price;          // or use double if you wish 
}; 

int main() 
{ 
	CodeInfo items[3] = { {"rb38ssw500x500", 200}, 
		{"rb38ssw600x600", 300}, 
		{"rb38ssw700x700", 400} 
	}; 
	
	do 
	{ 
	string code; 
		
		cout << "Enter product code (or ctrl-D to exit): "; 
		cin  >> code; 
		
		for (size_t i = 0; i < sizeof(items) / sizeof(CodeInfo); ++i) 
		{ 
			if (code == items[i].productCode) 
			{ 
				cout << "Price is: " << items[i].price << endl; 
				break; 
			} 
		} 
	} while (!cin.eof()); 
	
	return 0; 
}

That isn't a script at all....

That isn't a script at all....

my application...

I assume you mean you want to add the price of each item identified within the loop to a variable representing a "subtotal" that has been declared before the loop and given the value of zero before the loop (either by initialization or by assignment). If not, try to be more descriptive of your objective.

Yes that's what I want I need to have a subtotal showing everytime I type a code so the price will just add up to that subtotal

i have added a few more things but no success , i was hopping this was going to work , but no , can someone check the code and tell me what is going wrong with it please , im still learning ...

#include <iostream> 
#include <string> 
#include "stdio.h"
#include <iomanip>
#include <ctype.h>
using namespace std;
struct CodeInfo 
{  
	string  productCode; 
	double price; // or use double if you wish 
	double subtotal; 
}; 

int main() 
{ 
	CodeInfo items[3] = { {"rb38ssw500x500", 200}, 
		{"rb38ssw600x600", 300}, 
		{"rb38ssw700x700", 400} 
	}; 
	
	do 
	{
	string code; 
		double subtotal;	
		cout << "Enter product code (or ctrl-D to exit): "; 
		cin  >> code; 
	
		for (size_t i = 0; i < sizeof(items) / sizeof(CodeInfo); ++i) 
		{ 
			if (code == items[i].productCode) 
			{ 
				cout << "Price is: " << items[i].price << endl; 
			}
				else if(code != items[i].productCode)
					subtotal += items[i].productCode;
				cout << "\nSubtotal = " << setprecision(2) << subtotal << endl;
				break; 
			} 
		}
	} while (!cin.eof()); 
	
	return 0; 
}

maybe I'm crazy, but this seems to somewhat work:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <string>
#include <iomanip>
#include <ctype.h>

using namespace std;

struct CodeInfo
{
	string productCode;
	double price; // or use double if you wish
	double subtotal;
};

int main(int argc, char **argv)
{
	CodeInfo items[3] = { {"rb38ssw500x500", 200},{"rb38ssw600x600", 300},{"rb38ssw700x700", 400}};
	double subtotal;

	do {
		string code;
		size_t cnt = -1;

		cout << "Enter product code (or ctrl-D to exit): ";
		cin >> code;

		for (size_t i = 0; i < sizeof(items) / sizeof(CodeInfo); i++) {
			if (code == items[i].productCode) {
				cnt = i;
				cout << "setting\n";
			}
		}

		if (cnt != -1) {
			cout << "Price is: " << items[cnt].price << endl;
			subtotal += items[cnt].price;
		} else { 
			cout << "\nSubtotal = " <<  subtotal << endl;   //  setprecision(2) << subtotal << endl;
			break;
		}

	} while (!cin.eof());

	return 0;
}

maybe I'm crazy, but this seems to somewhat work:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <string>
#include <iomanip>
#include <ctype.h>

using namespace std;

struct CodeInfo
{
	string productCode;
	double price; // or use double if you wish
	double subtotal;
};

int main(int argc, char **argv)
{
	CodeInfo items[3] = { {"rb38ssw500x500", 200},{"rb38ssw600x600", 300},{"rb38ssw700x700", 400}};
	double subtotal;

	do {
		string code;
		size_t cnt = -1;

		cout << "Enter product code (or ctrl-D to exit): ";
		cin >> code;

		for (size_t i = 0; i < sizeof(items) / sizeof(CodeInfo); i++) {
			if (code == items[i].productCode) {
				cnt = i;
				cout << "setting\n";
			}
		}

		if (cnt != -1) {
			cout << "Price is: " << items[cnt].price << endl;
			subtotal += items[cnt].price;
		} else { 
			cout << "\nSubtotal = " <<  subtotal << endl;   //  setprecision(2) << subtotal << endl;
			break;
		}

	} while (!cin.eof());

	return 0;
}

it does work , thanks a lot ,... but cuz im still learning can u tell me what was i doing wrong and explain why was wrong ? thanks a lot !!

post #6 delete line 11. You don't want subtotal to be a variable of a class object, you want it to be a variable of the entire project.

post #6 line 24 declares a variable called subtotal to be used by the entire project, but it declares it within the loop starting on line 21 and ending on line 40, rather than declaring outside/before the loop. Declaring it inside the loop means a new variable named subtotal is declared each time through the loop, essentially overwriting the subtotal that was used during the previous iteration through the loop.

I strongly recommend you don't do this:
double subtotal;
subtotal += items[cnt].price;

I strongly recommend you do something like this:

//initialize subtotal to some meaningful value before trying to adding to it
double subtotal = 0;
subtotal += items[cnt].price;

or this:

double subtotal;
subtotal = 0; //assign a meaningful value to the variable before adding to it.
subtotal += items[cnt].price;

Some implementations of C++ compilers may initialize all numerical variables to zero by default, but I don't feel it's a wise idea to count on it. Do it yourself and be sure of the initial value of the variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.