简体   繁体   中英

Problems handling text input

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();
}
}

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.

  • "else if" must always be part of a "if" statment.
  • Avoid implicit casting, even if in this case is safe: int aa=a; //a is a char
  • "printf" and "scanf" are C library, for C++ its better to use streams like cout and cin.
  • Avoid recursion if not necesary: recursion may cause memory problems.
  • "scanf" with "%f" is for float, in your case, you must use "%u" that mean unsigned integer.

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM