繁体   English   中英

将字符串比较为C中的整数

[英]Compare string to integer in C

初学者在这里。 我正在尝试制作一个游戏,要求用户提出一个谜。 正确的答案是这样写的当前时间:hh:mm

在用户输入许多不同的错误猜测(例如随机字母)之前,该程序可以正常工作。 此后,即使答案正确,它也会给出错误。

这是代码:

#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdbool.h>

int main(){
    //check
    bool ok=false; //true when guess==real time
    int tent=0; //tryes
    //guesses
    char tempo[5]; //input string from user
    char ora[2];   //houres
    char min[2];   //mins
    //get time
    time_t my_time;
    struct tm * timeinfo; 
    time (&my_time);
    timeinfo = localtime (&my_time);
    //random
    srand(time(NULL));

    //guessing game, user shound input a string that conains current time to win:   hh:mm
    printf("In principio era uno, e il nulla.\n\n");
    printf("Poi l'uomo lo duplico', e tra essi traccio' la via...\n");
    printf("Lo fece ancora... e gli sembro' perfetto.\n");
    printf("Ma non era abbastanza... ");
    printf("Cosi' si spinse piu' in profondita', e sbirciando poco oltre trovo'...  ");

    do{
        //get guessed time (could also get words and non relevant numbers, if input is not hh:mm (current hour:current mins) user gets error)
        scanf("%s",&tempo);
        fflush(stdin);
        //split array tempo into ora and min to separate h from mins
        ora[0]=tempo[0];
        ora[1]=tempo[1];
        min[0]=tempo[3];
        min[1]=tempo[4];
        //cast guess form string to int
        int oraint=atoi(ora); //creat integer hour from string
        int minint=atoi(min); //integer mins

        //check guess == real time
        if(oraint==timeinfo->tm_hour && minint==timeinfo->tm_min){
            //win
            printf("\nCOMPLIMENTI! Hai trovato la risposta!\n");
            printf("\n\nEcco le tue prossime istruzioni!\n\n");
            printf("TURFeE1UQXdNREF3TVRFd01EQXdNVEF4TVRFd01ERXhNREV4TVRBd01URXdNVEV4TURFeE1UQXhNVEF4TVRFeE1ERXhNVEF3TVRBd01URXdNREV3TURBd01URXhNREV3TURBeE1EQXdNREF3TVRBd01ERXhNREF4TVRBeE1URXhNREV4TVRBeE1ERXdNVEV4TURBeE1EQXhNVEV3TVRBd01ERXhNREV3TURBd01UQXdNREV3TURBeE1UQXhNREF4TURFeE1ERXhNREV3TVRFd01ERXdNVEF4TVRBeE1URXdNREV4TVRBd01URXdNVEV3TVRBd01UQXhNVEF4TVRFeE1ERXhNREV4TVRBPQ==\n\n");
            printf("Che c'e'? devo anche dirti come decifrarle?\n");
            printf("...e va bene...ti do un'indizio\n");
            printf("Ricorda che, a volte, un colpo non basta.");
            ok=true;
        } else {
            tent++;
            int val=rand()%6; //random error pharases
            switch(val){
                case 0:
                    printf("Non ci siamo...\n\n");
                    break;
                case 1:
                    printf("Pare di no...\n\n");
                    break;
                case 2:
                    printf("Riprova.\n\n");
                    break;
                case 3:
                    printf("Pensaci meglio...\n\n");
                    break;
                case 4:
                    printf("Nah, prova ancora.\n\n");
                    break;
                case 5:
                    printf("Ti ho mai detto quante risposte hai gia' provato?\n");
                    printf("Beh... sono ben ");
                    printf("%d\n\n",tent);
                    break;
            }
        }
    }while(ok=true);

    getchar();
    return 0;
}

我正在尝试未学习的内容,因此请原谅愚蠢的错误或错误的代码。

这里有些问题:

scanf("%s",&tempo); 
fflush(stdin);
//split array tempo into ora and min to separate h from mins
ora[0]=tempo[0];
ora[1]=tempo[1];
min[0]=tempo[3];
min[1]=tempo[4];
//cast guess form string to int
int oraint=atoi(ora); //creat integer hour from string
int minint=atoi(min); //integer mins

逐行:

scanf("%s",&tempo);

在将诸如tempo的数组表达式传递给scanf时,请勿使用&运算符-在大多数情况下,数组表达式会自动转换为指针表达式。 那条线应该是

scanf("%s", tempo);

其次, tempo不够大,无法存储字符串 “ hh:mm”-请记住,字符串始终以0结尾。您需要分配一个数组,该数组至少比要存储的最长字符串大1个元素。 ,因此应将tempo声明为char[6]

第三,

fflush(stdin);

一般没有定义; “冲洗”输入流根本没有多大意义。 的确,Microsoft对其进行了定义,以清除其特定实现中的输入流,但总的来说,这句话毫无意义。 不要期望它在MSVC实施之外的任何地方都无法工作。

下一个,

ora[0]=tempo[0];
ora[1]=tempo[1];
min[0]=tempo[3];
min[1]=tempo[4];

tempo一样, oramin不是字符串 -您尚未为字符串终止符留出任何空间。 atoi无法正确转换它们。

您必须将oramin都声明为char[3] ,并确保将ora[2]min[2]设置为0:

ora[0] = tempo[0];
ora[1] = tempo[1];
ora[2] = 0;
min[0] = tempo[3];
min[1] = tempo[4];
min[2] = 0;

当然,您可以使用以下方法避免所有这些情况:

int ora;
int min;

if ( scanf( "%d:%d", &ora, &min ) == 2 )
{
  // read ora and min properly
} 
else
{
  // bad input or error
}

最后,

while(ok=true);

需要是

while ( ok == true ); // == for comparison, = for assignment

要么

while ( ok ); // my personal preference

ok=true 分配true ,以ok -来执行比较,则使用ok == true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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