473,795 Members | 3,333 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help with a While i'm a beginner

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

}

}
Jul 22 '05 #1
8 1268
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();

}

}

Jul 22 '05 #2
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 .
Jul 22 '05 #3
"Karthik Kumar" <ka************ *******@yahoo.c om> 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
Jul 22 '05 #4
Mike Wahler wrote:
"Karthik Kumar" <ka************ *******@yahoo.c om> 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.
Jul 22 '05 #5
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
Jul 22 '05 #6
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
Jul 22 '05 #7
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.
Jul 22 '05 #8
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.

Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3108
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting that Numerology program I wrote years ago in VB. There are times I am totally stuck (for instance, I just had an idea to put the numerical values of the alphabet and months of the year in a dictionary located in a function. Then, I can import the...
8
2012
by: Bshealey786 | last post by:
Okay im doing my final project for my first computer science class(its my major, so it will be my first of many), but anyway im a beginner so im not to great with C++ yet. Anyway this is the error msg that im getting: "Error executing cl.exe" this is the code that I have, but I know whats causing it, ill just show you the whole thing first though //file: Quadratic
3
1549
by: pestul | last post by:
I'm quite new with Access so please bare with me. I'm pretty sure what I'm looking for will require the integration with VB to function properly. Here's what I have: Portfolios for inviduals who have taken part in courses at this department. Information includes: Name (First/Last)
11
2505
by: shekhardeodhar | last post by:
The program compiles properly (most of it is from k&r) but the second function (here character_count) gives wrong answer. Can someone please explain why ? #include<stdio.h> #define IN 1 #define OUT 0 int word_count();
3
2463
by: vijaykokate | last post by:
Our company http://www.softnmation.com/ offers its customers a great variety of products. Everything you need can be found in this site. Web Template, CSS Template, Logo Template, Corporate Identity Package, Logo Set,Icon Set, Flash Animated Template, Flash Intro Header, Flash Site, PHP-Nuke Theme, Full Site, Stretched Web Templates and Stretched Flash Templates, ZenCart Templates ,osCommerce Templates and many more
20
2299
by: weight gain 2000 | last post by:
Hello all! I'm looking for a very good book for an absolute beginner on VB.net or VB 2005 with emphasis on databases. What would you reccommend? Thanks!
2
1937
by: theronnightstar | last post by:
I am writing an anagram program for my fiance. Figured it would be an excellent task to learn from. The way it is supposed to work is it reads in a word list from a file into a temporary vector<string>. From that it selects a random word 6 letters or more long. That is the word of the game - the one to make all the anagrams from. After it selects a word, I initialize two more vector<string>'s - unplayed_anagrams and played_anagrams. I want...
6
2293
crystal2005
by: crystal2005 | last post by:
Hello guys, I'm a beginner in Java application programming. I started to write a Java application in which link to MS Access database. I encountered a problem in deletion function. E.g. I would like to delete one record in database, it always shows "record not found" in my program, even if the data has been deleted. I tried to used function for each choices. But the compiler showed that we can't used function in static void. Is there...
4
2829
by: hussain3047 | last post by:
Hi i am a beginner in C sharp and trying to develop a wince 5.0 device application using C sharp. i am using a thread in the Form1 Load method which is continously running a method named main_program the problem is when i try to display any text on the textbox it shows the exception message as follows. Control.Invoke must be used to interact with controls created on a seperate thread.
0
10436
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.