I keep getting the wrong answer...I have a homework problem I have been trying to understand but have resulted in a headached.

Using the code shown below, select the correct output for an input of -1:

Input Number
If Number < 0 Then
Write “1”
Else 	
	If Number ==0 Then 
		Write “2”
              Else 
		Write “3”
End If
End If

Here is my code

#include <iostream>
using namespace std;

int main ()
{
Input Number = -1
if Number < 0 Then
Write “1”
Else	
	if Number ==0 Then 
		Write “2”
              Else 
		Write “3”
End if
End if

This C++ is all new to me but I keep getting 1 2 3 4 5 6 as my answer. The answer should be one of the following: 1 2 3 or -1

Any help would be appreciated!

Ask yourself these questions...How are you getting repeated numbers without a loop? What makes your program run through a second time? Pretend you are the computer executing your program, write out what the values are at each step.

I just started the class...so I am a bit lost.

Anything other suggestions?

Number is equal to -1 on line 6.

Is the statement on line 7 true or false?

What happens if the statement on line 7 is true? What happens if it is false?

(if you can't answer the above questions, reconsult your textbook)

What happens once things reach line 15?

Part of the problem is that you are not dealing with actual code, so there's a lot of extraneous stuff like the end if statements that don't actually occur in this language.

I know you want the answer, but it's best to fight with it a bit so you learn something.

According to your code, it is more like VHDL style for implementing C++.

You will not be able to solve this problem if you don't use the right syntax.

Let me tell you, problem is really easy, for a normal programmer it can be done in 2-5 mins.

But it is best for you to try it, From pseudo code it looks like you know what are the program requirements, you just have to use the right syntax.

:)

hint:
if(A==B)
{
cout<<"1"<<endl;
}
else if(A!=B)
{
cout<<"2"<<endl;
}

See thats half of your program already.

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.