How to make a loop until user says to break

Hi there,

I'm not very good in programming. All I know is what I took from forums...

I'm making a program for a MMO I play. It calculates the cash and resources needed to build certain 'modules'. I'm having trouble to do something very simple : make a loop until the user asks to break the loop.
Here is what the interface looks like :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
What module do you want to install? (Examples: 'Avenger' or 'VLLT')
dd // I input this

DD. COST each : 60,000 cr.

How many do you want? (Modules left:15)
(NOTE : You may enter a negative number if you made a mistake)
3 // I input this

Current Total Module Price : 180000 cr
+6 droids
Do you wish to add (an) other module(s)? (if so, enter 'y', if not, anything else)
y // I input this
What module do you want to install? (Examples: 'Avenger' or 'VLLT')
// Doesn't let me input anything, just prints the following directly

Current Total Module Price : 180000 cr
+6 droids
Do you wish to add (an) other module(s)? (if so, enter 'y', if not, anything else)


The bigger code box below is a part of the code I made. What I deleted is uninteresting, it's just the same thing again : other resources, other 'modules'...

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;


int main()
{
	struct resources	// Defines the amount of resources and their price
	{
		int price;
		int amount;
		
			resources()
			{
				price =0;
				amount=0;
			}

	} droid;

	int price=0;	// Total price of Modules
	int last;	// Price of last Module
	int nom;	// Number of Modules
	int tnom=0;	// Total Number of Modules
	string input;

	while ( true )
	{
	last=NULL;
	

	cout<<"What module do you want to install? (Example:'Avenger' or 'VLLT')"<<endl;
	getline(std::cin, input);

	transform(input.begin(), input.end(), input.begin(), ::tolower);



	/*	-------------
		NON-MO DRONES
		-------------
	*/

	// DD
	if (input == "defense drone" || input == "dd")
	{
		cout<<"\nDD. COST each : 60,000 cr." <<endl;
		last  =60000;
				//-------------------------
							 label_7:
					cout<<"\nHow many do you want? (Modules left:"<<15-tnom<<")\n(NOTE : you may enter a negative number if you made a mistake)\n";
					cin >>nom;

					if (tnom+nom > 15 || tnom+nom < 0)
					{
						cout<<"Sorry, that's not valid. The amount of modules must stay between 0 and 15.\nNever mind, let's try again.\n"<<endl;
						goto label_7;				// SOURCE OF ERROR!!
					}
					else 
					{
						price+=nom*last;
						tnom+=nom;
						droid.amount+=2*nom;
					}
				//-------------------------
	}



	/*	------------
		GENERAL CODE
		------------
	*/

	cout<<"\nCurrent Total Module Price : "<<price<<" cr";
	if (droid.amount == 0 ) { }
	else
	{
		if(droid.amount == 0) { }
		else
		{
			cout<<"\n+"<<droid.amount<<" droids";
		}
	}

	cout<<"\nDo you wish to add (an) other module(s)? (if so, enter 'y', if not, anything else)"<<endl;
	cin >>loop;

	if (tolower(loop) == 'y')
	{ input[0]='\0'; }
	else
	{ break ; }

	}

	cout<<"\n\nHit any key to continue."<<endl;
	cin.ignore();
	cin.get();

	
	return 0;

}


So can you help me? Thank you in advance. ☺
If you want to stop an infinite loop, use break;

Also, you should replace that little label_7 setup with a real loop. I had to ctrl+f the page to see where the label was and where all the goto statements were. You should have heard by now why goto is deprecated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    char again ;

    do
    {
        std::cout << "your input? " ;
        std::string input ;
        std::getline( std::cin, input ) ;
        std::cout << "you entered: " << input << '\n' ;

        // do something with the input

    } while( std::cout << "once again? "
             && std::cin >> again && std::tolower(again) == 'y'
             && std::cin.ignore( 1000, '\n' ) // throw away the new line
           ) ;

}
That's clever, the reason I don't put expressions in conditions like that is because people have yelled at me, but this is clearly a good reason to do it.
We could also write it this way (identical, really), with the simplest possible expression for the condition.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <cctype>

int main()
{
    char again ;

    do
    {
        std::cout << "your input? " ;
        std::string input ;
        std::getline( std::cin, input ) ;
        std::cout << "you entered: " << input << '\n' ;

        // do something with the input

        std::cout << "once again? " ;
        std::cin >> again ;
        again = std::tolower(again) ;
        std::cin.ignore( 1000, '\n' ) ; // throw away the new line

    } while( again == 'y' ) ;
}
Last edited on
If you want to stop an infinite loop, use break;
Yes, that's what I did in the sample code.

Also, you should replace that little label_7 setup with a real loop. I had to ctrl+f the page to see where the label was and where all the goto statements were. You should have heard by now why goto is deprecated.
Well, yes, I've heard about it, I know how much professionals hate it, but I really don't want to complicate the whole thing as making a loop would be more complex. Sorry.

And thank you JLBorges, that helps! I think I got it now, thank you!
Last edited on
Well, yes, I've heard about it, I know how much professionals hate it, but I really don't want to complicate the whole thing as making a loop would be more complex. Sorry.

Not if you do it right. Proper code structure makes your code less complex, and easier to maintain. And believe me, there are few things that will make your code harder to maintain than using goto statements.

I don't mean to be snide, but if you find the idea of a for loop to be "complex", then you're probably not ready to be tackling the sort of task that you're attempting. They're one of the most basic and fundamental building-blocks of control in C++ programs.
I didn't say they were complex. I said (or meant) that I find them MORE complex. Well, if you say I'm not ready then I won't ever be since I'm not learning C++ and I don't intend to. I'm doing this for the fun of it mostly.

But, OK, I believe you, I'll change it to a while loop.

EDIT : There, I think this is good :

Instead of :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
							 label_7:
					cout<<"\nHow many do you want? (Modules left:"<<15-tnom<<")\n(NOTE : you may enter a negative number if you made a mistake)\n";
					cin >>nom;

					if (tnom+nom > 15 || tnom+nom < 0)
					{
						cout<<"Sorry, that's not valid. The amount of modules must stay between 0 and 15.\nNever mind, let's try again.\n"<<endl;
						goto label_7;				// SOURCE OF ERROR!!
					}
					else 
					{
						price+=nom*last;
						tnom+=nom;
						droid.amount+=2*nom;
					}


I do :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
				while (true)
				{
					cout<<"\nHow many do you want? (Modules left:"<<15-tnom<<")\n(NOTE : You may enter a negative number if you previously made a mistake)\n";
					cin >>nom;
					

					if (tnom+nom > 15 || tnom+nom < 0)
					{
						cout<<"Sorry, that's not valid. The amount of modules must stay between 0 and 15.\nNever mind, let's try again.\n"<<endl;
						continue;
					}

					price+=nom*last;
					tnom+=nom;
					droid.amount+=2*nom;
					break;
				}


I still think it's more complex, but if that's the way it should work...
Last edited on
> I still think it's more complex, but if that's the way it should work...

The loop is probably not more complex; it is just more verbose.

Replacing the goto with a while(true) .. continue in the manner above does not qualitatively improve the code in any way. Though it would satisfy people who are dogmatic about blindly abolishing gotos, anyhow, somehow - people who have never really understood why goto was considered harmful.

There is a somewhat lengthy discussion on this topic in a recent thread, startig from http://www.cplusplus.com/forum/general/93381/#msg501742

The code here has similarities to your code http://www.cplusplus.com/forum/general/93381/#msg502369
Topic archived. No new replies allowed.