Beginner needs help with VERY basic C++ program

Discussion in 'Programming General' started by chunkygnome, Apr 14, 2008.

Beginner needs help with VERY basic C++ program
  1. Unread #1 - Apr 14, 2008 at 11:50 PM
  2. chunkygnome
    Referrals:
    0

    chunkygnome Guest

    Beginner needs help with VERY basic C++ program

    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.
     
  3. Unread #2 - Apr 15, 2008 at 8:56 AM
  4. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Beginner needs help with VERY basic C++ program

    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);
    }
     
  5. Unread #3 - Apr 15, 2008 at 10:26 AM
  6. SidStudios
    Joined:
    Mar 14, 2007
    Posts:
    201
    Referrals:
    0
    Sythe Gold:
    0

    SidStudios Active Member

    Beginner needs help with VERY basic C++ program

    Just in case you didn't know, that looks more like C than C++...
     
  7. Unread #4 - Apr 15, 2008 at 4:10 PM
  8. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Beginner needs help with VERY basic C++ program

    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.
     
  9. Unread #5 - Apr 16, 2008 at 2:33 AM
  10. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Beginner needs help with VERY basic C++ program

    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.
     
  11. Unread #6 - Apr 16, 2008 at 8:07 AM
  12. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    Beginner needs help with VERY basic C++ program

    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

    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. :)
     
  13. Unread #7 - Apr 18, 2008 at 5:54 PM
  14. Faskist
    Joined:
    Apr 25, 2005
    Posts:
    1,869
    Referrals:
    0
    Sythe Gold:
    0

    Faskist Tuxhead
    Banned

    Beginner needs help with VERY basic C++ program

    You're letting control reach the end of main(). Add an else and a return.
     
  15. Unread #8 - May 23, 2008 at 5:54 PM
  16. MavFan07
    Joined:
    Dec 18, 2007
    Posts:
    35
    Referrals:
    0
    Sythe Gold:
    0

    MavFan07 Member

    Beginner needs help with VERY basic C++ program

    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.
     
< What programming language do I need to learn. | A Few More Questions >

Users viewing this thread
1 guest


 
 
Adblock breaks this site