473,503 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pressing 'q' or non-numeric char to exit loop/program

Hi, this is a simple temperature converter (Fahrenheit to Celsius to
Kelvin). Using gcc 4.0.

The user is supposed to enter q or any other non-character to exit the
program.

Problem with this code:
I enter 'q' as soon as the program starts. It hangs.
Then run it again. Enter a number, it executes fine. But when I enter
q, it repeats last number and it doesn't 'pay attention' to the letter
entered.
Any ideas on how can I fix it? thanks in advance.

#include <stdio.h>

const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;

void Temperatures(double f_temp);

int main(void){

double f_temp;
char quit;
while (quit != "q"){
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();
scanf("%lf", &f_temp);
Temperatures(f_temp);
}
printf("\nbye!");
return 0;
}

void Temperatures(double f_temp){

double celsius, kelvin;

celsius = (F_TO_CELS_ONE_EIGHT * f_temp) + F_TO_CELS_THIRTYTWO;
kelvin = celsius + C_TO_KELVIN;

printf("Fahrenheit degrees: %.2f\n", f_temp);
printf("Celsius degrees: %.2f\n", celsius);
printf("Kelvin degrees: %.2f\n", kelvin);
}
Jun 27 '08 #1
11 12147
icarus said:

<snip>
Any ideas on how can I fix it?
Yes.
#include <stdio.h>

const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;

void Temperatures(double f_temp);

int main(void){

double f_temp;
char quit;
int quit; /* getchar returns int, not char */
>

while (quit != "q"){
while(quit != 'q'){

"q" is a string literal. You want to compare with a character, not a
string.
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();
scanf("%lf", &f_temp);
Bad idea. Let's say you enter the characters '2', '1', '2' (boiling point
of water) and a newline character. The first '2' is stored in quit,
leaving '1' and '2' to be converted into 12 which is stored in f_temp.
Now, 12 Fahrenheit is a lot colder than boiling point (in fact, it's 20
below freezing).

I recommend capturing the data as a string and inspecting the first
character. If it's not 'q', hand the string off to sscanf (or, better
still, strtod) for conversion.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #2
icarus wrote, On 09/05/08 21:57:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to
Kelvin). Using gcc 4.0.

The user is supposed to enter q or any other non-character to exit the
program.

Problem with this code:
I enter 'q' as soon as the program starts. It hangs.
Then run it again. Enter a number, it executes fine. But when I enter
q, it repeats last number and it doesn't 'pay attention' to the letter
entered.
Any ideas on how can I fix it? thanks in advance.

#include <stdio.h>

const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;

void Temperatures(double f_temp);

