-4

Im trying to get and compare different datas. I have to bring the name of the product and their prices (Orange juice, 5) but my problem is that i dont know how to do it for more than 1 product. im using getline to bring the data but i dont know how many products they will introduce and idk how to stop the loop.

(orange juice,5; milk,7;)

while (?????????) {

    getline(cin, product, ',');
    getline(cin, price, ';');
    products[num] = product;
    proces[num] = atoi(proce.c_str());

    num++;


}
  • Use `std::vector` if you don't know the size or even some other container better suitable for your task – Killzone Kid May 06 '18 at 08:20
  • if you don't know how many product will user insert, you have to ask it so me how to the user. you have different options: ask the number before asking for each product, or agree with use on something that means "I'm over, no more products to inserti" (e.g. "enter X for ending"), or something else an possibly more user friendly. In every case, using std:vector instead of a `Product` plain array (`Product[]`) will simplify your life – Gian Paolo May 06 '18 at 08:29
  • Welcome to Stack Overflow. I'm sorry people downvoted your question so quickly. You might improve the responses by including a [mcve]. – JHBonarius May 06 '18 at 08:51

3 Answers3

1

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
  • `using namespace std;` <-- no... just no. https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – JHBonarius May 06 '18 at 08:55
  • He's probably doing a homework, there's no harm using it in small projects or in school. In production namespaces may conflict and cause bad results, thus should be avoided. I see no issue it being used here. – Tuğberk Kaan Duman May 06 '18 at 08:56
  • I disagree. Homework and such are there to learn people how to do something correct. Teaching them bad habits will result in future bad programmers (which there are already lots of). – JHBonarius May 06 '18 at 08:59
  • Fine, I'll edit answer to `std::` version. Hang in there kitty. Edit is done, be a good programmer David. :) – Tuğberk Kaan Duman May 06 '18 at 09:00
0

try

bool check=false;
if(!getline(cin, price, ';'))check=true;
...
if(check)break;

and you should use std::vector instead of array here.

con ko
  • 1,368
  • 5
  • 18
0

I would just look ahead at the stdin buffer to see if the line was termined (by an enter = \n.)

#include <iostream>
#include <string>
#include <vector>
int main()
{
    std::string product;
    std::string price;
    std::vector<std::pair<std::string, int>> product_price;

    while (std::cin.peek() != '\n')
    {
        std::getline(std::cin, product, ',');
        std::getline(std::cin, price, ';');
        product_price.push_back(make_pair(product,std::stoi(price)));
    }

    for (auto& item : product_price)
    {
        std::cout
            << "Product: " << item.first << "\n"
            << "Price  : " << item.second << "\n";
    }

    return 0;
}
JHBonarius
  • 10,824
  • 3
  • 22
  • 41