Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pressing 'q' or non-numeric char to exit loop/program

Reply
Thread Tools

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

 
 
icarus
Guest
Posts: n/a
 
      05-09-2008
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);


}
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      05-09-2008
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
 
Reply With Quote
 
 
 
 
icarus
Guest
Posts: n/a
 
      05-10-2008
> 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.uk> wrote:
> 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


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-10-2008
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
 
Reply With Quote
 
icarus
Guest
Posts: n/a
 
      05-12-2008
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.com> wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I exit from "getline()" without pressing any key? caoliangbj@gmail.com C++ 3 06-08-2007 12:35 PM
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments