need help with a While i'm a beginner

S

SKY[net]

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

}

}
 
R

red floyd

SKY said:
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>
not <iostream.h>. The former is part of the standard. said:
#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();
 
K

Karthik Kumar

SKY said:
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>
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 ?
 
M

Mike Wahler

Karthik Kumar said:
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
 
K

Karthik Kumar

Mike said:
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.
 
I

Ioannis Vranos

Karthik said:
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.
 
R

Ron Natalie

SKY said:
#include <stdio.h>
#include <iostream.h>
#include said:
#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.
 
R

Ron Natalie

Karthik said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top