What is the issue with assigning the value of largest to the variable number?

  • Thread starter Thread starter Valour549
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion revolves around issues in a C++ program designed to find the largest and second-largest numbers from a set of positive integers. The main problem identified is the misuse of the assignment operator '=' instead of the equality operator '==' in an if-statement, which leads to incorrect logic and results. When the condition checks if a number is equal to the largest, it inadvertently assigns the value of largest to the number, causing subsequent comparisons to fail. The user also expresses frustration over inconsistencies in the code's behavior when copied into a new file, suggesting potential hidden errors in the original file. Ultimately, correcting the operator resolves the logical errors in the program.
Valour549
Messages
57
Reaction score
4
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:
Technology news on Phys.org
Sorry, misread ...
lemme check.
And please use the [ code ] instead of the [ spoiler ] goody​
 
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:
  if(number>largest)
{
  secondlargest=largest;
  largest=number;
}
  if(number=largest)
{
  secondlargest=secondlargest;
  largest=largest;
}
  if((number<largest)&&(number>secondlargest))
{
  secondlargest=number;
}

PS: I can't use the <code> function... I must use <spoiler>, because then I can't italics the part I'm referring to.
 
Last edited:
Don't get the error !

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 :smile:)
 
  • Like
Likes Valour549
Valour549 said:
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:
  if(number>largest)
{
  secondlargest=largest;
  largest=number;
}
  if(number=largest)   // Error here
{
  secondlargest=secondlargest;
  largest=largest;
}
  if((number<largest)&&(number>secondlargest))
{
  secondlargest=number;
}

PS: I can't use the <code> function... I must use <spoiler>, because then I can't italics the part I'm referring to.
Your second if clause will always be true, as long as largest is not equal to zero.
For comparison, use ==. For assignment, use =.
 
  • Like
Likes Valour549
Mark, look under the spoiler in post #1...
 
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:
/*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;
}
 
Holy dang. 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
 
Mark44 said:
Your second if clause will always be true, as long as largest is not equal to zero.
For comparison, use ==. For assignment, use =.
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
Valour549 said:
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;)

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.
 
  • Like
Likes Valour549
  • #11
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:
  if(number>largest)
{
  secondlargest=largest;
  largest=number;
}
  if(number=largest)
{
  secondlargest=secondlargest;
  largest=largest;
}
  if((number<largest)&&(number>secondlargest))
{
  secondlargest=number;
}
 
Last edited:
  • #12
Valour549 said:
Huh how does it do that...? The if(number=largest) is merely a condition
It's not a comparator, it is an assignment. These two bits of code do the same thing:
C:
if (number=largest)
   {
      cout << number << endl;
   }
C:
number=largest;
if (number)
   {
      cout << number << endl;
   }

What you want is
C:
if (number==largest)
   {
      cout << number << endl;
   }
 
  • Like
Likes Valour549
  • #13
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
Valour549 said:
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?
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 +.

Valour549 said:
2) If = is an assignment, then it doesn't even make sense to it inside the parenthesis of an if-clause, right?
It perfectly legal in C/C++. I've often seen things like
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.

Valour549 said:
Also, in your second code above, what on Earth does if(number) mean?
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.
 
  • Like
Likes Valour549
  • #15
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
Valour549 said:
number=largest; // So here we set their values to be equivalent.
That's not the right way to look at it. You are assigning the value of largest to the variable number.
Valour549 said:
By the way is number=largest the same thing as largest=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;

Valour549 said:
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?
}
 
  • #17
Valour549 said:
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?
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.
 
  • Like
Likes Silicon Waffle
  • #18
[about the statement 'number = largest;']

Mark44 said:
You are assigning the value of largest to the variable number.

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'.
 
Back
Top