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
    disaper.

    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
    descending.");

    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
    1. Advertising

  2. SKY[net]

    red floyd Guest

    SKY[net] wrote:
    > 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
    > disaper.
    >
    > can you help me?
    >
    >
    > #include <stdio.h>
    > #include <iostream.h>

    1. Use <iostream>, not <iostream.h>. The former is part of the standard.

    2. This code is C code, not C++ code, why are you including iostream?

    > #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
    > descending.");
    >
    > 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);


    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.

    e.g.
    while ((response = getchar()) != EOF && response != '\n')
    /* do nothing */ ;
    response = getchar();

    >
    > }
    >
    > }
     
    red floyd, Oct 6, 2004
    #2
    1. Advertising

  3. SKY[net] wrote:
    > 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
    > disaper.
    >
    > can you help me?
    >
    >


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


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

    #include <cstdio>
    #include <iostream>
    #include <cctype>


    >
    > int reponse, num1, num2, num3;


    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.

    >
    > void main ()


    int main ()

    Check the C++ FAQ as to why it has to be so.

    >
    > {
    >
    > printf ("\n Do you want to compare numbers? (o/n): ");


    Having included iostream already, think about cout instead of
    printf.

    > reponse = getchar ();
    > reponse = tolower (reponse);


    toupper takes a char and you are passing a 'int'.


    >
    > while (reponse == 'o') {


    May be you want a do .. while loop instead of while here.

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


    How about cin , cout here.

    >
    > if (num1 < num2 && num2 < num3)
    > printf("\n ascending.");
    >
    > else if (num1 > num2 && num2 > num3)
    > printf("\n descending.");
    >
    > 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);
    >
    > }


    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.
    http://akktech.blogspot.com .
     
    Karthik Kumar, Oct 6, 2004
    #3
  4. SKY[net]

    Mike Wahler Guest

    "Karthik Kumar" <> wrote in message
    news:41644988$1@darkstar...
    >
    > > reponse = getchar ();
    > > reponse = tolower (reponse);

    >
    > toupper takes a char and you are passing a 'int'.


    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
     
    Mike Wahler, Oct 6, 2004
    #4
  5. Mike Wahler wrote:
    > "Karthik Kumar" <> wrote in message
    > news:41644988$1@darkstar...
    >
    >>>reponse = getchar ();
    >>>reponse = tolower (reponse);

    >>
    >> toupper takes a char and you are passing a 'int'.

    >
    >
    > 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
    >
    >

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

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

    >
    >
    > This is C++ . All the above headers are deprecated.
    >
    > #include <cstdio>
    > #include <iostream>
    > #include <cctype>




    stdio.h and ctype.h are also valid C++98 headers.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
     
    Ioannis Vranos, Oct 6, 2004
    #6
  7. SKY[net]

    Default User Guest

    Karthik Kumar wrote:


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

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



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




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

    Ron Natalie Guest

    SKY[net] wrote:

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

    #include <iostream>
    Why bother including this at all if you aren't going to use it?
    > #include <ctype.h>
    >
    > int reponse, num1, num2, num3;


    Why are these globals?

    >
    > void main ()


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


    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.
    >
    > while (reponse == 'o') {
    >
    > printf ("\n what are the 3 numbers to compare?");
    > scanf ("%d %d %d", &num1, &num2, &num3);


    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?
    > printf ("\n do you want to compare 3 more numbers? (o/n): ");
    > reponse = getchar ();
    > reponse = tolower (reponse);


    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
    #8
  9. SKY[net]

    Ron Natalie Guest

    Karthik Kumar wrote:

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

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


    Which means nothing. Actually, iostream.h isn't deprecated,
    it doesn't exist as far as C++ is concerned.
    >
    >
     
    Ron Natalie, Oct 7, 2004
    #9
    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. David
    Replies:
    1
    Views:
    510
    David
    Jul 19, 2004
  2. =?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:
    621
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  3. Rensjuh
    Replies:
    7
    Views:
    1,008
    Mabden
    Sep 2, 2004
  4. Replies:
    9
    Views:
    463
    Keith Thompson
    Jul 3, 2009
  5. william nelson

    Beginner's Beginner

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

Share This Page