dear people!
i have made a program to calculate the smallest and largest of numbers trough a reference funtion.

i am stuck in a very small part. in main function i should do like if somebody dont enter anynumber or enter a number bellow 0 the program should say NOTHING TO PROCESS else it should display the result.
but this is not working here is my code in my code it displays nothing to process but also displays the result too.

#include <iostream>
using namespace std;
void determine (int &smallest, int&largest);
int main()
{
int small, large;

determine(small, large);
if(small || large<0)
{
cout <<"NOTHING TO PROCESS\n"<<endl;
}
else
cout << "Your Result:" << endl;
cout << small << ":" << large << "\n" << endl;
return 0;
}

//Function Name                 :determine
//Parameters                    :referenc,  integars
//Return value                  :Smallest & largest visa reference
//Partners                      :None
//Description                   :This function determines the smallest and largest of numbers.
void determine ( int &smallest, int &largest)
{
int A;
A=smallest;
A=largest;
while(A>=0)
{
cout << "Please a number (Negative value to end): ";
 cin >> A; 
if(A<0)
return;
if (A<smallest)smallest=A;    
if (A>largest)largest=A;     
}
}

What's this code supposed to do?

void determine ( int &smallest, int &largest)
{
int A;
A=smallest;
A=largest;

And regarding your original question, I'd say you meant the line of code to look like this instead:

if(small < 0 || large < 0)

Also, cin >> won't work if the user doesn't type anything in (in other words, it keeps waiting until the user types in something). If you want to get around this problem, I'd suggest using getline() instead of cin, and then manually converting the number back to a stringstream:

string line;
getline(cin, line);
istringstream convert(line);
convert >> A;

To be honest, your program is very confusing. Where are the numbers that you're trying to compare? Is the user supposed to input them? If so, it's not happening.

[edit] Nevermind, I didn't see the while {}. See what a difference indentation makes!

the first code that you asked what it supose to do is :
as a user enter A so first time that user enters a number i made it equal to smallest and largest cuz frist time there is no any other numebr to compare.

i did what you suggest for my real problem but it didnt work.
did you get what my code does?
if you can please run it and see yourself.
the code is that user enter as much numbers as they want and to end program and get result they have to enter a negative number so as soon as they enter negative number function will retun via reference parameters and it will display the result now the main problem is that if a user enter a negative number the frist time so in that case there is nothign to compare and it shoudl display NOTHING TO PROCESS and i used that if statment and its not working and giving me a pain. it does work but it dsipalys both that NOTHING TO PROCESS and also the result

Before I comment on this function, it should be noted that smallest and largest are not initialized when passed to determine

void determine ( int &smallest, int &largest)
{
  int A;
  A=smallest;
  A=largest; 
  while(A>=0)  
  {

Why do you set A right here? A should be the number inputted by the user. That said, you might consider using a do-while loop instead of while, so that you can enter the loop and get input before you check whether to repeat the loop.

cout << "Please a number (Negative value to end): ";
    cin >> A;
    if(A<0)
      return;
    if (A<smallest)smallest=A;
    if (A>largest)largest=A; 
  }
}

Small things here. You don't validate the input, so if the user puts something like "asdf" your program will likely go up in flames. For smallest, you have to be careful. If you initialize it to 0 at the beginning, it'll never have something less. You'll want to set it to the first value the user enters. I'd set it to -1 and have another if statement for that case (and before anyone blows up about inefficient design, the speed bottleneck is the user input, so an extra if statement won't be noticeable).

I KNOW bro acctualy the project that teacher has given us is freaking confusing!

i know the prgram waits that user enters a number and the function will stop the loop when user enter a negative number. so all my teacher asks for is that if a user doesnt enter any number (ENTER A NEGATIVE number at first) then if should say "nothing to process"

You could initialize both smallest and largest to -1 back in main before calling determine. This will eliminate the initialization problem, and you can use the method I suggested above for setting smallest on the first inputted value. Then in main, your if statement for "nothing to process" could look like this:

if(smallest < 0) //  nothing or negative value inputted
  cout << "nothing to process" << endl;

>I KNOW bro acctualy the project that teacher has given us is freaking confusing!
Well, that's probably your own fault for not doing proper planning. I always recommend using pseudocode to lay out your tasks before you, and then it's easy to see what has to be done. A lot of your errors here are not with C++ syntax, they are logic errors. And those are way harder to track down.

>so all my teacher asks for is that if a user doesnt enter any number (ENTER A NEGATIVE number at first) then if
>should say "nothing to process"
Did you try my getline() example in the previous post? It will do what you are asking for, although it does add more complexity to the code...

we havnt wstudied getline yet and we are not suposed to use anything behind what we have already studied bro!

we havnt wstudied getline yet and we are not suposed to use anything behind what we have already studied bro!

It depends what your instructor is looking for. If he/she meant that if the user enters -1 right away the program should exit, that will work. However entering literally nothing besides a return at the prompt will require the more complex method.

that didnt work either....
I JUST dont know why its nto working its soo simple in function if anyone enter a negative number for the first input the loop will stop and will return the result back to main so in this case cuz the first time they input negative in main it should say
if ( small || large<0)
cout << "NOTHING TO PROCESS" <<endl;

but its not working

Please post whatever changes you've made to the code. You keep saying it's not working, and we keep saying it should work.

in main it should say
if ( small || large<0)
cout << "NOTHING TO PROCESS" <<endl;

The logic on that line is wrong, period. If small is 500 (valid input), it'll evaluate to true, and print the message.

#include <iostream>
using namespace std;
void determine (int &smallest, int&largest);
int main()
{
int small, large;

determine(small, large);
if(small<0)
cout <<"NOTHING TO PROCESS\n"<<endl;
else
cout << small << ":" << large << "\n" << endl;
return 0;
}

//Function Name                 :determine
//Parameters                    :referenc,  integars
//Return value                  :Smallest & largest visa reference
//Partners                      :None
//Description                   :This function determines the smallest and largest of numbers.
void determine ( int &smallest, int &largest)
{
int A;
do
{
cout << "Please a number (Negative value to end): ";
 cin >> A;
 
if(A<0)
return;
if (A<smallest)smallest=A;    
if (A>largest)largest=A;     
}
while(A>=0);
}

here you go bro

#include <iostream>
using namespace std;
void determine (int &smallest, int&largest);
int main()
{
 int small, large;

 determine(small, large);
 if(small<0)
  cout <<"NOTHING TO PROCESS\n"<<endl;
 else
  cout << small << ":" << large << "\n" << endl;
 return 0;
}

//Function Name                 :determine
//Parameters                    :referenc,  integars
//Return value                  :Smallest & largest visa reference
//Partners                      :None
//Description                   :This function determines the smallest and largest of numbers.
void determine ( int &smallest, int &largest)
{
 int A;
 do
 {
  cout << "Please a number (Negative value to end): ";
  cin >> A;
  if(A<0)
   return;
  if (A<smallest)smallest=A;    
  if (A>largest)largest=A;     
 }
 while(A>=0);
}

The reason why it doesn't work when the user doesn't enter any numbers is because small and large are never initialized. Try initializing them to -1.

However, this causes a problem inside the function because there is no number that the user can enter that is smaller than -1 (otherwise it justs exits). One solution is to add something like this before the while() loop so that small and large are set correctly before entering the loop:

int A;
cout << "Please a number (Negative value to end): ";
cin >> A;
if (A < 0) {
    return;
}
else {
    smallest = A;
    largest = A;
}
// your while loop goes here

Of course, this only works if you have initialized small and large in your main() function!

This article has been dead for over six months. Start a new discussion instead.