beginner. Need HELP!!!!

Discussion in 'C++' started by SKY[net], Oct 6, 2004.

  1. SKY[net]

    SKY[net] Guest

    Hi, I need some help. I'm a beginner programmer.

    I need to do a comparison between 3 numbers and tell the user if it's
    ascendinf, descending, equal or in no ordre. and ask if the user wants
    to compare 3 more numbers. Here's what i've done.

    that code only asks me the 3 numbers. It closes right after that. I
    don't know if he does the comparison. Do you think I need a while to
    ask the question if the user wants to compare 3 other numbers?


    #include <stdio.h>

    int num1, num2, num3;

    void main ()

    {

    printf ("\n What are the 3 numbers to compare?");
    scanf ("%s", &num1, &num2, &num3);

    if (num1 < num2)
    if (num2 < num3)
    printf ("\n ascending");

    if (num1 < num2)
    if (num2 > num3)
    printf ("\n no order");

    if (num1 > num2)
    if (num2 > num3)
    printf ("\n descending");

    if (num1 > num2)
    if (num2 < num3)
    printf ("\n no order");

    if (num1 == num2)
    if (num2 == num3) ¸
    printf ("\n equals");

    if (num1 == num2)
    if (num2 != num3)
    printf ("\n no order");

    }
    SKY[net], Oct 6, 2004
    #1
    1. Advertising

  2. "SKY[net]" <> wrote...
    > Hi, I need some help. I'm a beginner programmer.
    > [..]
    > scanf ("%s", &num1, &num2, &num3);


    You need to understand how 'scanf' works. Every variable you put
    in the list _after_ the format, needs to have a corresponding field
    in the format. In your case, if you're entering three _integers_,
    you need to give three format fields: "%d %d %d".

    And, in general, don't use 'scanf' unless you have to. Use 'cin'
    and >>

    std::cin >> num1 >> num2 >> num3;

    No format to fix, no big deal. Don't forget to #include <iostream>

    And find a good C++ book. Make sure it doesn't have 'void main'
    in it. Writing 'void main' is embarrassing even for a beginner.

    > [..]


    Victor
    Victor Bazarov, Oct 6, 2004
    #2
    1. Advertising

  3. SKY[net]

    Mabden Guest

    "SKY[net]" <> wrote in message
    news:...
    > Hi, I need some help. I'm a beginner programmer.
    >
    > I need to do a comparison between 3 numbers and tell the user if it's
    > ascendinf, descending, equal or in no ordre. and ask if the user wants
    > to compare 3 more numbers. Here's what i've done.
    >
    > that code only asks me the 3 numbers. It closes right after that. I
    > don't know if he does the comparison. Do you think I need a while to
    > ask the question if the user wants to compare 3 other numbers?
    >


    The code isn't very good, but your main problem is that you don't have a
    loop. It will only ask again if you use some kind of looping like
    "while". Then you will need a way for the user to stop the loop, so
    something like "Enter X to exit" in the prompt (the printf) and a test
    of the first input variable.

    The reason it "closes right after that" is probably because you are
    running the program without opening a command window (or DOS box). Try
    opening a command window and running the program from the command line,
    and you should see the output, and the box will stay open so you can run
    it again.

    I realize it must be frustrating to run into all these problem right
    from the start, but keep at it and you'll get the hang of it.

    --
    Mabden
    Mabden, Oct 6, 2004
    #3
  4. SKY[net]

    DaKoadMunky Guest

    >Writing 'void main' is embarrassing even for a beginner.

    Of all the things one can know to make oneself a productive C++ programmer is
    this really that important?

    Sure, the smallest well-formed program looks like this...

    int main()
    {
    return 0;
    }

    ....but would you give somebody the thumbs down because they had been led to
    believe that void was an acceptable return from main either because of an
    incorrect textbook or a compiler that accepts it?
    DaKoadMunky, Oct 6, 2004
    #4
  5. "DaKoadMunky" <> wrote in message
    news:...
    > >Writing 'void main' is embarrassing even for a beginner.

    >
    > Of all the things one can know to make oneself a productive C++ programmer
    > is
    > this really that important?
    >
    > Sure, the smallest well-formed program looks like this...
    >
    > int main()
    > {
    > return 0;
    > }
    >


    Gratuitous nitpick; the smallest well formed program is

    int main()
    {
    }

    Return is optional in main, even though int is compulsory. Crazy.

    john
    John Harrison, Oct 6, 2004
    #5
  6. "SKY[net]" <> wrote in message
    news:...
    > Hi, I need some help. I'm a beginne rprogrammer.
    >
    > I need to do a comparison between 3 numbers and tell the user if it's
    > ascendinf, descending, equal or in no ordre. and ask if the user wants
    > to compare 3 more numbers. Here's what i've done.
    >
    > that code only asks me the 3 numbers. It closes right after that. I
    > don't know if he does the comparison. Do you think I need a while to
    > ask the question if the user wants to compare 3 other numbers?
    >
    >
    > #include <stdio.h>
    >
    > int num1, num2, num3;
    >
    > void main ()
    >
    > {
    >
    > printf ("\n What are the 3 numbers to compare?");
    > scanf ("%s", &num1, &num2, &num3);
    >
    > if (num1 < num2)
    > if (num2 < num3)
    > printf ("\n ascending");
    >
    > if (num1 < num2)
    > if (num2 > num3)
    > printf ("\n no order");
    >
    > if (num1 > num2)
    > if (num2 > num3)
    > printf ("\n descending");
    >
    > if (num1 > num2)
    > if (num2 < num3)
    > printf ("\n no order");
    >
    > if (num1 == num2)
    > if (num2 == num3) ¸
    > printf ("\n equals");
    >
    > if (num1 == num2)
    > if (num2 != num3)
    > printf ("\n no order");
    >
    > }


    As well as the other problems (lack of while loop and incorrect scanf) your
    logic could be improved. I don't think you have got all the possibilities,
    suppose num1 < num2 but num2 == num3, then your program will print nothing.

    The way to do this is with if ... else

    if (numbers are ascending)
    printf("ascending");
    else if (numbers are descending)
    printf("descending");
    else
    printf("no order");

    See how simple that is. It simply cannot be wrong. Now all you have to do if
    work out the correct expression for 'numbers are ascending' and 'numbers are
    descending'. 'Numbers are ascending' is easy enough, just do this 'num1 <
    num2 && num2 < num3'. You can work out the numbers are descending bit. Then
    you will have a program that it simpler than your original, and more
    correct.

    It is very important to forget about a while loop for now, get it working
    properly with one question first, then add the while loop later.

    john
    John Harrison, Oct 6, 2004
    #6
  7. SKY[net]

    Mike Wahler Guest

    "DaKoadMunky" <> wrote in message
    news:...
    > >Writing 'void main' is embarrassing even for a beginner.

    >
    > Of all the things one can know to make oneself a productive C++ programmer

    is
    > this really that important?


    Of course. A productive programmer is expected to write
    programs that work correctly. A C++ program which defines
    'main()' to return void, has absolutely no guarantee that
    it will work correctly, or at all.

    If I drive my car out of my driveway onto my (extremely
    low traffic) street without pausing to look both ways,
    chances are very good that nothing bad will happen.
    But if I do look first, I am guaranteed the ability
    to avoid driving into another car. I always look.
    I always define 'main()' as returning 'int'.

    > Sure, the smallest well-formed program looks like this...
    >
    > int main()
    > {
    > return 0;
    > }
    >
    > ...but would you give somebody the thumbs down because they had been led

    to
    > believe that void was an acceptable return from main either because of an
    > incorrect textbook or a compiler that accepts it?


    I wouldn't give him 'thumbs down', but I'd attempt to educate him.

    -Mike
    Mike Wahler, Oct 6, 2004
    #7
  8. SKY[net]

    J. Campbell Guest

    "John Harrison" <> wrote in message news:<>...
    > The way to do this is with if ... else
    >
    > if (numbers are ascending)
    > printf("ascending");
    > else if (numbers are descending)
    > printf("descending");
    > else
    > printf("no order");
    >
    > See how simple that is.
    > john


    What's wrong with an doing it using the oft-maligned-yet-ever-so-clear
    ?: operator rather than the ever-so-passe if operator?

    #include<iostream>

    int main(){

    std::cout << "Enter 3 numbers: ";
    int x , y, z;
    std::cin >> x >> y >> z;

    std::cout << "The numbers are in " <<
    (x < y ? y >= z ? "no":"ascending": y <= z ? "no":"decending")
    << " order" << std::endl;
    }
    ;-)
    J. Campbell, Oct 6, 2004
    #8
  9. SKY[net]

    Mike Wahler Guest

    "J. Campbell" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message

    news:<>...
    > > The way to do this is with if ... else
    > >
    > > if (numbers are ascending)
    > > printf("ascending");
    > > else if (numbers are descending)
    > > printf("descending");
    > > else
    > > printf("no order");
    > >
    > > See how simple that is.
    > > john

    >
    > What's wrong with an doing it using the oft-maligned-yet-ever-so-clear
    > ?: operator


    The '?:' operator appears 'clear' to some, not so to others.
    Also, its clarity often depends upon context.

    >rather than the ever-so-passe if operator?


    'if' is not an operator, it's a keyword. Your labeling of it
    as 'passe' just indicates to me that you place 'cleverness'
    above clarity. 'if' is one of most clear constructs in the
    language, as are e.g. 'while' and 'goto'. Even a nonprogrammer,
    upon seeing 'if' in source code, can deduce that it represents
    a decision point in the logic.


    >
    > #include<iostream>
    >
    > int main(){
    >
    > std::cout << "Enter 3 numbers: ";
    > int x , y, z;
    > std::cin >> x >> y >> z;
    >
    > std::cout << "The numbers are in " <<
    > (x < y ? y >= z ? "no":"ascending": y <= z ? "no":"decending")
    > << " order" << std::endl;


    I submit that the above expressed with 'if/else' is much more quickly
    grasped by most folks than with '?:' Especially since you've used
    a 'nested' '?:'.

    -Mike
    Mike Wahler, Oct 6, 2004
    #9
  10. SKY[net]

    Joe C Guest

    "Mike Wahler" <> wrote in message
    news:02Y8d.7689$...

    >> What's wrong with an doing it using the oft-maligned-yet-ever-so-clear
    >> ?: operator

    >
    > The '?:' operator appears 'clear' to some, not so to others.
    > Also, its clarity often depends upon context.
    >
    >>rather than the ever-so-passe if operator?

    >
    > 'if' is not an operator, it's a keyword. Your labeling of it
    > as 'passe' just indicates to me that you place 'cleverness'
    > above clarity.
    > -Mike


    I was joking :-/

    -Joe
    Joe C, Oct 6, 2004
    #10
  11. SKY[net]

    Ron Natalie Guest

    Mike Wahler wrote:

    >
    > Of course. A productive programmer is expected to write
    > programs that work correctly. A C++ program which defines
    > 'main()' to return void, has absolutely no guarantee that
    > it will work correctly, or at all.


    Actually, any compiler which fails to issue a diagnostic, for
    such is defective. The standard is clear. This isn't undefined
    behavior, the program is ill-formed.


    >
    >>Sure, the smallest well-formed program looks like this...
    >>
    >>int main()
    >>{
    >> return 0;
    >>}

    >

    Actually the smallest well-formed program is
    int main() { }

    Main gets a stupid dispensation on return values.
    Ron Natalie, Oct 6, 2004
    #11
  12. SKY[net]

    Mike Wahler Guest

    "Ron Natalie" <> wrote in message
    news:41645b9c$0$28338$...
    > Mike Wahler wrote:
    >
    > >
    > > Of course. A productive programmer is expected to write
    > > programs that work correctly. A C++ program which defines
    > > 'main()' to return void, has absolutely no guarantee that
    > > it will work correctly, or at all.

    >
    > Actually, any compiler which fails to issue a diagnostic, for
    > such is defective. The standard is clear. This isn't undefined
    > behavior, the program is ill-formed.


    Yes. But is an ill-formed program guaranteed to work correctly? :)

    -Mike
    Mike Wahler, Oct 7, 2004
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    574
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Kristin

    Beginner Java...need help!

    Kristin, Mar 21, 2006, in forum: Java
    Replies:
    5
    Views:
    495
    Bonney Armstrong
    Mar 22, 2006
  3. Rensjuh
    Replies:
    7
    Views:
    964
    Mabden
    Sep 2, 2004
  4. SKY[net]
    Replies:
    8
    Views:
    373
    Ron Natalie
    Oct 7, 2004
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    214
    7stud --
    Apr 12, 2011
Loading...

Share This Page