0
#include <stdio.h>

int wednesday(){
  int discount;
  printf("You are lucky on Wednesdays! You can get discount!\n");
  printf("Is today wednesday? Yes = 1  No= 2\n");
  scanf("%d", &discount);
  return discount;
}

void cost(int wednesday){
  int price;
  int discount;
  if(discount == 1){
    price = price - 2;
  }
  else{
    price = price;
  }
}

void main(){
  int discount;
    
  welcome();
  discount = wednesday();
  cost(discount);
}

I write other codes too but this part didn't work I couldn't find my mistake. I would be really happy if you help me to find my mistake.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • backticks are for inline code, you want to format a whole block. Brackets, parenthesis, and semicolons are all part of the source too. – yano Oct 21 '21 at 14:59
  • 1
    Post the full error message – Eugene Sh. Oct 21 '21 at 15:02
  • 1
    Welcome to SO. Please read your error message from top to bottom. There must be some compilation error shown before the message you mentioned. – Gerhardh Oct 21 '21 at 15:02
  • Please learn how to format questions properly. It's easy and it will take you only a few minutes of your time. – Jabberwocky Oct 21 '21 at 15:02
  • 1
    In function `cost()` you are reading from uninitialised variables. But the function doesn't *do* anything anyway. – Weather Vane Oct 21 '21 at 15:03
  • 1
    There is no function `welcome()` – Gerhardh Oct 21 '21 at 15:03
  • `discount` and `price` are never initialized. Local variables in C are not initialized to zero by default, they contain an undetermined value. – Jabberwocky Oct 21 '21 at 15:04
  • The error message `Id returned 1 exit` is probably because there is no `welcome` function in your program. Anyway read the other comments, they are all relevant – Jabberwocky Oct 21 '21 at 15:07

2 Answers2

1

In main you try to call a welcome function that doesn't exist:

welcome();

Either remove welcome(); or define the function somewhere above. That will fix your linker problem, but your code has several other problems:

int discount;
if(discount == 1){

is problematic. discount is unintialized, so it has an indeterminant value. I think what you want is

if(wednesday == 1){

This is the value returned from wednesday that you pass to cost.

Furthermore, price is uninitialized as well, so

price = price - 2;

results in an indeterminant value. You should initialize price to some initial value.

int price = 0; // probably not zero, but whatever the initial price should be

Finally, remove

else{
  price = price;
}

This is a no-op, nothing is gained by setting price equal to itself.

You also have an invalid signature for main, see What are the valid signatures for C's main() function?

yano
  • 4,827
  • 2
  • 23
  • 35
0

For starters there is neither declaration of the function welcome that is called in main

welcome();

Within the function wednesday you need to check whether this call of scanf

scanf("%d", &discount);

was successful. To make the function correct you should at least initially initialize the variable discount for example like

int discount = 2;

In this case if the call of scanf will not be successful the function will return the value 2 by default.

Within the function cost there are used two uninitialized variables price and discount

void cost(int wednesday){
  int price;
  int discount;
  if(discount == 1){
    price = price - 2;
  }
  else{
    price = price;
  }
}

So the function invokes undefined behavior.

Moreover the parameter wednesday is not used.

You should declare the function with one more parameter that specifies the price. And the result price should be returned from the function to main. For example

int cost( int price, int discount ){
  if(discount == 1 && price > 2 ){
    price = price - 2;
  }

  return price;
}

Also the variable price should have at least unsigned integer type or maybe a float type.

unsigned int cost( unsigned int price, int discount ){
  if(discount == 1 && price > 2 ){
    price = price - 2;
  }

  return price;
}

Pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335