Loop not stopping when it's suppose to

Hello. I'm trying to work on this code for a school project that calculates the earnings and streams of multiple songs. I can't get the loop to stop when it reaches the number of songs a user types in. How do I fix the problem?

// This program lets a user upload as many songs as they want or upload a single song.

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
int option;
int label;
int num;
double views;
int choice;
string artist;
string song;
int total;


//the program asks the user if they want to upload only one song or multiple songs

cout << " Wlecome to the CPCC uploading service!" << endl;

do
{

cout << "Please choose from the following menu options by entering a number 1-3." << endl;


cout << "1. Upload one song to a streaming service" << endl;
cout << "2. Upload multiple songs to a streaming service" << endl;
cout << "3. End program" << endl;

cin >> option;

if (option == 3)
{
cout << "Thank you for using the CPCC uploading service. Goodbye." << endl;
}

else if (option >= 4)
{
cout << "Error: number entered is not an option for options 1-3" << endl;
}


// TIDAl= 0.0125, Amazon= 0.00402, AM(Apple Music)= 0.00737, Spotify= 0.00437, Youtube= 0.00069
double Tidal= 0.0125, Amazon= 0.00402, AM= 0.00737, Spotify= 0.00437, Youtube= 0.00069;

// Option 1 is if a user wants to upload only one song.
if (option == 1)
{
cout << "which streaming service do you want to upload your song to?" << endl;
cout << "1: TIDAL" << endl;
cout << "2: Amazon" << endl;
cout << "3: Apple Music" << endl;
cout << "4: Spotify" << endl;
cout << "5: Youtube" << endl;

cout << "Please enter a number 1-5 to select which streaming service you want to upload your song to." << endl;

cin >> choice;

if (choice == 1)
{


cout << " How many views does the song have? " << endl;
cin >> views;
cin.ignore();

cout << " What is the Artist's name?" << endl;
getline(cin,artist);
cout << " What song from the artist do you want to upload? " << endl;
getline(cin,song);


cout << "Song Name: " << song << endl;
cout << "Artist's Name: " << artist << endl;
cout << "Streaming Service: " << "TIDAL" << endl;
cout << "Money Made $ " << (Tidal * views) << endl;


}

if (choice == 2)
{

cout << " How many views does the song have? " << endl;
cin >> views;
cin.ignore();

cout << " What is the Artist's name?" << endl;
getline(cin,artist);
cout << " What song from the artist do you want to upload?" << endl;
getline(cin,song);

cout << "Song Name: " << song << endl;
cout << "Artist's Name: " << artist << endl;
cout << "Streaming Service: " << "Amazon" << endl;
cout << "Money Made $ " << (Amazon * views) << endl;

}

if (choice == 3)
{

cout << " How many views does the song have? " << endl;
cin >> views;
cin.ignore();

cout << " What is the Artist's name? " << endl;
getline(cin,artist);
cout << " What song from the artist do you want to upload? " << endl;
getline(cin,song);

cout << "Song Name: " << song << endl;
cout << "Artist's Name: " << artist << endl;
cout << "Streaming Service: " << "Apple Music" << endl;
cout << "Money Made $ " << (AM * views) << endl;

}

if (choice == 4)
{

cout << " How many views does the song have? " << endl;
cin >> views;
cin.ignore();

cout << " What is the Artist's name" << endl;
getline(cin,artist);
cout << " What song from the artist do you want to upload?" << endl;
getline(cin,song);

cout << "Song Name: " << song << endl;
cout << "Artist's Name: " << artist << endl;
cout << "Streaming Service: " << "Spotify" << endl;
cout << "Money Made $ " << (Spotify * views);

}

if (choice == 5)
{

cout << " How many views does the song have? " << endl;
cin >> views;
cin.ignore();
cout << " What is the Artist's name? " << endl;
getline(cin,artist);
cout << " What song from the artist do you want to upload? " << endl;
getline(cin,song);

cout << "Song Name: " << song << endl;
cout << "Artist's Name: " << artist << endl;
cout << "Streaming Service: " << "Youtube" << endl;
cout << "Money Made $ " << (Youtube * views);
}


}

// option 2 is where the user can upload multiple songs. Another option is added where the user can end the program after they upload their songs.

else if (option == 2 )
{
cout << "which streaming service will you use to upload your songs?" << endl;

cout << "1: TIDAL" << endl;
cout << "2: Amazon" << endl;
cout << "3: Apple Music" << endl;
cout << "4: Spotify" << endl;
cout << "5: Youtube" << endl;

cin >> choice;

cout << "How many songs do you want to upload?" << endl;
cin >> num;

if (num <=0 )
{
cout << "Error: the number of songs you want to upload must be greater than 0." << endl;
}

cout << "Please enter 1 or 2 to select the type of distribution to use for your songs." << endl;

cout << "1: Major Label" << endl;
cout << "2: Independent Label" << endl;

cin >> label;



srand(num);


for(int song = 0; song < num; song++)
{
if(label == 1)
{
// major labels generate between 100,000 and 50,000,000 streams
num = 100000 + rand() % 49900000;
}
else
{
// indie labels generate between 10,000 and 1,000,000 streams
num = 10000 + rand() % 990000;
}

// add to the total streams
total += num;

// output the song number and the streams
cout << "\t Song #" << (song + 1) << ": \t" << num << " streams" << endl;
}
return 0;

}



}while (option !=3);






