# What is wrong with my code?

1. Dec 29, 2015

### Valour549

My code is to find the largest and second-largest numbers, from an arbitrary number of postive integers.

For some reason, the part of the code in italics is not being recognized. How do I know? Because:
- Let's say we choose to compared two numbers, if my first number is 5, and second number is 9, the result is correct. In other words it shows the largest number as being 9 and second-largest as being 5.
- However, if my first number is 9, and second number is 5, the result is incorrect. It shows the largest number as being 9 and the second-largest as being 0. In other words, it does not recognize the condition when an input is greater than secondlargest.

I have checked for ages and even compared it with examples online, and can't find where my code is wrong. Any help would be greatly appreciated.
#include<iostream>
using namespace std;
int main()
{
int number, largest, secondlargest, counter, c;
largest=0; secondlargest=0; counter=1;
cout<<"This program will find the largest and second largest numbers for you."<<endl;
cout<<"Please enter only positive integers for comparison."<<endl<<endl;
cout<<"How many numbers are you comparing? ";
cin>>c;
cout<<endl;
while(counter<=c)
{
cout<<"Please enter the "<<counter<<"st number: ";
cin>>number;
if (number>largest)
{ secondlargest=largest;
largest=number; }
else if (number>secondlargest)
{ secondlargest=number; }

counter++;
}
cout<<endl;
cout<<"The largest number is "<<largest<<endl;
cout<<"The second largest number is "<<secondlargest<<endl;
system("pause");
return 0;
}

Last edited by a moderator: Dec 29, 2015
2. Dec 29, 2015

### BvU

lemme check.
And please use the [ code ] instead of the [ spoiler ] goody​

3. Dec 29, 2015

### Valour549

Well my original code was like this, but it had the exact same issues... I don't get what's wrong since I've listed all the possible conditions.
Code (C):
if(number>largest)
{
secondlargest=largest;
largest=number;
}
if(number=largest)
{
secondlargest=secondlargest;
largest=largest;
}
if((number<largest)&&(number>secondlargest))
{
secondlargest=number;
}
PS: I cant use the <code> function... I must use <spoiler>, because then I can't italics the part I'm referring to.

Last edited: Dec 29, 2015
4. Dec 29, 2015

### BvU

Don't get the error !

Code (C):

Hello World
This program will find the largest and second largest numbers for you.
Please enter only positive integers for comparison.

How many numbers are you comparing? 2

Please enter the 1st number: 9
Please enter the 2st number: 5

The largest number is 9
The second largest number is 5

Sorry for the hello world - this is my first C++ program .... (brainwashed fortran programmer from a previous century )

5. Dec 29, 2015

### Staff: Mentor

Your second if clause will always be true, as long as largest is not equal to zero.
For comparison, use ==. For assignment, use =.

6. Dec 29, 2015

### BvU

Mark, look under the spoiler in post #1...

7. Dec 29, 2015

### Valour549

BvU... you're right. I copy and pasted the code I typed above into a brand new file and it worked flawlessly. My original friggin file is still giving me the error. Here's the code from my original file. You tell me what's different about it from the one I typed above (that would be responsible for this error).

Code (C):
/*Joe Tsai (my C++ can't read Chinese!) E94045020*/
#include<iostream>
using namespace std;
int main()
{
int number, largest, secondlargest, counter, c;
largest=0; secondlargest=0; counter=1;
cout<<"This program will find the largest and second largest numbers for you."<<endl;
cout<<"Please enter only positive integers for comparison."<<endl<<endl;
cout<<"How many numbers are you comparing? ";
cin>>c;
cout<<endl;
while(counter<=c)
{
cout<<"Please enter the "<<counter<<"st number: ";
cin>>number;
if (number>largest)
{
secondlargest=largest;
largest=number;
}
else if (number>secondlargest)
{  secondlargest=number;
}
counter++;
}
cout<<endl;
cout<<"The largest number is "<<largest<<endl;
cout<<"The second largest number is "<<secondlargest<<endl;
system("pause");
return 0;
}

8. Dec 29, 2015

### Valour549

Holy ****. There's a ghost in my computer.

So my original C++ file (which I freshly typed a few hours ago) -> Compile & Run -> gives the error described above.

Now I select the entire code, copy and paste it into another file (without a single difference) -> Compile & Run -> no error.

Here's my original C++ file (http://www.filedropper.com/e94045020hw51). You guys give it a go..... lol

9. Dec 29, 2015

### Valour549

Yes I understand now that my second if-clause will always be true, but can you kindly explain why that causes an error? You are right though. After I removed that part the error disappeared.

Afterall, the resulting effect from that if-clause shouldn't make a difference... (namely secondlargest=secondlargest; largest=largest;)

10. Dec 29, 2015

### D H

Staff Emeritus
The problem is that if(number=largest) sets number to the value of largest. Because of this your next test never passes because number is equal to largest.

You don't need all those tests you had in post #3. The two tests you have in post #7 suffice.

