Loop yes or no

How can i put a loop?

"Want to buy something else? Yes/No"
If Yes: start again from cout<<"Producto:";
if no: print a bill.

Thank you for your help.

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
35
36
37
38
39
40
41
42
43
44
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

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


	while (infile >> prod >> price)
	{

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

                  	}
    cout<<"El precio incluye iva (S/N)";
    cin>>iva;
    if (iva == "S"){
        cout<<"Cantidad: ";
         	cin>>can;
         	cout<<"Precio: "<<can*price;
         	} else if (iva == "N")
         	{ cout<<"Cantidad: ";
         	cin>>can;
         	double cobro =((((price*15)/100)+price)*can);
         	cout<<"Precio con iva incluido:" <<cobro<<endl;
         	}else{ cout<<"Opcion no valida"<<endl;}


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

	infile.close();



}
Last edited on
closed account (j3Rz8vqX)
Find the start and end of the certain procedure you wish to encapsulate(loop).

Consider a starting point and an ending point.
Starting point:
cout<<"Producto: ";
Ending point:
Your decision...
Ensure to prompt the user for an exit option.

An example:
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
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    string str;
    vector<string> datas;

    do{
    //<---Find the start point of your loop.
        cout<<"Enter a string: ";
        getline(cin, str);
        datas.push_back(str);
        while(cout<<"\nRepeat?(yes or no): "&&getline(cin,str)&&(str!="yes" && str!="no"))//Prompt/validate for exit choice.
            cout<<"Invalid, answer must be (yes or no)\n";//validate: error prompt
        cout<<'\n';
    //<---Find the end of your loop.
    }while(str=="yes");

    cout<<"The strings recorded were: \n";
    for(int i=0;i<datas.size();++i)
        cout<<datas[i]<<'\n';
    cout<<'\n';

    cout<<"Press <enter> to exit console: ";
    cin.get();
    return 0;
}
Enter a string: Hello World

Repeat?(yes or no): intentional
Invalid, answer must be (yes or no)

Repeat?(yes or no): yes

Enter a string: This is a string

Repeat?(yes or no): yes

Enter a string: Good Bye

Repeat?(yes or no): no

The strings recorded were:
Hello World
This is a string
Good Bye

Press <enter> to exit console:

I do not provide direct solutions to specifics, but rather examples that may help lead you to your goal.

The above would prompt a string then validate for continuation and repeat the validation loop if the string entered was not "yes" or "no". It would, then, repeat the procedures if the string was "yes".

Any data in str could end to procedure loop, other than "yes", but the validation loop would prohibit any data other than "yes" and "no".

Have a good day.
Topic archived. No new replies allowed.