簡體   English   中英

在if語句中使用strcmp

[英]Using strcmp in an if statement

我對C相當陌生,我試圖理解使用字符串和strcmpif語句中比較兩個字符串。

我的目標是能夠根據用戶輸入的內容運行其他功能。

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>

void gasbill();
void electricitybill();

int main()
{
  char input[20];
  const char gasCheck[4] = "gas";
  const char electricityCheck[13] = "electricity";

  printf("Your bills explained!\n\n");
  printf("In this application I will go through your gas and electricty bills.\n");
  printf("I will explain how each of the billing payments work, \nand the calculations that go on,\n");
  printf("to create your bill.\n\n");
  printf("Please choose a bill to get started with:\n- gas\n- electricity\n\n");
  fgets(input, 20, stdin);

  if (strcmp (input, gasCheck)== 0){
    printf("\nPreparing to run Gas bill!\n\n");
    system("PAUSE");
    system("cls");
    gasbill();
    system("PAUSE");
  }
  else if (strcmp (input, electricityCheck)== 0){
    printf("\nPreparing to run Electricity bill!\n\n");
    system("cls");
    electricitybill();
    system("PAUSE");}
  else {
    printf("\nError exiting...\n\n");
    system("PAUSE");
  }

  return 0;
}

void gasbill()
{
  float balanceBroughtForward, gasThisQuarter, subTotalPerQuarter;
  char poundSign = 156;

  printf("******Your gas bill, explained!******\n\n\n");
  printf("Hello, and welcome to your gas bill, explained. Let's get started.\n");
  printf("Please enter the balance brought forward from your previous statement: \n\n%c", poundSign);
  scanf("%f", &balanceBroughtForward);
  printf("\nHow this works:\n- The money that you did not pay last quarter for your gas bill\nhas been added to this quarterly payment\n\n");
  printf("\nNext let's add this to the amount of gas you have spent this quarter. \n(how much gas have you used so far in this billing period?)");
  printf(": %c", poundSign);
  scanf("%f", &gasThisQuarter);
  printf("\n\nNow what? The two values that you have entered\n(balance brought forward 
  and gas spent this quarter)\nare added together, %c%3.2f + %c%3.2f\n", poundSign, 
  balanceBroughtForward, poundSign, gasThisQuarter);
  subTotalPerQuarter = (balanceBroughtForward + gasThisQuarter);
  printf("This is"); 
}

void electricitybill()
{
  printf("Empty");
  system("PAUSE");   
}

無論何時運行if語句,它總是執行gasBill函數而不是electricBill函數。

提前致謝。

fgets將返回以換行符( \\n )結尾的字符串。 從其文檔

從流中讀取字符,並將它們作為C字符串存儲到str中,直到已讀取(num-1)個字符或到達換行符或到達文件末尾為止,以先發生的為准。

您可以測試尾隨換行符並將其刪除

fgets(input, 20, stdin);
size_t len = strlen(input);
if (input[len-1] == '\n') {
    input[len-1] = '\0';
}

或改為使用scanf讀取用戶輸入。

scanf("%19s", input);

作為旁白

const char gasCheck[4] = "gas";
const char electricityCheck[13] = "electricity";

可以更容易,更安全地聲明為

const char *gasCheck = "gas";
const char *electricityCheck = "electricity";

(這種形式可以避免將字符串文字復制到堆棧變量中。更重要的是,如果對數組的長度進行硬編碼,則可以消除潛在的錯誤源。)

fgets()從stdin讀取字符,直到看到換行符或EOF,如果看到換行符,它將被存儲在數組中。無論如何,fgets()將空字符附加到數組中。

因此,如果您想通過換行符結束輸入,我的較小改動就在這里,請更改此

const char gasCheck[4] = "gas";
const char electricityCheck[13] = "electricity";

const char gasCheck[5] = "gas\n";
const char electricityCheck[14] = "electricity\n";

暫無
暫無

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

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