int main(void){

double f_temp;
char quit;
What value does quit have here since it has never been assigned a value?
while (quit != "q"){
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();
So you have read one character, now you go and call scanf whatever the
suer input! What happens to the newline you enter at the end of the line?
scanf("%lf", &f_temp);
<snip>

scanf returns a value, what does it mean?

You need to start by thinking about how user input works, it looks like
you did not think about it before writing this. I would suggest that
using fgets for input and then checking and using what was entered.
--
Flash Gordon
Jun 27 '08 #3
You need to start by thinking about how user input works, it looks like
you did not think about it before writing this. I would suggest that
using fgets for input and then checking and using what was entered.
--
Flash Gordon
yeah yeah yeah flash gordon, spare me the lecture.

if you are so thoughtful,
why don't you elaborate a little more on your possible solution then?


On May 9, 11:48 am, Flash Gordon <s...@flash-gordon.me.ukwrote:
icarus wrote, On 09/05/08 21:57:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to
Kelvin). Using gcc 4.0.
The user is supposed to enter q or any other non-character to exit the
program.
Problem with this code:
I enter 'q' as soon as the program starts. It hangs.
Then run it again. Enter a number, it executes fine. But when I enter
q, it repeats last number and it doesn't 'pay attention' to the letter
entered.
Any ideas on how can I fix it? thanks in advance.
#include <stdio.h>
const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;
void Temperatures(double f_temp);
int main(void){
double f_temp;
char quit;

What value does quit have here since it has never been assigned a value?
while (quit != "q"){
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();

So you have read one character, now you go and call scanf whatever the
suer input! What happens to the newline you enter at the end of the line?
scanf("%lf", &f_temp);

<snip>

scanf returns a value, what does it mean?

You need to start by thinking about how user input works, it looks like
you did not think about it before writing this. I would suggest that
using fgets for input and then checking and using what was entered.
--
Flash Gordon
Jun 27 '08 #4
icarus wrote:
>
>You need to start by thinking about how user input works, it
looks like you did not think about it before writing this. I
would suggest that using fgets for input and then checking and
using what was entered.

yeah yeah yeah flash gordon, spare me the lecture. if you are so
thoughtful, why don't you elaborate a little more on your possible
solution then?
PLONK. I won't be seeing you.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
** Posted from http://www.teranews.com **
Jun 27 '08 #5
icarus said:
>You need to start by thinking about how user input works, it looks like
you did not think about it before writing this. I would suggest that
using fgets for input and then checking and using what was entered.
--
Flash Gordon

yeah yeah yeah flash gordon, spare me the lecture.
I didn't realise you don't like lectures. From now on, I shall spare you
lectures as well.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #6
icarus wrote, On 10/05/08 01:04:
>You need to start by thinking about how user input works, it looks like
you did not think about it before writing this. I would suggest that
using fgets for input and then checking and using what was entered.
--
Flash Gordon

yeah yeah yeah flash gordon, spare me the lecture.

if you are so thoughtful,
why don't you elaborate a little more on your possible solution then?
<snip>

Because you need to learn to solve these problems yourself rather than
being spoon fed. The final sentence you quote should be enough for you
to solve your problem and thinking about the questions I asked should
allow you to see what is wrong with your current code.
--
Flash Gordon
Jun 27 '08 #7
icarus wrote:
Hi, this is a simple temperature converter (Fahrenheit to Celsius to
Kelvin). Using gcc 4.0.

The user is supposed to enter q or any other non-character to exit the
program.

Problem with this code:
I enter 'q' as soon as the program starts. It hangs.
Then run it again. Enter a number, it executes fine. But when I enter
q, it repeats last number and it doesn't 'pay attention' to the letter
entered.
Any ideas on how can I fix it? thanks in advance.

#include <stdio.h>

const double F_TO_CELS_ONE_EIGHT = 1.8;
const double F_TO_CELS_THIRTYTWO = 32.0;
const double C_TO_KELVIN = 273.16;

void Temperatures(double f_temp);

int main(void){

double f_temp;
char quit;
while (quit != "q"){
printf("Enter temperature in Fahrenheit (q to quit):
");
quit = getchar();
scanf("%lf", &f_temp);
Temperatures(f_temp);
}
printf("\nbye!");
return 0;
}

void Temperatures(double f_temp){

double celsius, kelvin;

celsius = (F_TO_CELS_ONE_EIGHT * f_temp) + F_TO_CELS_THIRTYTWO;
kelvin = celsius + C_TO_KELVIN;

printf("Fahrenheit degrees: %.2f\n", f_temp);
printf("Celsius degrees: %.2f\n", celsius);
printf("Kelvin degrees: %.2f\n", kelvin);
}
/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

#define F_TO_CELS_RATIO ((100 - 0) / (212.0 - 32))
#define F_TO_CELS_OFFSET (-32.0)
#define C_TO_KELVIN_OFFSET 273.16

void Temperatures(double f_temp);
int get_line(char **lineptr, size_t *n, FILE *stream);

int main(void)
{
double f_temp;
char **quit;
int rc;
char *buff = NULL;
size_t size = 0;

fputs("Enter temperature in Fahrenheit (q to quit):", stdout);
fflush(stdout);
while((rc = get_line(&buff, &size, stdin)) 0) {
f_temp = strtod(buff, quit);
if (*quit == buff) {
break;
}
Temperatures(f_temp);
printf("Enter temperature in Fahrenheit (q to quit):");
fflush(stdout);
}
puts("\nbye!");
return 0;
}

void Temperatures(double f_temp)
{
double celsius, kelvin;

celsius = F_TO_CELS_RATIO * (f_temp + F_TO_CELS_OFFSET);
kelvin = celsius + C_TO_KELVIN_OFFSET;
printf("Fahrenheit degrees: %.2f\n", f_temp);
printf("Celsius degrees: %.2f\n", celsius);
printf("Kelvin degrees: %.2f\n", kelvin);
}

int get_line(char **lineptr, size_t *n, FILE *stream)
{
int rc;
void *p;
size_t count;

count = 0;
while ((rc = getc(stream)) != EOF
|| !feof(stream) && !ferror(stream))
{
++count;
if (count == (size_t)-2) {
if (rc != '\n') {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
}
break;
}
if (count + 2 *n) {
p = realloc(*lineptr, count + 2);
if (p == NULL) {
if (*n count) {
if (rc != '\n') {
(*lineptr)[count] = '\0';
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
}
} else {
if (*n != 0) {
**lineptr = '\0';
}
ungetc(rc, stream);
}
count = 0;
break;
}
*lineptr = p;
*n = count + 2;
}
if (rc != '\n') {
(*lineptr)[count - 1] = (char)rc;
} else {
(*lineptr)[count - 1] = '\0';
break;
}
}
if (rc != EOF || !feof(stream) && !ferror(stream)) {
rc = INT_MAX count ? count : INT_MAX;
} else {
if (*n count) {
(*lineptr)[count] = '\0';
}
}
return rc;
}

/* END new.c */
--
pete
Jun 27 '08 #8
pete wrote:
char **quit;
f_temp = strtod(buff, quit);
if (*quit == buff)
Thats' wrong.
It should be

char *quit = NULL;

f_temp = strtod(buff, &quit);
if (quit == buff)

instead.
--
pete
Jun 27 '08 #9
Thanks Pete, you da man!! you've outdone yourself mate...

I'll tweak it around to keep on learning C. I appreciate it.

~~
Richard Heathfield, I liked your first message, where I could learn
something mate.
I got the bad vibe from "Flash Gordon", not you mate. I believe I
stated that on the opening paragraph.

I truly perceived a condescending tone with that fellow. And that
won't be tolerated.
That was confirmed by his second reply.

What is this? do we need to start being pushovers now? worship and
respecting bullies because they know more?

If I got on his nerves for asking a newbie question, then he should
have ignored my lousy post and move on.
I think a message something like .."you can use this and that
function, here's a quick example: " that would have been plenty and
that's all I was asking really.

Now I'm done. Again, thanks for your first message. That got me going
on the right direction.
I hope this clarifies a little. If not, too bad.

On May 10, 3:48 am, pete <pfil...@mindspring.comwrote:
pete wrote:
char **quit;
f_temp = strtod(buff, quit);
if (*quit == buff)

Thats' wrong.
It should be

char *quit = NULL;

f_temp = strtod(buff, &quit);
if (quit == buff)

instead.

--
pete
Jun 27 '08 #10
icarus wrote:
[...]
I got the bad vibe from "Flash Gordon", not you mate. I believe I
stated that on the opening paragraph.

I truly perceived a condescending tone with that fellow. And that
won't be tolerated.
That was confirmed by his second reply.
He spotted a number of errors in your code, hinted at
ways to remedy them, and finished by recommending a different
(and superior) approach altogether. What in the world are you
complaining about?

May the wax in your wings melt and dribble.

--
Er*********@sun.com
Jun 27 '08 #11
icarus wrote:
Thanks Pete, you da man!! you've outdone yourself mate...
You're welcome.

If you have questions about the code
I'll answer them as best as I can.

--
pete
Jun 27 '08 #12

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

Similar topics

4
2540
by: Javier Gomez | last post by:
I have a query field which is a non editable field because is a function's result. Due to I need to edit the information, Can you please suggest me any solution for this problem?? (No matter...
2
7148
by: Cindy | last post by:
Hi all you smarties out there, I'm having a little conundrum with my asp.net page Scenario: I have a form (asp.net) with no code behind (as yet). I have placed a javascript function on a...
4
3858
by: roxanaislam | last post by:
Submitting Form by pressing the "ENTER" key -------------------------------------------------------------------------------- Hi: I have a search form in my application which has 4 dropdown...
9
2943
by: Camellia | last post by:
Hi all, I'll get straight into it. When I try to run the code: ..... while (scanf("%c", &c) == 1) printf("%c", c); ..... I input "abcd" follows by an EOF(Ctrl + d) instead of pressing...
2
2255
Saghar
by: Saghar | last post by:
Hi, Could somebody help me about this topic? I want to know how we can call and execute a non-java source file in a java application. For example, a simple Java application is run, there is an...
18
4582
by: Zytan | last post by:
I want the same function to be run whether you press Enter or double click the listbox. It seems really verbose to write both handlers to both events everytime, even if they both call the same...
1
2475
by: GlennMBella2 | last post by:
this is probably basic, however I ahve a form with number of buttons and upon pressing certain buttons as part of hte buttons press function, i would like to update a table (communications) with...
2
3328
by: Astrix | last post by:
I have been working on session timeout for an application and I got a lot of help from this discussion Board. My Thanks to all of you. For e.g If i have a website which has multiple pages, I...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
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...
1
6988
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...
0
7456
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...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
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...
0
1510
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 ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.