Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,126 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,782 people online right now. Registration is fast and FREE... Join Now!




determine which r Postive and Negative numbers

 
Reply to this topicStart new topic

determine which r Postive and Negative numbers

cmps_sciloser
16 Apr, 2007 - 07:47 PM
Post #1

New D.I.C Head
*

Joined: 16 Apr, 2007
Posts: 4


My Contributions
Okay, I was given the assignment where any user is prompted to enter numbers from a keyboard and the program stated where the numbers entered were either positve or negative. I have compiled the following and I've tried to run the program. I did not have any errors but I didn't have any output. Can someone tell me what I did wrong..?

CODE
#include<iostream.h>
using namespace std;

int main()

{
int num_great=0, num_less=0, num_is=0;
int c=0;
int n;
int x;

while ( c < x )
{
cout<<"Enter a number\n";

cin>>n;
c++;

if ( n < 0)
num_less=n;
cout<<"this is a negative number"<<num_less<<"\n";

if (n > 0)
num_great=n;
cout<<"this is a positve number"<<num_great<<"\n";

else if (n = 0)
num_is=n;
cout<<"You entered zero but zero is nor positve or negative"<<num_is<<"\n";

}

return 0;

}



help me please~~~!
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: Determine Which R Postive And Negative Numbers
16 Apr, 2007 - 08:59 PM
Post #2

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,906



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
if ( n < 0)
num_less=n;
cout<<"this is a negative number"<<num_less<<"\n";

You have to use braces after an if construct if you plan to use multiple statements in that block.
If we don't add braces, only the first statement following the if condition is taken as a part of the block which executes if the condition is true.

Hence, you'll have to add braces for every if statement in your code.

QUOTE
else if (n = 0)

This is a common mistake made by many.
Use == to test if n is equal to zero. Using a single = simply sets the value 0 to n and returns true if successful.

Your final program should look like this:
CODE

#include<iostream>
using namespace std;

int main()
{
int num_great=0, num_less=0, num_is=0;
int c=0;
int n;
int x;

while ( c < x )
{
cout<<"Enter a number\n";

cin>>n;
c++;

if ( n < 0)
{
   num_less=n;
   cout<<"this is a negative number"<<num_less<<"\n";
}
else
{
   if (n > 0)
   {
      num_great=n;
      cout<<"this is a positve number"<<num_great<<"\n";
   }
   else
   if (n == 0)
   {
       num_is=n;
       cout<<"You entered zero but zero is nor positve or  negative"<<num_is<<"\n";
   }
}
}
return 0;

}

User is offlineProfile CardPM
+Quote Post

Xing
RE: Determine Which R Postive And Negative Numbers
16 Apr, 2007 - 09:13 PM
Post #3

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 2 times
Dream Kudos: 1575
My Contributions
I rectified visible errors in your code
CODE

#include<iostream>
using namespace std;

int main()

{
    int num_great=0, num_less=0, num_is=0;
    int c=0;
    int n;
    int x;

    while ( c < x )
    {
        cout<<"Enter a number\n";

        cin>>n;
        c++;

        if ( n < 0)
        {
            num_less=n;
            cout<<"this is a negative number"<<num_less<<"\n";
        }

        else if (n > 0)
        {
            num_great=n;
            cout<<"this is a positve number"<<num_great<<"\n";
        }
        else if (n == 0)
        {
            num_is=n;
            cout<<"You entered zero but zero is nor positve or negative"<<num_is<<"\n";
        }
    }
    return 0;
}


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:16PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month