-2

When I call a function into main function, my code gets stuck. When I debug my code, it is free of error but when I run it, it is stuck. How can I solve this?

#include <stdio.h>

float before_discount_price(int, int, char);
float discount_price(int, int, char);
void displayinfo();

int main() {
  char name[20], customerID[20], type, category, memberCard;
  int quantity, age;
  float price, total, totalCharge;

  printf("Enter Your Name: ");
  gets(name);
  printf("Enter Your ID: ");
  scanf("%s", & customerID);
  printf("Your Age: ");
  scanf("%d", & age);
  fflush(stdin);
  printf("Choose your Activities: (A for Waterpark, B for Safari Night): ");
  scanf("%c", & type);
  printf("How many tickets you want to buy: ");
  scanf("%d", & quantity);
  fflush(stdin);
  printf("Do you have a member card?: (Y for Yes, N for No):");
  scanf("%c", & memberCard);

  // i get stuck at here, i have no idea to solve it .....
  price = before_discount_price(quantity, age, type);
  total = discount_price(quantity, total, memberCard);
  printf("%.2f", & total);
  return 0;
}
// first function
float before_discount_price(int quantity, int age, char type) {
  int total;
  int i = 1;

  while (i <= quantity) {
    if (type == 'A') {
      if (age >= 18) {
        total = total + 30;
      } else {
        total = total + 15;
      }
    }
    if (type == 'B') {
      if (age >= 18) {
        total = total + 25;
      } else {
        total = total + 15;
      }
    }
    i++;
    return total;
  }
}
//second function
float discount_price(int quantity, int total, char memberCard) {
  float totalprice, discount;
  int i = 0;
  while (i <= quantity && memberCard == 'Y') {
    discount = total * 0.3;
  }
  i++;

  return totalprice = total - discount;
}
//third function(not yet done)
void displayinfo() {
  printf("Hi");

}

Output of Above Code:

stuck

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 1
    Inside the `before_discount_price` function you have the variable `total`, but you never initialize it. That means its value will be *indeterminate* (look at it as garbage). I also recommend you do some debugging of the function, some simple [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging) should tell you a mistake of it. – Some programmer dude Sep 14 '21 at 10:02
  • As for the "get stuck" problem, that doesn't actually happen in the `before_discount_price` function, but rather the `discount_price` function, Which I also suggest you debug, as that should show you the problem very quickly. By the way, you have the same problem with uninitialized variable in `discount_price`. – Some programmer dude Sep 14 '21 at 10:03
  • 1
    MS VC compiler emits about 10 warnings. Please fix them! One of them is for `gets()` which is obsolete and is no longer part of the standard C library. Please read [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Weather Vane Sep 14 '21 at 10:04
  • I also suggest you learn more about loops in C, especially the `for` loop, which should help solve the "get stuck" problem. – Some programmer dude Sep 14 '21 at 10:04
  • 2
    Another, which is a sure error, is for `printf("%.2f", & total);` Although `print` resembles `scanf` in some ways, they are quite different functions. – Weather Vane Sep 14 '21 at 10:06
  • 2
    On a totally different note, passing an input-only stream (like `stdin`) to `fflush` is *undefined behavior*. If you want to skip leading white-space before reading a character with `scanf`, simply add a space in the format string. As in `scanf(" %c", & type);` – Some programmer dude Sep 14 '21 at 10:06
  • Regarding the first comment, you also have `return total` in the wrong place. It should be outside the `while` loop. – Weather Vane Sep 14 '21 at 10:18

1 Answers1

0

1.gets()The basic problem is that the function doesn't know how big the buffer is, so it continues reading until it finds a newline or encounters EOF, and may overflow the bounds of the buffer it was given.so,use scanf("%[^\n]s", name); instead of gets(name);

2.

  • & is used to get the address of the variable. C does not have a string type, String is just an array of characters and an array variable stores the address of the first index location.
  • By default the variable itself points to the base address and therefore to access base address of string, there is no need of adding an extra &.so,write this scanf("%s", customerID); instead of scanf("%s", & customerID);.

3.printf("%.2f", & total); we dont use & in printf function because it only prints the value of variable in output console,there are no changes going to be made in variable. So it is not required to send the address.

4.int total; initialize the variable to zero because It is often set to zero initially, because it serves as a variable slowly adding the rest of the numerical values.

E.g. If you want the sum of 1,2,3,4,5 it would be: sum = 1+2+3+4+5. If you use any other initial value for sum, let's say 1, the sum would be calculated as 1+1+2+3+4+5

vegan_meat
  • 878
  • 4
  • 10