Want to buy something else? Yes/No

I need a loop for this, but i don't know how to put it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string prod;
	double price, pos, can;
	string name;
	ifstream infile("productos1.txt");
	cout<<"Producto: ";
	cin>>name;


	while (infile >> prod >> price)
	{

    if (name == prod)
          cout <<"Precio: "<<price <<endl;

          //i think here's where i've to put it.

         	}
         	cout<<"Cantidad: ";
         	cin>>can;
         	cout<<"Precio: "<<can*price;


	infile.close();



}
Last edited on
A little more detail would be nice exactly what is to be looped. Does your current loop work, or is that the one you are having issues with?

Probably what I would do if you want to loop through the input of a file...

1
2
3
4
5
6
7

do
{
infile >> prod >> price;
//do your loop stuff now
} while (!infile.eof());


This way you loop through the program and stop when it hits the end of the file.
Last edited on
To be looped:
1
2
3
4
5
6
7
8
9
10
11
12
13
cout<<"Producto: ";
	cin>>name;

	while (infile >> prod >> price)
	{

    if (name == prod)
          cout <<"Precio: "<<price <<endl;
         	}
         	cout<<"Cantidad: ";
         	cin>>can;
         	cout<<"Precio: "<<can*price;


So, that's why i don't know where i've to put it.
Can you give a detailed explanation of what you want it to do (give us two iterations of each loop).

If I'm able to derive from your code, you want the user to keep inputting until a valid name is entered. I would do this like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
name = "EMPTY";

while (name != prod)
{
    cout << "Producto: ";
    cin >> name;

    //When this condition is true, it'll run, and the loop will be broken
    if (name == prod)
    {
         cout << "Precio: " << price << endl;

         cout << "Cantidad: ";
         cin >> can;
         cout << "Precio: " << can * price;
    }

    
}
Okay, so first:
I put the name of the product, so if name == prod (which is on the txt)
should print price and quantity, and the final price.

In the end, there should be the option for the client to buy an other product. And go to the name of the product.

(I'm sorry if my english is very bad)
Okay, in that case then, if you want to enter another product name we can do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
name = "EMPTY";

while (name != prod)
{
	cout << "Producto: "; 
	cin >> name;
	
	if (name == prod)
	{
		bool again = false;
		char letter;
		
		cout << "Precio: " << price << endl;
		
		cout << "Cantidad: ";
		cin >> can;
		
		cout << "Precio: " << can * price << endl;
		
		cout << "Would you like to buy anything else? [y/n]: ";
		cin >> letter;
		
		if (letter == 'y' || letter = 'Y')
		{
			name = "EMPTY";
		}
	}
	else
	{
		cout << "Item not found, please try again...\n";
	}
}
Topic archived. No new replies allowed.