|
|||||||
| Programming A place for our community to discuss their own security related coding projects. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
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; } |
|
|||
|
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
|
|
|||
|
Quote:
and char sc2[3]; |
|
||||
|
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. |
|
|||
|
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? |
|
||||
|
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. |
|
||||
|
Quote:
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 */;
}
Code:
int FlushInputStream(FILE *const p)
{
int ch = 0;
while( (ch = getc(p)) != EOF && ch != '\n')
{
/* Empty body */
}
return EOF == ch;
}
__________________
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". |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|