0
MyUserID

More C HW Help Please :)

Recommended Posts

Sorry to keep posting my C programming homework woes here, but you guys always do a great job of giving me advice. Anyway, I'm having problem with this function:

int mainmenu(void)
{
int choice;
char name[25];

printf("Please enter a choice.\n");
printf("1. Convert a Temperature\n");
printf("2. Quit the Program\n");

scanf("%d", &choice);

if(choice == 1)
{
printf("Please enter your name:\n");
gets(name);
}

system("cls");
return choice;

}

The problem is that it keeps skipping over the "if" statement. I even made that part a completly different function, and it still skipped it. Wierd thing is that it only skips the gets part. I dont understand!!!
>:(

------------------------------------------------------

Remember kids, eagles may soar, but at least weasels dont get sucked into jet engines.

project5b.c

Share this post


Link to post
Share on other sites
Works for me on my Linux box, using gcc version 2.95.4 20010319 (Debian prerelease).



Welcome to Temperature Converter 1.0.
by Christopher D. Thompson
------------------------------------

Use this program to convert temperatures between
Celcius, Faherenheit, and kelvin.

Please enter a choice.
1. Convert a Temperature
2. Quit the Program
1
Please enter your name:
sh: cls: command not found
Please select a conversion:
---------------------------

1. Fahrenheit to Celcius
2. Fahrenheit to kelvin
3. Celcius to Fahrenheit
4. Celcius to kelvin
5. kelvin to Celcius
6. kelvin to Fahrenheit
7. Return to the main menu.

1

sh: cls: command not found
Please enter the value of the temperature that
you would like to have converted.

Valid entries include:
----------------------
Celcius: -500 to 500
Fahrenheit: -1,000 to 1,000
kelvin: 0 to 800
32
The new temperature is 0.0.

The only hiccup is that since I'm not using DOS, I don't have the 'cls' program, so my screen doesn't clear. There is a way to do that entirely inside of C, however.

Actually, I just noticed that it asks for my name but jumps right ahead without waiting for input. This is because (I think) there is a return still in the input buffer after reading the digit for the menu. gets() sees this, thinks it's gotten it's string already, and shoves null into name. Doubling up the gets(name) fixes it, but there has to be a better way to do this. Besides, my compiler bitches about using gets (it is vulnerable to buffer overflow attacks). Since you don't seem to be using name anyway, I wouldn't worry about it.

Share this post


Link to post
Share on other sites

Dude, you rock!! I would never have come up with that!! Now, I wonder, if there is any command in C to clear that return out rather than doubling up. But hey, it works, so I'm happy. :)


------------------------------------------------------

Remember kids, eagles may soar, but at least weasels dont get sucked into jet engines.

Share this post


Link to post
Share on other sites
Hi there,


Please consider NOT using gets(). It does no bounds checking and could therefore be quite dangerous in a situation where a buffer overflow could lead to an exploit. I know that in this case the use of gets() is not going to cause a problem but in a few years when you're working on something where there may be an issue..... If you never, ever use gets() you'll not run into issues with it.

"C" and its relatives are bastard languages. There's no strong type checking and there is no built in bounds checking. Just about all of the discovered buffer overflow exploits have been found in programmes written in C ( or a relative ).



Anyway. I had a very quick look and I _think_ that gets() is possibly getting an EOF from stdin. Perhaps an fflush(stdin) before the gets() ?? Better to just use either fgets() or scanf().

It works like a charm if you use scanf(). ie.

if(choice == 1)
{
printf("Please enter your name:\n");
scanf('%s", &name);
}

Ooroo
Mark F...

Share this post


Link to post
Share on other sites
Bingo, fflush was the function to clear the input buffer that I couldn't remember.

Note for MyUserID: You also need to fix the part where the temperature is input. I missed that on my first read-through. The way you have it now, the valid ranges are always printed first.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

0