Could you point the reason the second scanf doesn't work ?

Hi Experts!

Given the code bellow could you point the reason the second scanf doesn't work ?

What is needed for that?


#include <stdio.h> 
#include <stdlib.h>
#include <cstdio>

int main(int argc, char** argv) 
{

int opcao_conver;

char opcao_char[1],opcao_numer[1];

printf("do you want to your computer explode now?\n");
printf("Y/N?\n");
scanf("%c",&opcao_char);

printf("you said %c, right? Now i need you to put a number 0-9",opcao_char);
scanf("%c",&opcao_numer);

opcao_conver=atoi(opcao_numer);

if( (opcao_char[1]=='y' || opcao_char[1]=='Y')&& (opcao_conver==8) )
{
	printf("your computer will explode in, 5,4,3,2,1\n");
	printf("BOOM");
	
}
else
{
	printf("You are good,FOR NOW!\n");
}
	return 0;
}

Open in new window


Thanks in advance!
Eduardo FuerteDeveloper and AnalystAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
Q_28258967.c:14: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘char (*)[1]’
Q_28258967.c:14: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘char (*)[1]’
Q_28258967.c:16: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’
Q_28258967.c:16: warning: format ‘%c’ expects type ‘int’, but argument 2 has type ‘char *’
Q_28258967.c:17: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘char (*)[1]’
Q_28258967.c:17: warning: format ‘%c’ expects type ‘char *’, but argument 2 has type ‘char (*)[1]’
0
ozoCommented:
scanf("%c",opcao_char);

printf("you said %c, right? Now i need you to put a number 0-9",opcao_char[0]);
scanf("%c",&opcao_numer[0]);

opcao_conver=atoi(opcao_numer);

if( (opcao_char[0]=='y' || opcao_char[0]=='Y')&& (opcao_conver==8) )
0
ozoCommented:
Note also that if you are using cooked mode terminal IO the first scanf will not see anything until you enter a newline, so the printf on line 16 will not print anything until then. And if you had typed anything besides a newline, there will already be another character waiting in the buffer which the second scanf will then see and return immediately.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Eduardo FuerteDeveloper and AnalystAuthor Commented:
Unfortunatelly after the changes you gived me the second scanf has no effect...

#include <stdio.h> 
#include <stdlib.h>
#include <cstdio>

int main(int argc, char** argv) 
{

int opcao_conver;

char opcao_char[1],opcao_numer[1];

printf("do you want to your computer explode now?\n");
printf("Y/N?\n");
scanf("%c",opcao_char);

printf("you said %c, right? Now i need you to put a number 0-9",opcao_char[0]);
scanf("%c",&opcao_numer[0]);

opcao_conver=atoi(opcao_numer);

if( (opcao_char[0]=='y' || opcao_char[0]=='Y')&& (opcao_conver==8) )
{
	printf("your computer will explode in, 5,4,3,2,1\n");
	printf("BOOM");
	
}
else
{
	printf("You are good,FOR NOW!\n");
}
	return 0;
} 

Open in new window

0
ozoCommented:
The second scanf will read the next character after the character read by the first scanf.
If you typed a newline character after you the character that the first scanf read, the second scanf will read that newline character.

Try entering
Y8<newline>
after the Y/N? prompt.
0
Eduardo FuerteDeveloper and AnalystAuthor Commented:
This way runs ok....


#include <stdio.h> 
#include <stdlib.h>
#include <cstdio>

#include <iostream>
using namespace std;

int main(int argc, char** argv) 
{

int opcao_conver;

char opcao_char[1],opcao_numer[1], opcao_aux[1];

printf("do you want to your computer explode now?\n");
printf("Y/N?\n");

//cout <<  endl; 

scanf("%c",&opcao_char);

scanf("%c",opcao_aux);

printf("you said %c, right? Now i need you to put a number 0-9",opcao_char[0]);
scanf("%c",&opcao_numer[0]);

//cout <<  endl; 

opcao_conver=atoi(opcao_numer);

printf("number you said %i, ",opcao_conver);

printf("Letra %c, ",opcao_char[0]);

if( (opcao_char[0]=='y' || opcao_char[0]=='Y')&& (opcao_conver==8) ) 
{
                printf("your computer will explode in, 5,4,3,2,1\n");
                printf("BOOM");
                
}
else
{
                printf("You are good,FOR NOW!\n");
}
                return 0;
}

Open in new window

0
ozoCommented:
%*[^\n]\n
is another way to skip to the end of the line
0
Infinity08Commented:
The proper way to deal with line based input, is to use fgets (http://en.cppreference.com/w/c/io/fgets) to read a line of input into a buffer, and then get whatever you need out of that buffer (using sscanf (http://en.cppreference.com/w/c/io/sscanf) eg.).

Don't manually try to skip newline characters the way you did, because you're only ignoring the problem that way, and it'll come back to bite you later on (when a line break actually consists of two characters \r\n eg.).
0
Eduardo FuerteDeveloper and AnalystAuthor Commented:
Infinity08

Thank you for your assistance!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.