Frustrated by a problem I don't see !
Nov 14, 2011 at 7:40pm UTC
Hello everyone !
I'm having a weird problem after execution.
Here's my code :
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
#include<iostream>
#include<string>
#include<vector>
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);
}
//INVOICE COMPILER
void getInvoice() {
double Total = Quantity*Price;
cout<<"\n\n------------------ Invoice ------------------\n\n\n" ;
cout<<"Products : " <<(int ) products.size()<< endl;
cout<<"Descriptions : " <<(int ) descriptions.size()<< endl;
cout<<"Prices : " <<(int ) prices.size()<< endl;
cout<<"Quantities : " <<(int ) quantities.size()<< endl;
}
//CLASS VARIABLES
private :
string Number, Description;
double Price;
int Quantity;
//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;
do {
cout<<"\nWhat's the product number? " ; getline(cin, Number_);
myInvoice.setNumber(Number_);
cout<<"\nWhat's the description? " ; getline(cin, Description_);
myInvoice.setDescription(Description_);
cout<<"\nWhat's the price? " ; cin >> Price_;
myInvoice.setPrice(Price_);
cout<<"\nWhat's the quantity? " ; cin >> Quantity_;
myInvoice.setQuantity(Quantity_);
cout<<"\nWant to save more invoices in the system? (NO=0) :" ; cin >> n;
} while (n!=0);
myInvoice.getInvoice();
system("pause" );
return 0;
}
The output I get is this :
http://i40.tinypic.com/qrn6tk.png
I don't understand why program doesn't take in consideration anymore this line :
This is just a test program.
Nov 14, 2011 at 7:59pm UTC
cin >> n;
does not discard new line character, getline() tries to read the line and read empty line.
You can update the code in such a way:
1 2 3
do {
getline(cin, Number_);
}while (Number_ == "" );
Nov 23, 2011 at 5:18pm UTC
Thanks a lot !
Nov 23, 2011 at 5:31pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
int main() {
string Number_, Description_;
double Price_;
int Quantity_ ,n;
Invoice myInvoice;
do {
cout<<"\nWhat's the product number? " ; getline(cin, Number_);
myInvoice.setNumber(Number_);
cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); // get rid of that pesky Enter key
cout<<"\nWhat's the description? " ; getline(cin, Description_);
myInvoice.setDescription(Description_);
cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); // get rid of that pesky Enter key
cout<<"\nWhat's the price? " ; cin >> Price_;
myInvoice.setPrice(Price_);
cout<<"\nWhat's the quantity? " ; cin >> Quantity_;
myInvoice.setQuantity(Quantity_);
cout<<"\nWant to save more invoices in the system? (NO=0) :" ; cin >> n;
} while (n!=0);
Topic archived. No new replies allowed.