Reputation: 1
I have a problem with my code. I'm working my final project using OPENGL c++.
I want to create a code about simulation 2D in opengl. In my simulation, I ask the user to input how many objects can be shown in the application. my problem is the input can not detect characters, but can detect the numeric if out of range. I have create a code for handle if out of range, range about character not function.
for example : I input the character of 'a', want to show error handling. like "maaf, jumlah inputan anda tidak berada dalam range.", so only input the numeric.
void inputan(){
printf("APLIKASI SIMULASI ANIMASI SEMUT\n\n");
printf("Silahkan inputkan jumlah semut antara 1 - 50 : ");
scanf("%f",&input);
if(input > 50 || input < 1){
ulang();
}
char a = 'a';
int aa=a;
else if(input == aa){
ulang();
}
}
void ulang(){
printf("---------------------------------------------------\n");
printf("Maaf, jumlah inputan anda tidak berada dalam range.\n");
printf("Silahkan inputkan jumlah semut antara 1 - 50 : ");scanf("%f",&input);
if(input > 50 || input < 1){
ulang();
}
char a = 'a';
int aa=a;
else if(input == aa){
ulang();
}
}
Upvotes: 0
Views: 167
Reputation: 14815
Well, looking your code, I think there si lot of problems. You probably should read a C/C++ manual before to try programming a 2D simulation.
So your code refactorized in C probably look:
unsigned int getAntNumber()
{
unsigned int result=0;
printf("APLIKASI SIMULASI ANIMASI SEMUT\n\n");
printf("Silahkan inputkan jumlah semut antara 1 - 50 : ");
while (1!=scanf("%u",&input) || input <1 || input >50)
{
printf("---------------------------------------------------\n");
printf("Maaf, jumlah inputan anda tidak berada dalam range.\n");
printf("Silahkan inputkan jumlah semut antara 1 - 50 : ");
}
return result;
}
Edit to allow multiple input tests:
unsigned int getAntNumber()
{
unsigned int result=0;
int mustEnd=1; /*C, unlike C++ does not allow bool*/
printf("APLIKASI SIMULASI ANIMASI SEMUT\n\n");
printf("Silahkan inputkan jumlah semut antara 1 - 50 : ");
while ( mustEnd != 0)
{
if (1!=scanf("%u",&input)) /* Caution, this test must be the first one!*/
{
/*Input is not an unsigned integer*/
printf("---------------------------------------------------\n");
printf("Maaf, jumlah inputan anda tidak berada dalam range.\n");
printf("Silahkan inputkan jumlah semut antara 1 - 50 : ");
}
else if (input <1 || input >50)
{
/*Input is out of range*/
printf("---------------------------------------------------\n");
printf("Maaf, jumlah inputan anda tidak berada dalam range.\n");
printf("Silahkan inputkan jumlah semut antara 1 - 50 : ");
}
/*You may add easily more tests here*/
else
{
mustEnd=true;
}
}
return result;
}
Upvotes: 1
Reputation: 2176
1st problem.
as far as c++ is concerned
this is wrong
if(input > 50 || input < 1){
ulang();
}
char a = 'a'; //cant declare between if and else if
int aa=a;
else if(input == aa){
ulang();
}
but this can be done
if(input > 50 || input < 1){
ulang();
}
char a = 'a';
int aa=a;
if(input == aa){ //no else if but only if. But i don't know if this serves the purpose
ulang();
}
2nd problem
scanf("%f",&input);
this line is a very obscure to us. if input
is a global(file scope) variable of type float then its ok but since you want to input chars
you need to have
scanf("%c",&blah);
because you cant input characters through %f
specifier. if you give characters but have %f
scanf
will fail.
3rd problem : actually i hope you have included <cstdio>
. moreover dont use scanf
and printf
. they belong to the other beast C.
Upvotes: 0