need help with a While i'm a beginner

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

  1. SKY[net]

    SKY[net] Guest

    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.

    I know that the comparison works. but now, my problem is that once i
    enter the 3 numbers i see him giving me the answer, but the screen

    can you help me?

    #include <stdio.h>
    #include <iostream.h>
    #include <ctype.h>

    int reponse, num1, num2, num3;

    void main ()


    printf ("\n Do you want to compare numbers? (o/n): ");
    reponse = getchar ();
    reponse = tolower (reponse);

    while (reponse == 'o') {

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

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

    else if (num1 > num2 && num2 > num3) printf("\n

    else if (num1 == num2 && num2 == num3)
    printf("\n equals.");

    else printf("\n no order.");

    printf ("\n do you want to compare 3 more numbers? (o/n): ");
    reponse = getchar ();
    reponse = tolower (reponse);


    SKY[net], Oct 6, 2004
    1. Advertisements

  2. SKY[net]

    red floyd Guest

    Your problem is here. There is still a '\n' pending on stdin after you
    have done your scanf() above. You need to flush to the end of line
    before doing your printf/response.

    while ((response = getchar()) != EOF && response != '\n')
    /* do nothing */ ;
    response = getchar();
    red floyd, Oct 6, 2004
    1. Advertisements

  3. This is C++ . All the above headers are deprecated.

    #include <cstdio>
    #include <iostream>
    Unless there is a compelling reason to have them, avoid globals.
    At least, in this case they can be local variables of function
    main to get your job done.
    int main ()

    Check the C++ FAQ as to why it has to be so.
    Having included iostream already, think about cout instead of
    toupper takes a char and you are passing a 'int'.

    May be you want a do .. while loop instead of while here.
    How about cin , cout here.
    If you are not able to view the output and that is your concern,
    most probably you are running from a GUI-based editor interface
    and the process gets killed after that.

    Try running it from the cmd-line *or*

    have a statement something like -

    system("pause"); // you may have to include <cstdlib> for this.

    just before you end the program.

    And a test case to your program -
    What happens if enter as 12 12 23 ?
    or 23 23 12 , for that matter ?
    Karthik Kumar, Oct 6, 2004
  4. SKY[net]

    Mike Wahler Guest

    1. OP is callling 'tolower()', not 'toupper()'.

    2. Both 'tolower()' and 'toupper()' have a single
    parameter of type 'int', and both return type 'int'.

    Mike Wahler, Oct 6, 2004
  5. Oops !! Sorry about that .
    Thanks Mike for the clarifying that.
    Karthik Kumar, Oct 6, 2004

  6. stdio.h and ctype.h are also valid C++98 headers.
    Ioannis Vranos, Oct 6, 2004
  7. SKY[net]

    Default User Guest

    Karthik Kumar wrote:

    Incorrect. <iostream.h> is nonstandard, not deprecated.

    Brian Rodenborn
    Default User, Oct 6, 2004
  8. SKY[net]

    Ron Natalie Guest

    Why are these globals?
    int main()
    While there is a level of system dependence to things, in general you do
    not get input from the keyboard via the standard input until a new line
    is entered.
    And what happens if the scanf fails? You don't bother to check.
    Why don't you print the num1, num2, and num3 to make sure you are
    getting them?
    The problem with using getchar here is that you don't know what you're
    getting. It could be data not matched by the previous scanf (most
    likely is). In which case it's not equal to 'o' and your program exits.

    While I'd first suggest if you are trying to learn C++ use C++
    constructs. Read up on iostream. However, you'd have the same
    problems there, just with different interfaces.

    1. You should read a whole line (try fgets() rather than getchar() and
    add the newline to the scanf) when getting each of these inputs.
    Ron Natalie, Oct 7, 2004
  9. SKY[net]

    Ron Natalie Guest

    Which means nothing. Actually, iostream.h isn't deprecated,
    it doesn't exist as far as C++ is concerned.
    Ron Natalie, Oct 7, 2004
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.