Reputation: 25
Why is this do while loop infinitely executing?
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
unsigned int base;
cout << "Base: ";
cin >> base;
for (int i = 1; i <= base; i++) {
int j = 0;
do {
cout << "#";
j++;
} while (j = i);
cout << endl;
};
system("pause");
// keep on building up until you reach 'base'
return 0;
}
I am really confused about this. This program is supposed to create a triangle like this
#
##
###
(user inputs bottom number, so in this example the base = 3) Anybody help fix my rookie mistake?
Upvotes: 0
Views: 41
Reputation: 878
You might wanna try while (j == i);
.
j = i
is a variable declaration/assignment which will always be true as long as it succeeds. It seems like you want to compare the two, so use the equal to operator: ==
.
Edit: Made a typo and therefore the same mistake as your question shows. Fixed that.
Upvotes: 2