How to terminate the program from class?
Nov 23, 2011 at 7:26pm UTC
Hello everyone !
I'm trying to terminate a program from inside a function in my class. Look Below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include<iostream>
#include<string>
#include<vector>
#include<cstdlib>
using namespace std;
class Invoice {
public :
//SET METHOD FOR PRODUCT NUMBER
void setNumber(string number) {
Number = number;
products.push_back (Number);
}
//SET METHOD FOR DESCRIPTION
void setDescription(string description) {
Description = description;
descriptions.push_back (Description);
}
//SET METHOD FOR PRICE
void setPrice(double price) {
Price = price;
prices.push_back (Price);
}
//SET METHOD FOR QUANTITY
void setQuantity(int quantity) {
Quantity = quantity;
quantities.push_back (Quantity);
}
//USER MESSAGE
void Message() {
int i;
vector<int >::size_type sz = products.size();
cout<<"\nJu keni " <<sz<<" fatura te ruajtura ne sistem:\n" ;
for ( i=0; i < sz; i++ ) {
cout<<"\n" <<i+1<<". " << products[i];
}
if (i = sz) cout<<endl;
cout<<"\nCilen fature doni te aksesoni? " ; cin>>NumInvoice;
while (NumInvoice < sz || NumInvoice > sz) {
cout<<"\nKeni dhene numer te gabuar. Jepni nje te sakte: " ;
cin>>NumInvoice;}
cout<<endl;
FInvoice(NumInvoice);
}
//INVOICE COMPILER
int FInvoice(int m) {
int t = m - 1;
int n;
double Total = prices[t] * quantities[t];
cout<<"Produkti: " << products[t]<<endl;
cout<<"\t " << descriptions[t]<<"\n\t " << prices[t]<<"\n\t " << quantities[t]<<"\n" ;
cout<<"Totali: " <<Total<<endl;
cout<<"\nDoni te aksesoni ndonje fature tjeter? (JO = 0) " ; cin>>n;
if (n = 0) {
} //WHAT'S HERE ?????????????????????????????????????????????????????????????????????????????????
else Message();
}
//CLASS VARIABLES
private :
string Number, Description;
double Price;
int Quantity, NumInvoice;
//VECTORS FOR SAVING INFORMATION
vector<string> products;
vector<string> descriptions;
vector<double > prices;
vector<int > quantities;
};
//MAIN FUNCTION
int main() {
string Number_, Description_;
double Price_;
int Quantity_ ,n;
Invoice myInvoice;
cout<<"Programi ka si funksion ruajtjen e faturave te mallrave ne sistem dhe lejon perdoruesin te aksesoje te gjithe mallrat pasi jane ruajtur." ;
//CONTINUOUS LOOPING TO SAVE ALL INVOICES
do {
cout<<"\n\nNumri i Produktit: " ; cin >> Number_;
myInvoice.setNumber(Number_);
cout<<"\nPershkrimi i Produktit: " ; getline(cin, Description_);
while (Description_ == "" ) { getline(cin, Description_);}
myInvoice.setDescription(Description_);
cout<<"\nCmimi i Produktit: " ; cin >> Price_;
myInvoice.setPrice(Price_);
cout<<"\nSasia e Produktit: " ; cin >> Quantity_;
myInvoice.setQuantity(Quantity_);
cout<<"\nProdukti u ruajt me sukses ne sistem." <<endl;
cout<<"\nDoni te ruani me shume fatura? (JO = 0) " ; cin >> n;
} while (n!=0);
myInvoice.Message();
return 0;
}
Please help.
Last edited on Nov 23, 2011 at 7:43pm UTC
Nov 23, 2011 at 7:49pm UTC
LINE 54 !!
Whatever you insert in cin>>n inside function FInvoice() program sends you to function Message() again.
I want to terminate the program if n is 0.
Nov 23, 2011 at 7:53pm UTC
If you just want to bomb out use exit() which is in cstdlib
exit(EXIT_SUCCESS) if the program has terminated OK.
exit(EXIT_FAILURE) if the program has failed.
or you could throw an exception that is not caught, maybe
Nov 23, 2011 at 8:18pm UTC
I tried it but still the function sends me to Message() ...any way to implement this ?
Nov 23, 2011 at 9:18pm UTC
if ( n == 0 )
You are assigning 0 to n, which returns 0 AKA "false" therefor the else clause always runs.
Nov 23, 2011 at 10:13pm UTC
Thanks, that's right :)
Topic archived. No new replies allowed.