hello im writing a if statment for class and i cant get the program to terminate if the user goes to the else statement it still prints out the other cout's here is my program your supposed to enter in a id number then the crn of the class u want to take ( for example 4587) but when u enter a wrong crn number such as 123 it displays the "course not found" but still prints the rest of the program but i want it to terminate there.

#include<iostream>
using namespace std;

#define ch1 120.25
#define healthid 35.00

int main()

{
  int id;
  double credit,payment,totalpay;
  int crn;
  string prefix;
  
  cout <<fixed;
  cout .precision (2);
  
  
  
  cout <<"Enter student ID:V" ;
  cin >>id ;
  
  cout <<"Enter CRN for first course:" ;
  cin>>crn;
  if (crn == 4587)
     {prefix = "MAT 236";
     credit = 4;
     }
 else if (crn == 4599)
     {prefix = "COP 220";
     credit = 3;
     }
 else if (crn == 8997)
     {prefix = "GOL 124";
     credit = 1;
     }
 else if (crn == 9696)
     {prefix = "COP 100";
     credit = 5;
     }
 else if (crn == 4580)
     {prefix = "MAT 230";
     credit = 3;
     }
 else if (crn == 4581)
     {prefix = "MAT 231";
     credit = 4;
     }
 else if (crn == 4582)
     {prefix = "MAT 232";
     credit = 2;
     }
 else if (crn == 4583)
     {prefix = "MAT 233";
     credit = 2;
     }
 else if (crn == 3587)
     {prefix = "MAT 256";
     credit = 4;
     }
 else if (crn == 4519)
     {prefix = "COP 420";
     credit = 3;
     } 
 else
 {cout<<"Course not found try again!!\n\n";
  
 }
  
  
  payment = credit * ch1;
  totalpay = payment + healthid ;
  
  
  cout <<"VALENCIA COMMUNITY COLLEGE\n" ;
  cout <<"ORLANDO,FL 10101\n" ;
  cout <<"**************************\n\n" ;
  
  cout <<"Fee Invoice Prepared for Student: V" <<id <<"\n\n" ;
  
  cout <<"1 Credit Hour= $120.25\n\n" ;
  
  cout <<"CRN\tPrefix\t Credit Hours\n" ;
  cout <<crn <<"\t" <<prefix <<"\t  " <<credit <<"\t$"<<payment <<"\n\n" ;
  
  cout <<"\tHealth & ID fees $" <<healthid <<"\n\n" ; 
  cout <<"-----------------------------\n\n" ;
  cout <<"\tTotal Payments: $" <<totalpay <<"\n" ;
  
  
  
  
  
  
  
  system ("pause") ;
  
}

Recommended Answers

All 3 Replies

i figured it out myself i put

else
 {cout<<"Course not found try again!!\n\n";
  system("pause");
  return 0;
  
 }

in the else statement and it worked =) took alot of brain power =P

int main() is missing return(0);
Which is what would terminate you program, as well.

btw, You don't need system("pause"); cout <<"Slam on your keyboard."; cin.get();

Then terminate it there. :) If you do not do something to break the line of execution, you will step over each statement in order. Try this to kill the program from the else clause:

else
{
    system("pause");
    return 0;
}

This matches your end of main code, but I do not recommend the habit of using system() for something mundane like pausing execution. It is too risky because system() is easily turned to the dark side by replacing the pause program with something malicious. A better way to do the same thing uses the equivalent of getch():

else
{
    cout << "press any key to continue . . .";
    getch(); // not portable
    return 0;
}

And the portable way is only a bit less convenient by forcing you to type [Enter] instead of any key right away:

else
{
    cout << "press [Enter] to continue . . .";
    cin.get();
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.