# C++ code to find out the total amount of sales we made

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

Suppose, we are selling 4 items and the price of the i-th item is given in the array 'cost[i]'. Now we sell the items in the order given in the string 'items'. We have to find out the total amount of sales that we have made. The string 'items' contain integer numbers from 1 to 4, duplicates can be present and they can be in any order.

So, if the input is like cost = {10, 15, 10, 5}, items = "14214331", then the output will be 75.

## Steps

To solve this, we will follow these steps −

total := 0
for initialize i := 0, when i < size of items, update (increase i by 1), do:
total := total + cost[items[i] - '0' - 1]
return total

## Example

Let us see the following implementation to get better understanding

#include <bits/stdc++.h>
using namespace std;
#define N 100
int solve(int cost[], string items) {
int total = 0;
for(int i = 0; i < items.size(); i++)
total += cost[items[i] -'0' - 1];
}
int main() {
int cost[] = {10, 15, 10, 5};
string items = "14214331";
cout<< solve(cost, items);
return 0;
}

## Input

{10, 15, 10, 5}, "14214331"

## Output

75