Jeremy McMahan
Jeremy McMahan

Reputation: 21

How do I get C to exit the program if any of these three statements are true?But after all three statements have been processed?

Trying to get the program to exit if any of the three if statements are true, but only after all three have been processed. I have formatted this a number of ways but I can only get it to exit after each statement, not after all three. So basically it will continue on with the rest of the functions even if two of the if statements are true.

 int getValues(float* loan, float* rate, int* years)
 {

//Prompt the user for the loan amount, the yearly interest rate, and the loan length in years, then make sure each value is large enough

  printf("Enter a loan amount (dollars and cents):         ");
  scanf("%f", loan);
  printf("Enter a yearly interest rate (ex. 2.5 for 2.5%): ");
  scanf("%f", rate);
  printf("Enter the loan length in years (integer number): ");
  scanf("%d", years);
   if (*loan < 1)
       printf("\nStarting amount must be at least one dollar.\n");

   if (*rate < .1)
       printf("Interest rate must be at least .1%.\n");

   if (*years < 1)
   {   printf("Number of years must be at least 1.\n");
       exit (100);
   }

  return(0); 
}

This is just the most recent attempt. I am extremely new to programming and am spending countless hours on this one little program.

Upvotes: 2

Views: 61

Answers (4)

Jay Kumar R
Jay Kumar R

Reputation: 537

int errors = 0;
if (*loan < 1) {
       printf("\nStarting amount must be at least one dollar.\n");
       errors = 1;
}
..

like the above set variable errors or anything to become 1. later check and exit. ..

Upvotes: 0

Jon
Jon

Reputation: 1085

I would have a boolean variable that initializes to false, and then when a condition is hit, set to true. Then it's a simple boolean check at the end.

int getValues(float* loan, float* rate, int* years)
{
  int shouldExit = 0;
  //Prompt the user for the loan amount, the yearly interest rate, and the loan length in years, then make sure each value is large enough

  printf("Enter a loan amount (dollars and cents):         ");
  scanf("%f", loan);
  printf("Enter a yearly interest rate (ex. 2.5 for 2.5%): ");
  scanf("%f", rate);
  printf("Enter the loan length in years (integer number): ");
  scanf("%d", years);
  if (*loan < 1){
   shouldExit = 1;
   printf("\nStarting amount must be at least one dollar.\n");
  }
  if (*rate < .1){
   shouldExit = 1;
   printf("Interest rate must be at least .1%.\n");
  }
  if (*years < 1)
  {   printf("Number of years must be at least 1.\n");
      shouldExit = 1;
  }

  if (shouldExit == 1){
      exit (100);
  }

  return(0); 
}

Upvotes: 1

Tyler H
Tyler H

Reputation: 443

Use a flag for this, it's an easy way to keep track of a specific condition being met, when you don't want to immediately act on it.

int flag = 0;
if (*loan < 1)
{
    printf("\nStarting amount must be at least one dollar.\n");
    flag = 1;
}

if (*rate < .1)
{
    printf("Interest rate must be at least .1%.\n");
    flag = 1;
}

if (*years < 1)
{   
    printf("Number of years must be at least 1.\n");
    flag = 1;
}

if(flag == 1)
{
    exit(100)
}

Upvotes: 1

dbush
dbush

Reputation: 224362

You need to add a flag variable that you would set in the body of each if. Then you would check the flag and exit if it is set.

   int flag = 0;
   if (*loan < 1) {
       printf("\nStarting amount must be at least one dollar.\n");
       flag = 1;
   }
   if (*rate < .1) {
       printf("Interest rate must be at least .1%.\n");
       flag = 1;
   }
   if (*years < 1) {   
       printf("Number of years must be at least 1.\n");
       flag = 1;
   }
   if (flag) {
        exit(100);
   }

Upvotes: 1

Related Questions