• Log in or sign up for an account here: https://www.sythe.org/login

Beginner needs help with VERY basic C++ program

  • Thread starter Thread starter chunkygnome
  • Start date Start date
C

chunkygnome

Guest
I am just starting C++ and tried to write a program that tells if a number is bigger than another/ equal. It works fine when a number is bigger or smaller, but the "equal" part doesn't work.

#include <stdio.h>

main()
{
int a , b;
do {

printf("First # is ");
scanf("%d",&a);

printf("Second number is ");
scanf("%d",&b);

if (a<b) printf("First number is less than second");
if (b<a) printf("Second number is less than first");
if (b==a) printf("They are equal");
} while (a < 999);
}



Help would be appreciated, as i am getting balder and balder with more hair being torn out.
 
The equality check works fine for me but even so I made a couple adjustments to make it more effective. Since you are taking in integer input you should be using %i instead of %d (for a double variable type). Also, I've changed it from being 3 ifs to an if and 2 else ifs as it will never happen that more than one of them be true they might as well be combined. The last change was adding && b < 999 which tells it to quit the while loop whenever either one of 'a' or 'b' are equal to or greater than 999. Feel free to keep posting with more questions or if you're still having trouble. :)

Code:
#include <stdio.h>

main()
{
int a , b;
do {

printf("First # is ");
scanf("%i",&a);

printf("Second number is ");
scanf("%i",&b);

if (a<b) printf("First number is less than second.\n\n");
else if (b<a) printf("Second number is less than first.\n\n");
else if (b==a) printf("They are equal.\n\n");
} while (a < 999 && b < 999);
}
 
Just in case you didn't know, that looks more like C than C++...
Well, his code (apart from the include) is compatible in both C and C++. I suppose that since he mentions C++ that, that will be the language in which he will progress further.
 
Member of the Month Winner
Sythe's 10th Anniversary
The equality check works fine for me but even so I made a couple adjustments to make it more effective. Since you are taking in integer input you should be using %i instead of %d (for a double variable type). Also, I've changed it from being 3 ifs to an if and 2 else ifs as it will never happen that more than one of them be true they might as well be combined. The last change was adding && b < 999 which tells it to quit the while loop whenever either one of 'a' or 'b' are equal to or greater than 999. Feel free to keep posting with more questions or if you're still having trouble. :)

Code:
#include <stdio.h>

main()
{
int a , b;
do {

printf("First # is ");
scanf("%i",&a);

printf("Second number is ");
scanf("%i",&b);

if (a<b) printf("First number is less than second.\n\n");
else if (b<a) printf("Second number is less than first.\n\n");
else if (b==a) printf("They are equal.\n\n");
} while (a < 999 && b < 999);
}
Well, you learn something new every day.

I always thought 'd' was for decimal.

Anyway, the beauty of C and C++ is they are compatible with each other (assuming you use a C++ compiler, which will compile C anyway). C functions can be used in C++ in any instance, and C++ functions can be used in any C code assuming you do indeed use a C++ compiler, which is quite ingenious.

I would actually rewrite that code so it accepts any non-zero integer, and anything below one causes the program to terminate. It is more effective to do it that way, rather than limiting the user to 999 and below.
 
Well, you learn something new every day.

I always thought 'd' was for decimal.
Probably that's because you learned the basic so fast that you just flew through the simple stuff like that and made an assumption. :P

I would actually rewrite that code so it accepts any non-zero integer, and anything below one causes the program to terminate. It is more effective to do it that way, rather than limiting the user to 999 and below.
Yeah, I'm aware I didn't fix it completely to watch for all types of bad input but I figure I shouldn't do all the work for him anyway. :)
 
The equality check works fine for me but even so I made a couple adjustments to make it more effective. Since you are taking in integer input you should be using %i instead of %d (for a double variable type). Also, I've changed it from being 3 ifs to an if and 2 else ifs as it will never happen that more than one of them be true they might as well be combined. The last change was adding && b < 999 which tells it to quit the while loop whenever either one of 'a' or 'b' are equal to or greater than 999. Feel free to keep posting with more questions or if you're still having trouble. :)

Code:
#include <stdio.h>

main()
{
int a , b;
do {

printf("First # is ");
scanf("%i",&a);

printf("Second number is ");
scanf("%i",&b);

if (a<b) printf("First number is less than second.\n\n");
else if (b<a) printf("Second number is less than first.\n\n");
else if (b==a) printf("They are equal.\n\n");
} while (a < 999 && b < 999);
}
You're letting control reach the end of main(). Add an else and a return.
 
hmm. I have no knowledge of the stdio.h library(i'm assuming it stands for standard i/o)

anyway, I would remove the if on the last else if statement because if a !> b and a !< b then a must equal b. also there is no return statement, it that matters.
 
Runescape Gamblingrunescape gambling
Back
Top