0

Please help me figure out this problem. I enter data for 3 laptops and loop prints data of the last laptop only for 3 times.

#include <iostream> #include <string> using namespace std; class laptop { private: string brand; string model; int serial; string color; float price; float processor_Speed; int ram; float screen_Size; public: void display() { cout<<"Brand: "<<brand<<endl; cout<<"Model: "<<model<<endl; cout<<"Serial No. "<<serial<<endl; cout<<"Color: "<<color<<endl; cout<<"Price: "<<price<<endl; cout<<"Processor Speed: "<<processor_Speed<<endl; cout<<"RAM :"<<ram<<endl; cout<<"Screen Size: "<<screen_Size<<endl; cout<<"-------------------"<<endl; } laptop(string b, string m, int s, string c, float p, float ps,int r, float sz) { brand = b; model = m; serial = s; color = c; price = p; processor_Speed = ps; ram = r; screen_Size = sz; } }; int main() { string brand; string model; int serial; string color; float price; float processor_Speed; int ram; float screen_Size; for (int i = 0; i<3; i++) { cout << "Laptop model :" << i + 1 << endl << endl; cout << "Brand name (string):"; cin >> brand; cout << "Modal name (string):"; cin >> model; cout << "Serial name (int):"; cin >> serial; cout << "color name (string):"; cin >> color; cout << "Price (int):"; cin >> price; cout << "processor speed (float):"; cin >> processor_Speed; cout << "RAM (int):"; cin >> ram; cout << "Screen size (float):"; cin >> screen_Size; cout << endl << endl; } cout << "____________________________________" << endl; for (int i = 0; i<3; i++) { cout << "Laptop Model :" << i + 1 << endl << endl; laptop details(brand, model, serial, color, price, processor_Speed, ram, screen_Size); details.display(); // print.upgrade_RAM(); cout << endl << endl << endl; cout << "_

22nd Sep 2018, 8:05 PM
S.K
2 Antworten
0
it prints out only the last one every time, I need it to print all inputs.
22nd Sep 2018, 8:18 PM
S.K
0
Hello bro, In my way of thinking, problem is when you entered data to a laptop at first it's fine. but when you entered data to second laptop first laptop's data replaced with new data. Because here one object is capable of holding only one laptops data . you need to create 3 object for 3 laptops. one more thing is , In this program create objects outside for loop . because when you create a object inside the loop, object is created as locally, it not not possible to access from outside the loop.and don't use constructors here. it is very complex to handle here.
22nd Sep 2018, 9:48 PM
Arjun gowda
Arjun gowda - avatar