English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories
6

How do I tell the program that the last number entered was the same.
#include

int
main()
{

int a, b, c, d, e, total;

printf ("Enter a number : "); /*recieve first number*/

scanf ("%d",&a);

if (a%2==0)
{
printf ("The entered number is EVEN.\n");

}
else
{
printf ("The entered number is ODD.\n");

}

printf ("Enter a number : "); /*second number*/

scanf ("%d",&b);

if (b%2==0)
{
printf ("The entered number is EVEN.\n");

}
else
{
printf ("The entered number is ODD.\n");

}
if ("%d=%d", b, a);
{
printf("That is the same number.\n");
}
This is only part of the program. If i enter 3 first than 4 it says I just entered the same number. I think by using 'if' I need to use 'else'.
I want it to tell the user that they entered the same number as the number previous when they actually do.

2007-02-09 08:10:43 · 3 answers · asked by schrickhellcat 1 in Computers & Internet Programming & Design

3 answers

you're incorrectly using the "=" The single "=" means you set a varible to the next.

Since you're using c++ you can use if (a==b). That will compare a and b.

a=b makes the varible a equal to b.

As good programming practice you should give your variables meaningful names. ie. Num1 and Num2 instead of a and b

2007-02-09 08:22:08 · answer #1 · answered by Dennis D 2 · 3 0

In your another post I couldn't find a "answer to this question" button, anyway yes you must use: printf or cout to print out.

In c/c++ for equality comparision you must use "==".
In your code you had written:

if ("%d=%d", b, a);
{
printf("That is the same number.\n");
}

but the correct form is as follows:

if (b==a);
{
printf("a and b are equal.\n");
}

2007-02-09 16:35:11 · answer #2 · answered by Poco Metis 2 · 1 0

The problem is you have a semicolon after your if statement.
if ("%d=%d", b, a);
Also, your if statement doesn't make sense. You need to use:
if(a == b), which means if a equals b. a = b in C will assign a to b, and it will always be true unless b is equal to 0.
That will always terminate the if. You won't need an else statement.

Good luck!

2007-02-09 18:32:20 · answer #3 · answered by Anonymous · 2 0

fedest.com, questions and answers