Remote Exploit Forums

Go Back   Remote Exploit Forums > Specialist Topics > Programming


Programming A place for our community to discuss their own security related coding projects.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-28-2009, 07:54 PM
Junior Member
 
Join Date: Jun 2009
Posts: 8
Default Scanf() Problem

Hey I'm learning C and trying to do different problem sets current one that im working on is to take in 3 inputs and then display them out. im not sure what exactly in happening but when i try to put in my 2nd input it skips past the 3rd input and jumps directly to displaying any help in understanding why its not working right is appreciated
Thanks
heres the code

#include"stdio.h"

main(void)
{
int size =0;
char sc1,sc2 ;

// to ensure proper input
do
{
printf("Please enter in size of square no bigger then 24 or smaller then 3:");
scanf("%d",&size);
}while((size >=24) ||(size<=2));


// getting chars for nested sqares
printf("Please enter in one special character:\n");
scanf ("%c" ,&sc1);

printf("Please enter in one more special character\n");
scanf ("%c",&sc2);

//displaying varibles
printf("size %d\n",size ) ; printf("sc1 %c sc2 %c\n",sc1, sc2);

return 0;
}
Reply With Quote
  #2 (permalink)  
Old 08-29-2009, 01:16 AM
Gitsnik's Avatar
Senior Member
 
Join Date: Jun 2009
Location: The Crystal Wind
Posts: 494
Default

Your problem is that scanf is a PoS when it comes to reading numbers. Your safest/sanest bet is to blow it out to two lines, do an fgets into char buffer and then use sscanf to strip your number into your size variable. If my recollection of the function is correct (probably not this early on a saturday), it will look something like:
Code:
fgets(buffer, 5, stdin);
sscanf(buffer, "%d", &size);
Note that you will get some strange looking output when you request your character (an additional line), but fixing that is up to you.
__________________
Never underestimate the power of human stupidity - it is like a force of nature, capable of destroying even the most well laid plans.

Last edited by Gitsnik; 08-29-2009 at 01:18 AM. Reason: typo in fgets code.
Reply With Quote
  #3 (permalink)  
Old 08-30-2009, 05:49 PM
Junior Member
 
Join Date: Jun 2009
Posts: 8
Default

It grabs the number just fine. its the other two chars that its not handling so well. it gets the number then it skips the first request for a special char and goes on to the second one. compile the code and give it a shot to see what happenes
Reply With Quote
  #4 (permalink)  
Old 08-31-2009, 12:23 AM
Senior Member
 
Join Date: Jun 2008
Posts: 358
Default

Quote:
It grabs the number just fine. its the other two chars that its not handling so well. it gets the number then it skips the first request for a special char and goes on to the second one. compile the code and give it a shot to see what happenes
scanf might store it as a string(needs a null at the end), try %s instead of %c
and char sc2[3];
Reply With Quote
  #5 (permalink)  
Old 08-31-2009, 01:12 AM
Gitsnik's Avatar
Senior Member
 
Join Date: Jun 2009
Location: The Crystal Wind
Posts: 494
Default

I am aware that it will grab the number just fine, I am also well aware that scanf is a piece of crap when it comes to doing things like this (not to mention some possible insecurities in its implementation.

Splitting it out how I suggested is saner, faster, neater and makes you write better code. The fact that you are now a couple of days into this should be enough to prove this.
__________________
Never underestimate the power of human stupidity - it is like a force of nature, capable of destroying even the most well laid plans.
Reply With Quote
  #6 (permalink)  
Old 09-02-2009, 04:13 AM
Junior Member
 
Join Date: Jun 2009
Posts: 8
Default

I see that you are correct and after playing with it i get warnings but it works would you be able to tell me if im right as to what I'm thinking the first line fgets(buffer, 5, stdin); gets standard input and places it in to a buffer and im guessing that 5 is the max size that it will accept ?
the second line sscanf(buffer, "%d", &size); this is pretty clear cut but because sscanf grabs input from buffers it puts it in to variables at there memory address that is probably a horrible way to put it but am i correct in my understanding?
Reply With Quote
  #7 (permalink)  
Old 10-03-2009, 07:51 AM
Virchanza's Avatar
Senior Member
 
Join Date: Sep 2008
Location: I am not living
Posts: 728
Default

Here's an explanation of the problem you're having:

Question 12.18a

Here's a better explanation:

http://c-faq.com/stdio/gets_flush2.html
__________________
Ask questions on the open forums, that way everybody benefits from the solution, and everybody can be corrected when they make mistakes. Don't send me private messages asking questions that should be asked on the open forums, I won't respond. I decline all "Friend Requests".

Last edited by Virchanza; 10-03-2009 at 07:55 AM.
Reply With Quote
  #8 (permalink)  
Old 10-05-2009, 01:00 PM
Just burned his ISO
 
Join Date: Jun 2009
Posts: 4
Default

You need to use fflush(stdin); after your scanf() to empty the input buffer
Reply With Quote
  #9 (permalink)  
Old 10-05-2009, 02:17 PM
Virchanza's Avatar
Senior Member
 
Join Date: Sep 2008
Location: I am not living
Posts: 728
Default

Quote:
Originally Posted by BuRner View Post
You need to use fflush(stdin); after your scanf() to empty the input buffer
I'm not going to deny that your solution works on more than one type of computer, but you should just know that the C Standard says flushing input streams is a no-no (to be pedantic, the behaviour is "undefined", meaning your program can crash if it wants to).

Here's a quick discussion of it:

Things to Avoid in C/C++ -- fflush(stdin), Part 2 - GIDNetwork

And here's an alternative way of flushing an input stream that will work on all kinds of computer:

Code:
int ch = 0;

while((ch = getc(fp)) != EOF && ch != '\n')
{
    /* Empty body */;
}
Of course to make it neater you can make a function out of it:

Code:
int FlushInputStream(FILE *const p)
{
     int ch = 0;

     while( (ch = getc(p)) != EOF && ch != '\n')
     {
         /* Empty body */
     }

     return EOF == ch;
}
Then you could call this function after you call scanf.
__________________
Ask questions on the open forums, that way everybody benefits from the solution, and everybody can be corrected when they make mistakes. Don't send me private messages asking questions that should be asked on the open forums, I won't respond. I decline all "Friend Requests".
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On



All times are GMT. The time now is 03:19 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2