Results 1 to 5 of 5
Hi, I'm quite a beginner in C++.I am working in QtCreator.I have made a program which co-operates with some .sh executable files. I have a problem when the scanf command ...
- 02-12-2010 #1Just Joined!
- Join Date
- Feb 2010
- Posts
- 3
[SOLVED] Problem with scanf--->SemiBeginner
Hi, I'm quite a beginner in C++.I am working in QtCreator.I have made a program which co-operates with some .sh executable files. I have a problem when the scanf command takes a character and not a number.Well, to make you understand better, this is my program:
Well, as you can see the program when being run, shows a menu which allows you to choose what you want to do.The problem is that when somebody gives a character (for example "a" or "b" or "school" or anything else) and no a number the program goes mad and starts the menu again and again till I push Ctrl+CCode:#include <stdio.h> #include <stdlib.h> int main() { int a; while (1){ printf("If you are not root, please become...\n\n 1 = What's my mac? 7 = Start aircrack-ng and hack them all\n 2 = Change my mac 8 = Send DeAth to broadcast\n 3 = Start capturing IVS 9 = See available modems\n 4 = Start Reading files 10 = What's my interface? \n 5 = Stop interface 11 = About this prog \n 6 = Start interface 12 = Exit \n"); scanf("%d",&a); if (a==1) system("/etc/macchanger1.sh"); else if (a==2) system("/etc/macchanger2.sh"); else if (a==3) system("/etc/dumpcapt.sh"); else if (a==4) system("/etc/aireplayread.sh"); else if (a==5) system("/etc/airmonstop.sh"); else if (a==6) system("/etc/airmonstart.sh"); else if (a==7) system("/etc/aircrack.sh"); else if (a==8) system("/etc/aireplaysenddeath.sh"); else if (a==9) system("/etc/airodump-ng.sh"); else if (a==10) system("/etc/airmon-ng.sh"); else if (a==11) printf(" This prog was written on February 2010 with purpose to make your life easier\n in hacking yours WEP modems...Cheers!!!\n Note that you must have installed macchanger and aircrack-ng \n"); else if (a==12) break; else if (a<=1 || a>=12) printf("Please type a number from 1 to 12\n"); else {printf("ERROR\n"); break;} } return 0; }
I have put the last "else" in order to avoid this problem with the thought that this "else" would include all the characters (because I have already included all the numbers on what to do so the thing that remains are the characters) and so, if somebody gave a character the the ERROR message would be shown and then the program would exit (because of the break; after the printf) something that never happens....
Can you please help me solve this problem? Thanks
- 02-12-2010 #2Just Joined!
- Join Date
- Feb 2010
- Posts
- 18
Hi,
the line:
scanf("%d",&a);
read a digit: if you insert a letter, it gives error!
you should check, something like
if (scanf("%d",&a) == 1) {
do my thing
} else {
you insert a letter
}
see the manual for scanf behaviour (it returns the decoded input: 1
if it decodes an integer, 0 if it see a letter)
bye
giammyLast edited by oz; 02-17-2010 at 09:54 PM. Reason: removed spam
- 02-12-2010 #3Just Joined!
- Join Date
- Feb 2010
- Posts
- 3
- 02-12-2010 #4Just Joined!
- Join Date
- Feb 2010
- Posts
- 18
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
while (1){
printf("If you are not root, please become...\n\n 1 = What's my mac? 7 = Start aircrack-ng and hack them all\n 2 = Change my mac 8 = Send DeAth to broadcast\n 3 = Start capturing IVS 9 = See available modems\n 4 = Start Reading files 10 = What's my interface? \n 5 = Stop interface 11 = About this prog \n 6 = Start interface 12 = Exit \n");
if (scanf("%d",&a) != 1) continue;
if (a==1) system("/etc/macchanger1.sh");
else if (a==2) system("/etc/macchanger2.sh");
else if (a==3) system("/etc/dumpcapt.sh");
else if (a==4) system("/etc/aireplayread.sh");
else if (a==5) system("/etc/airmonstop.sh");
else if (a==6) system("/etc/airmonstart.sh");
else if (a==7) system("/etc/aircrack.sh");
else if (a==
system("/etc/aireplaysenddeath.sh");
else if (a==9) system("/etc/airodump-ng.sh");
else if (a==10) system("/etc/airmon-ng.sh");
else if (a==11) printf(" This prog was written on February 2010 with purpose to make your life easier\n in hacking yours WEP modems...Cheers!!!\n Note that you must have installed macchanger and aircrack-ng \n");
else if (a==12) break;
else if (a<=1 || a>=12) printf("Please type a number from 1 to 12\n");
else {printf("ERROR\n"); break;}
}
return 0;
}
- 02-12-2010 #5Just Joined!
- Join Date
- Feb 2010
- Posts
- 3
hmmmm thx soooo much but I did it like this and works as well!!!:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char tst[256];
int a;
while (1){
printf("If you are not root, please become...\n\n 1 = What's my mac? 7 = Start aircrack-ng and hack them all\n 2 = Change my mac 8 = Send DeAth to broadcast\n 3 = Start capturing IVS 9 = See available modems\n 4 = Start Reading files 10 = What's my interface? \n 5 = Stop interface 11 = About this prog \n 6 = Start interface 12 = Exit \n");
if (scanf("%d",&a)==1){
if (a==1) system("/etc/macchanger1.sh");
else if (a==2) system("/etc/macchanger2.sh");
else if (a==3) system("/etc/dumpcapt.sh");
else if (a==4) system("/etc/aireplayread.sh");
else if (a==5) system("/etc/airmonstop.sh");
else if (a==6) system("/etc/airmonstart.sh");
else if (a==7) system("/etc/aircrack.sh");
else if (a==
system("/etc/aireplaysenddeath.sh");
else if (a==9) system("/etc/airodump-ng.sh");
else if (a==10) system("/etc/airmon-ng.sh");
else if (a==11) printf(" This prog was written on February 2010 by Hakermania\n a 15 years old child with purpose to make your life easier\n s modems...Cheers!!!\n Note that you must have installed macchanger and aircrack-ng \n Contact me:\n");
else if (a==12) break;
else if (a<=1 || a>=12) printf("Please type a number from 1 to 12\n");}
else {printf("ERROR do not type characters, please type a number from 1 to 12\n"); scanf("%s",tst);}
}
return 0;
}
But, anyway, thx for the help!!!



