Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 2 votes

How to use scanf in C.

myvariable scanf

  • Please log in to reply
30 replies to this topic

#13 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 30 October 2010 - 05:13 PM

@Nullw0rm, thanks for the feedback! I didn't realise the '/0' was saved and used as the next input.

I am however confused as to why it works with the second code snippet?


The "\0" is not the problem in this case, you can visualize the problem like this.

When you are asking for it to enter a number first, you press 23 then hit enter. The enter button creates a newline character, or "\n", and this is accepted as a char. This means after you press enter, the newline is automatically plugged into your second scanf asking for a "char" and it is skipped completely.

The way of which you can resolve this is to "eat" the newline character in reference to my last solution of using scanf() parameter %*c or using fgets to capture the number and sscanf to store the integer into a variable ignoring the newline char. In C++ you may see a reference to "cin.ignore()" to replicate this behaviour.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#14 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 19 December 2010 - 01:37 AM

getchar(); and gets(); are like scanf()

1) getchar(); takes one symbol. IF i want 2 symbol, I should write getchar(); getchar();
2) gets(); takes full string with spaces. For example: If I write "Vakho Qor". Scanf takes "Vakho Qor"
3) scanf(); takes symbols before space. For example: If I write "Vakho Qor". Scanf takes only "Vakho"
  • 0

#15 AntLaTech

AntLaTech

    CC Regular

  • Just Joined
  • PipPipPip
  • 30 posts

Posted 05 January 2011 - 12:55 PM

Nice post and thank you for it...
  • 0

#16 VakhoQ

VakhoQ

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 126 posts

Posted 23 January 2011 - 06:47 AM

getchar() is also like a scanf() but this gets only one Symbol. this is good function. For example: If we want to count, how many "K" are in our text file , we can write:
freopen ("text.txt", "r", stdin);
char c; int count=0;

while(c!=EOF) {    //  While you Don't Find EOF in the text file make this:
c=getchar();  // read one symbol
if (c=="k") count++;   // if there is "K",  Increase...
} // End when you find EOF, this menas End Of File, its like \n and \r in the text file

  • 0
GNU/Linux Is the Best.

#17 hanemi

hanemi

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 30 January 2011 - 05:12 AM

I have a problem with scanf here.

...
printf("please input x1\n");
scanf("%d, &x1");
fflush(stdin);

printf("please input y1\n");
scanf("%d, &y1");
fflush(stdin);
...

it doesn't work when I run it. everything i key in a number for "x1" i get this in the next line "segmentation fault <core dumped>". is there anything wrong with my code?
  • 0

#18 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 30 January 2011 - 03:16 PM

I have a problem with scanf here.


You extended the quotes around both parameters. scanf("%d, &x1") should be scanf("%d", &x1)
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#19 hanemi

hanemi

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 31 January 2011 - 12:56 AM

thanks!!!

but it still doesn't work. I can't type in whole numbers...
  • 0

#20 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 31 January 2011 - 01:03 AM

but it still doesn't work. I can't type in whole numbers...


What is your full code?
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#21 hanemi

hanemi

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 31 January 2011 - 02:16 AM

eeps. it's ok. i changed %d to %lf it works.
  • 0

#22 Promo2

Promo2

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 12 April 2012 - 06:59 AM

Thanks, good tutorial.

If I wanted to scan a particular part of the input how would this be done?

For example, if i knew a 4 digit number was going to be entered and i wanted to scan only the second digit WITHOUT using the modulus, would this be possible?

I have tried various arrangements such as 2%d, %2d etc and nothing seems to be consistent...
  • 0

#23 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 13 April 2012 - 03:08 PM

Thanks, good tutorial.

If I wanted to scan a particular part of the input how would this be done?

For example, if i knew a 4 digit number was going to be entered and i wanted to scan only the second digit WITHOUT using the modulus, would this be possible?

I have tried various arrangements such as 2%d, %2d etc and nothing seems to be consistent...


You probably will have better luck scanning it in to a string, and calling upon the second number: string[1].

Trying various random arrangements of characters may not be the best way to learn in this situation. Try to read the scanf manual entry where available: http://www.cplusplus...y/cstdio/scanf/
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#24 poffpaul

poffpaul

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts
  • Learning:C, Java, C++, Objective-C

Posted 01 August 2012 - 03:22 PM

Ok, let's see if I can resurrect this post. I have tried various options from this thread to try and fix my issue, but I can't tell. I was following some tutorials to learn C, but I have experience from several years back with C++, and I kinda just jumped ahead. I designed this almost 100% on my own, and I modified it until it worked. But I can't figure out why the commented section at the end doesn't work. Ideas? Is it just impossible to use a char in this manner?

Thanks

(I'm just showing main because there is a lot of other code and I don't believe it is relevant. If necessary, just let me know and I'll throw the whole darn thing up here.)




int main()
{
/* after game ends, does user want to play again? */
int playagain;

/* users choice of game to play */
int input;

/* determine if a new line is needed for menu options */
int firstplay = 0;

do
{
if (firstplay == 0)

/* menu options */
{
printf("Enter 1 to play an addition game.\n");
} else
{
printf("\nEnter 1 to play an addition game.\n");
}
printf("Enter 2 to play a subtraction game.\n"
"Enter 3 to play a multiplication game.\n"
"Enter 4 to play a division game.\n"
"Enter 5 to exit: ");
scanf("%d", &input);

getchar();

/* select proper options based on menu choice of user */
switch(input)
{
case 1:
addgame();
break;
case 2:
subgame();
break;
case 3:
multgame();
break;
case 4:
divgame();
break;
case 5:
printf("\nPlay again soon!\n");
return 0;
default:
printf("You have failed to enter a number\n"
"between 1 and 5. How hard could it be?");
break;
}

/* after a game, offer to play again */
printf("\n\nWould you like to play again?\n"
"Press 1 to play, press 0 to exit: ");
scanf("%d", &playagain);

if(playagain == 0)
{
printf("\nPlay again soon!\n");
}

firstplay = 1;

} while (playagain != 0);

return 0;
}


/* For this whole DO WHILE loop, I initially tried to use this:
(I'm assuming it is clear where each of these lines would occur)

char playagain;

printf("\n\nWould you like to play again? (y/n): ");
scanf("%c", &playagain);

if(playagain != ( "N" || "n" )
{
printf("\nPlay again soon!\n");
}

} while (playagain != ( "N" || "n" );

It seems to me that the problem came at the scanf line...
am I correct? Thanks
*/
  • 0





Also tagged with one or more of these keywords: myvariable, scanf