0

I was just wondering something. I have the following code:

#include <iostream> using namespace std; int main() { int number, largest, counter = 1; while (counter <= 10) { cout << "Enter a number: "; cin >> number; if (counter = 1) { largest = number; } else if (number > largest) { largest = number; } counter++; } cout << "\n\nThe largest number is: " << largest; system("pause"); return 0; } 

The thing is, it never terminates. I did manage to fix the problem by modifying the code a little, but I was wondering why this happened. Here is the fixed code:

#include <iostream> using namespace std; int main() { int number, largest, counter = 1; cout << "Enter a number: "; cin >> number; largest = number; while (counter < 10) { cout << "Enter a number: "; cin >> number; if (number > largest) { largest = number; } counter++; } cout << "\n\nThe largest number is: " << largest << endl; system("pause"); return 0; } 

It seems that after removing the else if statement it worked. What happened?

4
  • Doesn't the compiler give you a warning? Commented Jun 30, 2011 at 13:37
  • If I'd be the compiler I'd say like "Cannot implicitly convert type int to bool" or like "you shouldn't set a variable in if" or like "hey Emile wazzup why use only one = in if?Try ==." Commented Jun 30, 2011 at 13:43
  • 1
    @JohnnyCageWins. My compiler (MSVC) says something along the lines... Commented Jun 30, 2011 at 13:44
  • @Armen say hi from me ^^ Commented Jun 30, 2011 at 13:49

10 Answers 10

8
if (counter = 1) 

this should be

if (counter == 1) 

otherwise, you're going to reset your counter to 1 each iteration.

Sign up to request clarification or add additional context in comments.

Comments

4

if (counter = 1) reassigns 1 to counter every loop this being always < 10.

You want if (counter == 1).

Comments

4

A common mistake:

if( counter = 1) // assignment operator 

This will set counter's value to 1 at each iteration, and the loop never finishes.

You should use

if( counter == 1) // equality operator ^^^^ 

which is exactly what you mean

Comments

3
 if (counter = 1) 

This does not compare counter and 1, it assigns 1 to counter and then checks counter- which we just set to 1 so it will always be positive and always be <= 10.

Comments

1

The line

if (counter = 1) 

Should be

if (counter == 1) 

since you want to compare, not to assign value.

Comments

1

Your first example had

if (counter = 1) 

instead of

if (counter == 1) 

so the if statement would reset counter to 1 during each iteration.

Comments

0

Your problem is here:

if (counter = 1) 

Assignment instead of comparison. Compile with higher warning level.

Comments

0
if (counter = 1) 

So, counter value is 1 forever.

Comments

0

You're assigning 1 to counter rather than comparing it, use == instead of =

Comments

0

As other notice, this is a common error. Your could avoid it by typing

if( 1 == counter ) 

instead of

if( counter == 1 ) 

since

if( 1 = counter ) 

would not compile (if you made the error and forgot an '=').

2 Comments

IIRC the Debian hack was implemented by changing an equality to an assignment. Since then, it may look backward, but I'm a big fan of constant-lvalue comparison.
Personal opinion, but I hate and loathe than construct. Compiling with warnings enabled will catch this particular error. Code analyzers will catch instances that the compiler won't catch such as if ((counter = 1)) ... Don't make me write things bass-ackwards.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.