954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with simple problem

Hi,
Here's my code:
#include

int main ()
{
int i;

cout << "Please enter a number" << endl;
cin >> i;

if (i < 0)
{ cout << "The number you entered is negative.\n";}

if (i = 0)
{ cout << "The number you entered is zero.\n";}

if (i > 0)
{ cout << "The number you entered is positive.\n";}

else
{ cout << "Thank you for using this program.\n";}

return 0;
}


I just want to have the program print if the number is 0, negative or positive. The code works with no errors but all it does is ask for a number, and when you hit enter it says Thanks...What am I doing wrong? I've been on this for 2 hours! HELP.

LAMJAM
Newbie Poster
15 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

This is assignment:

i = 1


This is comparison:

i == 1


[edit]And these are code tags : [code][/code]
;)

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

what does " if (i=0) " do?

think about that, there's an error on that line

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

what does " if (i=0) " do?

think about that, there's an error on that line

Well, I think it should go to cout << The number you entered is zero. Right??

LAMJAM
Newbie Poster
15 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 
Well, I think it should go to cout << The number you entered is zero. Right??

Is zero true or false?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Thanks! I knew it was something stupid!

LAMJAM
Newbie Poster
15 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

I have another one that's making me crazy. My code so far is...I just need some prodding-please.

#include


void print_value
int global = x;
x = 0;


int main ()

{

int local = y;
y = num?
cin please enter num.


// function incx loop 15 times

incx = x++ //increment x by 1 each time

cout the value of x is x.

LAMJAM
Newbie Poster
15 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

you're not thinking!
What does i=0 do?
So what happens when you do if (i=0) ?
And indeed what is the result if if (i=0) ?

some hints:
assignment, false

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

you're not thinking! What does i=0 do? So what happens when you do if (i=0) ? And indeed what is the result if if (i=0) ?

some hints: assignment, false

Thanks,
I figured that one out. I just had to change it to (i ==0) and it worked.
Now my problem is with the other code...

#include


void print_value
int global = x;
x = 0;


int main ()

{

int local = y;
y = num
cin please enter num.


//function incx loop 15 times

incx = x++ //increment x by 1 each time

cout the value of x is x.

LAMJAM
Newbie Poster
15 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

Okay, that's not really your code is it? Your compiler must be screaming at you. Are you having trouble with the error messages?

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

That's what I have so far. I can't figure it out. And this is my first exposure to C++.

I'm supposed to:
declare global int variable within main named x initialized to 0
declare a local int variable in main named y
prompt user for value of y in main
add loop that calls function incx y number of times
include function incx to increment value of x by one
and print the value of x after each loop

I'm lost!!

LAMJAM
Newbie Poster
15 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

>declare global int variable within main named x initialized to 0

int x = 0;

declare a local int variable in main named y

int main()
{
   int y;
   return 0;
}

>prompt user for value of y in main

You did a prompt in your first post. It was the cout/cin combo.

>add loop that calls function incx y number of times

You'll need a function incx() prototyped or defined before you call it. The function definition will look a lot like main. You'll likely want to use a for loop from 0 to y.

for ( int i = 0; i < y; ++i )
{
   /* loop body */
}


>include function incx to increment value of x by one

Something to put in the loop body.

>and print the value of x after each loop

Something to put in the loop body.

That ought to be enough for you to take another spin at it. And please post your next code attempt within code tags : [code][/code].

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

ps if you have time go to:
http://www.freewebs.com/yb2pls/index.htm , and please sign the guest book

#include

int main ()
{
int i;

cout << "Please enter a number" << endl;
cin >> i;

if (i < 0)
{
cout << "The number you entered is negative.\n";
}

if (i == 0)
{
cout << "The number you entered is zero.\n";
}

if (i > 0)
{
cout << "The number you entered is positive.\n";
}

else
{
cout << "Thank you for using this program.\n";}

return 0;
}

ps if you have time go to:
http://www.freewebs.com/yb2pls/index.htm , and please sign the guest book

yb1pls
Newbie Poster
24 posts since Dec 2004
Reputation Points: 11
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You