return 0;
}
welcome!
You may have noticed other posts with code have it formatted and indented like in a code editor. To do that, select the code, and mash the <> button on the sidebar or type code and /code inside [] pairs. Once we can read it better, the problem should be easy to spot and fix.

edit: found it anyway. you modify num inside the loop, changing it from the # of songs to some gibberish big value. It probably ends eventually, but it would take months to get there typing all that crap in :) This would also break the 'total' variable

srand num is cute but probably not effective as the random numbers would be the same every time you did 3 songs or every time you did 10 songs etc. The same seed gives the same values, which is useful for some code, but not useful for randomized code. Use time(0) or better, the <random> header and high quality random tools of modern C++ (rand is from C and a low quality relic).
Last edited on
What do you mean by that? just change num to something else?
perhaps have a variable called numsongs that you use for the number of songs. num is not a descriptive name, and its difficult to guess what you want it to mean from its name. "number" of course, but what number, what does it represent? Its a mystery ... so I would rename num to something meaningful, whatever it should be, and add another one for num_songs or whatever words you want

then it looks roughly like
cout how many songs...
cin >> numsongs;

for(int i{}; i < numsongs; i++)
{
randonumber = something;
etc;
}
Last edited on
I still have the infinite loop problem.

for(int i{}; i < numsongs; i++)
{
randomnumber = something;
etc;


if (label == 1)
{
// major labels generate between 100,000 and 50,000,000 streams
numsongs = 100000 + rand() % 49900000;
}
else if (label ==2 )
{
// indie labels generate between 10,000 and 1,000,000 streams
numsongs = 10000 + rand() % 990000;
}

// add to the total streams
total += numsongs;

// output the song number and the streams
cout << "\t Song #" << (amount + 1) << ": \t" << numsongs << " streams" << endl;


(amount <= numsongs);


}
If I just have the new line, it won't generate any random number for the stream. Maybe should use time?
nope, it didn't work.
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
As the original formatted:

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
	int option;
	int label;
	int num;
	double views;
	int choice;
	string artist;
	string song;
	int total;

	//the program asks the user if they want to upload only one song or multiple songs

	cout << " Wlecome to the CPCC uploading service!" << endl;

	do {
		cout << "Please choose from the following menu options by entering a number 1-3." << endl;

		cout << "1. Upload one song to a streaming service" << endl;
		cout << "2. Upload multiple songs to a streaming service" << endl;
		cout << "3. End program" << endl;

		cin >> option;

		if (option == 3) {
			cout << "Thank you for using the CPCC uploading service. Goodbye." << endl;
		}
		else if (option >= 4) {
			cout << "Error: number entered is not an option for options 1-3" << endl;
		}

		// TIDAl= 0.0125, Amazon= 0.00402, AM(Apple Music)= 0.00737, Spotify= 0.00437, Youtube= 0.00069
		double Tidal = 0.0125, Amazon = 0.00402, AM = 0.00737, Spotify = 0.00437, Youtube = 0.00069;

		// Option 1 is if a user wants to upload only one song.
		if (option == 1) {
			cout << "which streaming service do you want to upload your song to?" << endl;
			cout << "1: TIDAL" << endl;
			cout << "2: Amazon" << endl;
			cout << "3: Apple Music" << endl;
			cout << "4: Spotify" << endl;
			cout << "5: Youtube" << endl;

			cout << "Please enter a number 1-5 to select which streaming service you want to upload your song to." << endl;

			cin >> choice;

			if (choice == 1) {
				cout << " How many views does the song have? " << endl;
				cin >> views;
				cin.ignore();

				cout << " What is the Artist's name?" << endl;
				getline(cin, artist);
				cout << " What song from the artist do you want to upload? " << endl;
				getline(cin, song);

				cout << "Song Name: " << song << endl;
				cout << "Artist's Name: " << artist << endl;
				cout << "Streaming Service: " << "TIDAL" << endl;
				cout << "Money Made $ " << (Tidal * views) << endl;
			}

			if (choice == 2) {
				cout << " How many views does the song have? " << endl;
				cin >> views;
				cin.ignore();

				cout << " What is the Artist's name?" << endl;
				getline(cin, artist);
				cout << " What song from the artist do you want to upload?" << endl;
				getline(cin, song);

				cout << "Song Name: " << song << endl;
				cout << "Artist's Name: " << artist << endl;
				cout << "Streaming Service: " << "Amazon" << endl;
				cout << "Money Made $ " << (Amazon * views) << endl;
			}

			if (choice == 3) {
				cout << " How many views does the song have? " << endl;
				cin >> views;
				cin.ignore();

				cout << " What is the Artist's name? " << endl;
				getline(cin, artist);
				cout << " What song from the artist do you want to upload? " << endl;
				getline(cin, song);

				cout << "Song Name: " << song << endl;
				cout << "Artist's Name: " << artist << endl;
				cout << "Streaming Service: " << "Apple Music" << endl;
				cout << "Money Made $ " << (AM * views) << endl;
			}

			if (choice == 4) {
				cout << " How many views does the song have? " << endl;
				cin >> views;
				cin.ignore();

				cout << " What is the Artist's name" << endl;
				getline(cin, artist);
				cout << " What song from the artist do you want to upload?" << endl;
				getline(cin, song);

				cout << "Song Name: " << song << endl;
				cout << "Artist's Name: " << artist << endl;
				cout << "Streaming Service: " << "Spotify" << endl;
				cout << "Money Made $ " << (Spotify * views);
			}

			if (choice == 5) {
				cout << " How many views does the song have? " << endl;
				cin >> views;
				cin.ignore();

				cout << " What is the Artist's name? " << endl;
				getline(cin, artist);

				cout << " What song from the artist do you want to upload? " << endl;
				getline(cin, song);

				cout << "Song Name: " << song << endl;
				cout << "Artist's Name: " << artist << endl;
				cout << "Streaming Service: " << "Youtube" << endl;
				cout << "Money Made $ " << (Youtube * views);
			}
		}

		// option 2 is where the user can upload multiple songs. Another option is added where the user can end the program after they upload their songs.

		else if (option == 2) {
			cout << "which streaming service will you use to upload your songs?" << endl;

			cout << "1: TIDAL" << endl;
			cout << "2: Amazon" << endl;
			cout << "3: Apple Music" << endl;
			cout << "4: Spotify" << endl;
			cout << "5: Youtube" << endl;

			cin >> choice;

			cout << "How many songs do you want to upload?" << endl;
			cin >> num;

			if (num <= 0) {
				cout << "Error: the number of songs you want to upload must be greater than 0." << endl;
			}

			cout << "Please enter 1 or 2 to select the type of distribution to use for your songs." << endl;

			cout << "1: Major Label" << endl;
			cout << "2: Independent Label" << endl;

			cin >> label;

			srand(num);

			for (int song = 0; song < num; song++) {
				if (label == 1) {
					// major labels generate between 100,000 and 50,000,000 streams
					num = 100000 + rand() % 49900000;
				} else {
					// indie labels generate between 10,000 and 1,000,000 streams
					num = 10000 + rand() % 990000;
				}

				// add to the total streams
				total += num;

				// output the song number and the streams
				cout << "\t Song #" << (song + 1) << ": \t" << num << " streams" << endl;
			}
			return 0;
		}

	} while (option != 3);

	return 0;
}


This program is begging to use switch statements and some functions. main() is way too long.

Perhaps for the stream income code:

1
2
3
4
5
6
7
8
9
10
11
12
			//srand(num);

			for (int song = 0; song < num; ++song) {
				const int generate {label == 1 ? 100000 + rand() % 49900000 : 10000 + rand() % 990000};

				// add to the total streams
				total += generate;

				// output the song number and the streams
				cout << "\t Song #" << (song + 1) << ": \t" << generate << " streams" << endl;
			}
			//return 0; 

Last edited on
Topic archived. No new replies allowed.