Link to home
Start Free TrialLog in
Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil

asked on

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!
Avatar of ozo
ozo
Flag of United States of America image

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]’
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) )
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Eduardo Fuerte

ASKER

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

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.
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

%*[^\n]\n
is another way to skip to the end of the line
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.).
Infinity08

Thank you for your assistance!