:?: can anyone tell me where the error is?

#include<iostream.h>
#include<conio.h>

void main()
{
 clrscr();
void zero_small(int &,int &);
int a,b;


cout<<"\n\n\n\tEnter the first no  ";
cin>>a;
cout<< "\n\n\n\tEnter the second no  ";
cin>>b;

 zero_small(a,b);
cout<<"\n\n\n\tValue of the first no is "<<a;
cout<<"\n\n\n\tValue of the second no is  "<<b;
getch();
}


void  zero_small(int& a,int &b  )
{
 if (a<b)
  {a==0;  }
else b==0;

return;
}

Dani AI

Generated

A few focused notes that build on the helpful replies from , , and and that address issues not covered above.

The expression you wrote in the function computes a boolean result and then discards it; that is why nothing actually changes. Turning on compiler warnings (for example g++ -Wall -Wextra -pedantic) will typically flag “statement with no effect” or similar and catch this quickly. Also avoid void main() — the portable signature is int main() — and remove nonstandard DOS-era helpers (conio.h, clrscr, getch) if you want code that compiles cleanly on modern toolchains.

Two additional practical points: (1) the prototype declared inside main is legal C++ but unusual and confusing; put function declarations at file scope, or define the function before main. (2) Decide and document the behavior for equal inputs — in the current code the else branch will run when a == b, which may not be what you want. Make the equality case explicit so tests cover a < b, a > b, and a == b.

Quick checklist to finish up: enable full warnings, move the prototype out of main, use the standard headers and int main(), explicitly handle the equal case, and add simple tests for the three comparison outcomes (and a negative-number case). Those steps will make the bug obvious, improve portability, and prevent similar mistakes in future.

Recommended Answers

All 7 Replies

Member Avatar for Member #46692

> can anyone tell me where the error is?
Erm, maybe your logic amongst other things?

I mean what were you expecting the function to do:

void  zero_small ( int& a, int &b  )
{
   if ( a < b )
   {
      a == 0;
   }
   else b == 0;

   return;
}

i wanted to print the smaller no as 0..thatz why i did that..is that wrong?

Member Avatar for Member #46692

Well why do you have a return in the function, you shouldn't need one because you are passing back by reference.

You don't use the double equal sign if you are setting a value to a variable.

This is how I might do it.

#include <iostream.h>

void zero_small ( int &a, int &b )
{
  if ( a < b )
  {
    a = 0; //one equal sign not double!
  }
  else b = 0;
 //no returns
}

int main()
{
  int a;
  int b;

  cout << "\n\n\n\tEnter the first no  ";
  cin >> a;
  cout << "\n\n\n\tEnter the second no  ";
  cin >> b; 
  zero_small ( a, b );

  cout << "\n\n\n\tValue of the first no is " << a;
  cout << "\n\n\n\tValue of the second no is  " << b;

  getchar();
  getchar();
  return 0;
}

I agree with iamthwee. There seems no point in that segment of code! It checks a relation: if a is equal to zero or not, if a is less than b, otherwise it relates b to 0, which is pointless.

Is the code meant to say, check a==(some integer) and then print a=some expression, in the case that a<b, or something?

Or - you are trying to assign a or b to 0, in which case it would be a=0 or b=0, not the relational operator ==.


EDIT//oops! sorry for posting this, i think the top two posts were posted as i was writing this!

Well why do you have a return in the function, you shouldn't need one because you are passing back by reference.

Wait, I'm not sure if he edited the code in the first post, but the function just had return; , not return someVal; . It's not neccesary to include the return; statement at the end of a void function, but I sometimes put it just so I'm controlling when the function returns, not the end curly bracket. Is that considered "wrong" or weird?

>Is that considered "wrong" or weird?
Falling off the end of a function automatically returns, so it's weird in that people don't expect to see that kind of redundancy in well written code. It's not wrong though.

thank u !!...i got de point!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.