userxbw

Struct array, functions, error handling C++

Sep 10th, 2022
1,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4.  
  5.  
  6. using namespace std;
  7.  
  8. struct Vehicles
  9.  
  10. {
  11.  
  12. string car_model;
  13.  
  14. string car_make;
  15.  
  16. int car_mileage;
  17.  
  18. int car_year;
  19.  
  20. };
  21. /* function phrams naming is not the same
  22.    as your varitables name, and it uses
  23.    its own naming schem inside of itself
  24. */
  25. void getData(Vehicles a[],int s);
  26. void output( Vehicles v[], int size);
  27.  
  28. int main(){
  29. int amount;
  30.       cout<<"Enter amount of cars to be logged\n";
  31.             cin>>amount;
  32.       Vehicles var[amount];
  33.       getData(var,amount);
  34.       output(var,amount);
  35. return 0;
  36. }
  37.  
  38. void getData(Vehicles a[],int s)
  39. {   /* use finctions naming inside
  40.       itself
  41.     */
  42.     char run='y';
  43.  
  44.       for(int i = 0; i < s; i++)
  45.       {    /* you can pass striaght into
  46.               the members, saving time and
  47.               memory by not using extra data vars
  48.            */
  49.  
  50.             cout
  51.             <<" Input the Car Model for car number "
  52.             "<"
  53.             <<i+1
  54.             <<">"
  55.             << endl;
  56.             cin  >> a[i].car_model;
  57.             cout << "Input the Car  Make ";
  58.             cin  >> a[i].car_make;
  59.             while(run=='y'){
  60.                   cout << "Input Car mileage:" << endl;
  61.       /* error handling bad input char for int */
  62.                   cin  >> a[i].car_mileage;
  63.                   if(!cin){
  64.                         cin.clear();
  65.                         cin.ignore(
  66.                         numeric_limits<streamsize>::max(),
  67.                         '\n');
  68.                         /*.ask again */
  69.                         continue;
  70.                   }else{
  71.                               run='n';
  72.                         }
  73.             }/* reset for next loop */
  74.                run='y';
  75.            while(run=='y'){
  76.                   cout << "Input Car Year:" << endl;
  77.                   cin  >> a[i].car_year;
  78.                   if(!cin){
  79.                         cin.clear();
  80.                         cin.ignore(
  81.                         numeric_limits<streamsize>::max(),
  82.                         '\n');
  83.                         continue;
  84.                  }else{
  85.                               run='n';
  86.                        }
  87.            }
  88.         }
  89. }
  90.  
  91. void output( Vehicles v[], int size )
  92. {
  93.       /*
  94.             this formatting style is of
  95.             my own makes it easier to
  96.             change things;
  97.      */
  98.  
  99.       for ( int i = 0; i < size; i++)
  100.       {
  101.  
  102.             cout
  103.             <<endl
  104.             <<endl
  105.             << "("
  106.             <<i+1
  107.             <<")"
  108.             <<"Car Model"
  109.             << "::"
  110.             << v[i].car_model
  111.             << endl;
  112.  
  113.             cout
  114.             << "Car Make"
  115.             << "::"
  116.             << v[i].car_make
  117.             << endl;
  118.  
  119.             cout
  120.             << "Car Mileage"
  121.             << "::"
  122.             << v[i].car_mileage
  123.             << endl;
  124.  
  125.             cout
  126.             << "Car Year"
  127.             << "::"
  128.             << v[i].car_year
  129.             << endl<<endl;
  130.       }
  131. }
  132.  
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment