Re: need help with a While i'm a beginner

From: Karthik Kumar (kaykaylance_nospamplz_at_yahoo.com)
Date: 10/06/04


Date: Wed, 06 Oct 2004 12:33:28 -0700

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 .


Relevant Pages

  • Re: Could use some help...:)
    ... cout << endl ... int B::operator (unsigned row, unsigned column) ...
    (comp.lang.cpp)
  • Re: C++ compile error
    ... hello.C: In function `int main': hello.C:5: error: `cout' undeclared hello.C:5: error: (Each undeclared identifier is reported only once for each function it appears in.) ... cout is part of the 'std' namespace, ... I'm totally puzzled by this complex language when I compile my first program. ...
    (freebsd-questions)
  • Re: Multithread heap assertion failure(Continued)
    ... class CTestThread: public CWinThread{ ... heap (I've seen this happen to people who used cout in this fashion). ... int ExitInstance{ ... This does not set a 1ms timeout; ...
    (microsoft.public.vc.mfc)
  • c++ Linux2Windows text file conversion
    ... int uw(string src,string dest); ... cout << " Error opening source\n"; ... string dest; ...
    (comp.lang.cpp)
  • Recursive Function Help (pls) pt 2
    ... Recursive function 1 takes input and displays a sequence of spaces; ... void Ascending (int, int); ... void Descending; ...
    (comp.lang.cpp)