You can take infinite user inputs if you don't know the size and exit on a certain word. Here's an example code. Notice that just after getline(cin, product, ',')
I've placed an if
statement. If user enters exit,
at that point, program quits.
I've also used vectors. Vectors are like arrays, but their size can be changed during run time, thus you can store infinite (as much as your memory) data in it.
Last part is to display the output.
This is an example way of solving the problem, you can apply any method you like to.
#include <iostream>
#include <string>
#include <vector>
std::string product;
std::string price;
std::vector<std::string> products;
std::vector<int> prices;
int main()
{
unsigned num = 0;
while (true)
{
getline(std::cin, product, ',');
if(product == "exit")
break;
getline(std::cin, price, ';');
products.push_back(product);
prices.push_back(atoi(price.c_str()));
num++;
}
for(unsigned i = 0; i < products.size(); i++)
{
std::cout << "Product: " << products.at(i) << "\n";
std::cout << "Price : " << prices.at(i) << "\n";
}
}
Input I've used:
orange juice,5;milk,7;exit,
Output produced:
Product: orange juice
Price : 5
Product: milk
Price : 7