Eduardo Fuerte
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?
Thanks in advance!
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;
}
Thanks in advance!
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_nu mer);
if( (opcao_char[0]=='y' || opcao_char[0]=='Y')&& (opcao_conver==8) )
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_nu
if( (opcao_char[0]=='y' || opcao_char[0]=='Y')&& (opcao_conver==8) )
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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;
}
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.
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.
ASKER
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;
}
%*[^\n]\n
is another way to skip to the end of the line
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.).
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.).
ASKER
Infinity08
Thank you for your assistance!
Thank you for your assistance!
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]’