简体   繁体   中英

Using strcmp in an if statement

I am fairly new to C, and I am trying to understand using strings and strcmp to compare two strings in an if statement.

My goal is to be able to run a different function depending on what the user has inputted.

#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");   
}

When ever it runs the if statement it always executes the gasBill function and not the electricityBill function.

Thanks in advance.

fgets will return a string which ends with a newline ( \\n ). From its documentation

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

You could either test for a trailing newline and strip it off

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

or read user input using scanf instead.

scanf("%19s", input);

As an aside

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

could be declared slightly more easily and safely as

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

(This form saves copying the string literals into stack variables. More importantly, it removes a potential source of bugs if you hard-code too small a length for the arrays.)

fgets() read characters from stdin until a newline is seen or an EOF, if newline was seen, it would be stored in the array.Anyway, fgets() appends a null character to the array.

so if you want end your input by newline, my minor alteration is here, change this

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

to

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

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