Home > Software design >  Id returned 1 exit status in C and I can't find my mistake
Id returned 1 exit status in C and I can't find my mistake

Time:10-22

#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.

CodePudding user response:

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?

CodePudding user response:

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 )
  • Related