11. Dec 29, 2015

### Valour549

Huh how does it do that...? The if(number=largest) is merely a condition which if satisfied performs -> secondlargest=secondlargest; largest=largest;

More realistically, let's talk step-by-step about why it gives me the error, which I honestly cannot comprehend. Assume now I've chosen to compare only two numbers.

So I've set secondlargest=0, and largest=0. My first number is 9.

if(number>largest)
is satisfied. Therefore, secondlargest=0, largest=9. Number is still 9. Moving on...

if(number=largest) is now satisfied. Therefore, secondlargest=0, largest=9. Number is still 9. Nothing has changed. Moving on...

if((number<largest)&&(number>secondlargest)) is not satisfied. No actions are performed.

Right now as it stands, secondlargest=0, and largest=9. My second number is now 5.

if(number>largest) is not satisfied. No actions are performed.

if(number=largest) is not satisfied. No actions are performed.

if((number<largest)&&(number>secondlargest)) is satisfied. Therefore, secondlargest=5.

For reference let me copy the code down here:
Code (C):
if(number>largest)
{
secondlargest=largest;
largest=number;
}
if(number=largest)
{
secondlargest=secondlargest;
largest=largest;
}
if((number<largest)&&(number>secondlargest))
{
secondlargest=number;
}

Last edited: Dec 29, 2015
12. Dec 29, 2015

### Staff: Mentor

It's not a comparator, it is an assignment. These two bits of code do the same thing:
Code (C):

if (number=largest)
{
cout << number << endl;
}

Code (C):

number=largest;
if (number)
{
cout << number << endl;
}

What you want is
Code (C):

if (number==largest)
{
cout << number << endl;
}

13. Dec 29, 2015

### Valour549

Hmm I'm starting to get what you mean now... Though I still have some questions:

1) If = is used by itself, it assigns number to be equal to whatever the value of largest is. But in the cases of >= (greater than or equal to), or, <= (less than or equal to), the equal signs are comparators here, not assignments?

2) If = is an assignment, then it doesn't even make sense to it inside the parenthesis of an if-clause, right? Also, in your second code above, what on earth does if(number) mean?

14. Dec 29, 2015

### Staff: Mentor

The comparison operators are ==, <, >, >=, <=, != . Each combination of characters is its own symbol: >= is not the same as > and =, just like ++ is not the same as + and +.

It perfectly legal in C/C++. I've often seen things like
Code (C):

if (a =(int*)calloc(n,sizeof(int)) == NULL)
{
cout << "Error allocating memory" << endl;
}

which allocates memory inside the if, using the if the make sure the memory was allocated.

It tests if number is zero or not, which is ultimately what comparators do: if number==largest is false, then (number==largest) == 0 is true.

15. Dec 29, 2015

### Valour549

number=largest; // So here we set their values to be equivalent. By the way is number=largest the same thing as largest=number?
if (number) // Here we are testing if number is zero or not, in other words, we are also testing if largest is zero or not.
{
cout << number << endl; // Hmmm so if the result is true, then the code will display the number (which should be zero) here? If false, it won't display anything?
}

16. Dec 29, 2015

### Staff: Mentor

That's not the right way to look at it. You are assigning the value of largest to the variable number.
Here you are storing the value of number to the variable named largest. In an assignment statement, the variable being assigned to goes on the left side of =.

So you could have this: number = 3;
but it would be a syntax error to have this: 3 = number;

You could also have this: number = largest + 1;
but not this: largest + 1 = number;

17. Dec 29, 2015

### D H

Staff Emeritus
In some languages (but not in C or C++ or Java), a=b is an example of a special kind of statement, the assignment statement. In some of those languages, you can say a=b=c=42 and have all three of those variables set to the value 42. There is no assignment statement in C.

In C, C++, Java, and some other languages, = is an operator rather than a statement. You can use a=42; in C-like languages because of the way the assignment operator works. You can even use a=b=c=42; in those languages, once again thanks to the way the assignment operator works. But because it is an operator rather than a statement, you can also use the assignment operator as an expression in C-like languages.

This of course means that you can shoot yourself in the foot (or a couple of feet higher) in C-like languages with statements such as if (number = largest) { ... } . One way around this is to use yoda conditions: if (42 == number) { ... } . The cited code won't compile if one mistakenly types if (42 = number) { ... } . Yoda conditions only help if you want to compare a variable with a constant. They won't help in your case because you want to compare two variables.

Personally, I can't stand (hate and loathe are better words) yoda conditions. A much better solution is to use a good compiler and set the warning level to a reasonable value. Because this is a common mistake, even a halfway decent compiler will warn you that if (number = largest) { ... } might be a mistake.

18. Dec 29, 2015

### Staff: Mentor

[about the statement 'number = largest;']

To put it another way (in case the OP's native language isn't English), you are copying data from the variable (memory location) 'largest' to the variable (memory location) 'number'.