簡體   English   中英

如果字符邏輯有缺陷

[英]if else character logic is flawed

好的,所以我現在嘗試使用check_status()下面的函數檢查代碼。

它們在代碼中沒有錯誤,但我認為我搞砸了邏輯。 我想在運行時將其放入S並打印“ CORRECT ANSWER。S”

現在,當您放入FEFWRGV或非S,SH,MJ,MS的代碼時,代碼可以正常工作並打印出錯誤消息。 然而! 當您放入SSDFIEWF或類似的東西時,它會顯示“ CORRECT ANSWER。S”

我檢查是否輸入了S的方式是if(status [0] =='S')

有沒有辦法我可以像這樣編寫代碼:if(status [0] =='S'&& status [1] == void)

因為我知道自己在檢查狀態[0],然后要確保未使用狀態[1]。

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <cstdio>

//functions called
float wages_loop();
float other_loop();
float interest_loop();
void check_status();


//start main 
int main(void)
{
    char status[10], another[10];
    char buffer[80];

    float wages, other_income, interest, dividends, test;
    int dependents;
    int single_acc = 0, mj_acc = 0, ms_acc = 0, sh_acc = 0;

    printf("Would you like to start: ");
    gets_s(another);

    if (another[0] != 'y' && another[0] != 'n')
    {
        while (another[0] != 'y' && another[0] != 'n')
        {
            printf("\n\n INCORRCT ANSWER. \n\n");
            printf("\n Would you like to start. (y or n)");
            gets_s(another);
        }
    }
    while (another[0] == 'y')
    {


        check_status();


        interest = 0;


        printf("\n\n\t\t Your interest is: %.2f \n", interest);
        system("pause");

        printf("Would you like anoter: ");
        gets_s(another);

        if (another[0] != 'y' && another[0] != 'n')
        {
            while (another[0] != 'y' && another[0] != 'n')
            {
                printf("\n\n INCORRCT ANSWER. \n\n");
                printf("\n Would you like anoter. (y or n)");
                gets_s(another);
            }
        }

    } //end loop

    printf("\n\n\t\t\t Number of Singles filleing: %i \n", single_acc);

    return 0;

}//end main



void check_status()
{
    char status[10];
    int check = 0;

    while (check != 1)
    {
        printf("What is your Status: ");
        gets_s(status);

        if (status[0] == 'S' && status[1] == 'H')
        {
            printf("\n\n CORRECT ANSWER. SH \n\n");
            check = 1;
        }
        else if (status[0] == 'S')
        {
            printf("\n\n CORRECT  ANSWER. S \n\n");
            check = 1;
        }
        else if (status[0] == 'M' && status[1] == 'J')
        {
            printf("\n\n CORRECT ANSWER. MJ \n\n");
            check = 1;
        }
        else if (status[0] == 'M' && status[1] == 'S')
        {
            printf("\n\n CORRECT ANSWER. MS \n\n");
            check = 1;
        }
        else
        {
            printf("\n\n INCORRECT ANSWER. noting \n\n");
        }
    }   
}

有沒有辦法我可以像這樣編寫代碼:if(status [0] =='S'&& status 1 == void)

您快要完成了。 C-“字符串”終止符不是void而是'\\0'

因此,要測試“字符串”是否在第一個字符(即status[0]之后結束,請執行以下操作:

if (status[0] == 'S' && status[1] == '\0') 

順便說一句:要聲明不帶任何參數的函數,請使用void ,如下所示:

check_status(void);

正如Joachim Pileborg已經對您的問題發表評論,您使用的gets_s()錯誤。 gets_s()需要兩個參數

編譯器應該已警告您有關缺少的第二個參數。

認真對待此類警告是一個好主意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM