在if语句中使用strcmp

时间:2013-05-15 12:42:13

标签: c string strcmp

我是C的新手,我正在尝试使用字符串和strcmp来比较if语句中的两个字符串。

我的目标是能够根据用户输入的内容运行不同的功能。

#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函数而不是powerBill函数。

提前致谢。

2 个答案:

答案 0 :(得分:5)

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

(这种形式可以节省将字符串文字复制到堆栈变量中。更重要的是,如果你为数组编写的代码长度太小,就会删除潜在的错误来源。)

答案 1 :(得分:0)

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