constructor not displaying values in c++ switch statement -
hi im trying build program ferry keep track of free space im stuck trying output values user gave in inside constructor. problem occurs in case 5 im trying output it. loop doesnt work cuz break while loop didnt work either. creating seperate function didnt work. appreciated.
#include <iostream> #include <string> #include <sstream> using namespace std; struct cars { int carlength, carcount; string registrationnumber; int carseats; } caaars[100]; void printcars(cars car); int n, x; int main() { int loop = 0; string mystr, mystr1, mystr2; int ferrylength; cout << "the ferry program\n\n"; cout << "please enter length of ferry in meters: "; cin >> ferrylength; while (loop == 0) { system("cls"); cout << "choose vehicle \n\n"; cout << "1: car.\n"; cout << "2: bus.\n"; cout << "3: lorrie.\n"; cout << "4: digger.\n"; cout << "5: print report.\n"; cout << "6: close program.\n\n"; int choice; cin >> choice; switch (choice) { case 1:{ cout << "\nenter amount of cars needed placed on ferry: "; cin >> x; cin.ignore(); while (n != x) { cout << "\nplease enter length of car in metres: "; getline(cin, mystr); stringstream(mystr) >> caaars[n].carlength; //for integers. cout << "\nplease enter registration number: "; getline(cin, caaars[n].registrationnumber); //for strings , both cout << "\nplease enter amount of seats in car: "; getline(cin, mystr2); stringstream(mystr2) >> caaars[n].carseats; n++; } } break; case 2: break; case 3: break; case 4: break; case 5: { int l; l = 0; (l = 0; l < x; l++) printcars(caaars[n]); } break; case 6: loop++; break; }// end of while loop. } return 0; } void printcars(cars car) { int p = 1; system("cls"); cout << "car "; cout << p; cout << ":" << endl; cout << "\nthe registration number is: "; cout << car.registrationnumber << endl; cout << "\nthe car length is: "; cout << car.carlength << endl; cout << "\namount of car seats: "; cout << car.carseats << endl; p++; }
you iterating using n
while should use l
instead:
case 5: { int l; l = 0; (l = 0; l < x; l++) printcars(caaars[l]); }
Comments
Post a Comment