Newbie - Problem printing multiple lines from data structure in an array

This is a discussion on Newbie - Problem printing multiple lines from data structure in an array within the C++ Programming forums, part of the General Programming Boards category; I am having problems getting multiple lines to print out from a data structure that is within an array. Right ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    9

    Newbie - Problem printing multiple lines from data structure in an array

    I am having problems getting multiple lines to print out from a data structure that is within an array. Right now I am only getting the output for the last record entered into the array. I'm not sure if it is overwriting the records or if it is something wrong with they way it is looping. If anyone can give me some advice, it would be greatly appreciated.

    Here is the code for my data structure
    Code:
    struct vehicles_in{
     string inVin;
     int inPassengerCount;
     string inLuxury;
     int inMpg;
     char inType;
     int inCapacity;
    }new_vehicle [IN_VEHICLE];
    Here is the code the is accepting the input
    Code:
    void addNewVehicle()
    {
      int in;
    
      for (in=0; in<IN_VEHICLE; in++)
    {
        cout << "Please enter the VIN:" << endl;
        cin >> new_vehicle[in].inVin;
        cout << "Please enter passenger capacity for this vehicle:" << endl;
        cin >> new_vehicle[in].inPassengerCount;
        cout << "Is this vehicle a luxury vehicle? Y/N" << endl;
        cin >> new_vehicle[in].inLuxury;
        cout << "What is this vehicles miles per gallon?" << endl;
        cin >> new_vehicle[in].inMpg;
        cout << "Please specify vehicle type. (C)ar, (T)ruck, (V)an, or (M)otorcycle:" << endl;
        cin >> new_vehicle[in].inType;
        system ("clear");
        menuSelect();
        system("clear");
        break;
      }
    }
    Here is the code where the functions are called that print the data from the array.
    Code:
    void listVehicles()
    {
      int in;
    
      for (in=0; in<IN_VEHICLE; in++)
      {
        printVehicle();
        break;
      }
    }
    
    void printVehicle()
    {
      int t;
    
      
      for (t=0;t<IN_VEHICLE; t++)
      {
      if (!new_vehicle[t].inVin.empty())
      {
      cout << "Vin";
      cout << "                   ";
      cout << "Passenger Count";
      cout << "       ";
      cout << "Luxury Vehicle";
      cout << "     ";
      cout << "MPG";
      cout << "                   ";
      cout << "Type" << endl;;
      cout << new_vehicle[t].inVin;
      cout << "     ";
      cout << new_vehicle[t].inPassengerCount;
      cout << "                     ";
      cout << new_vehicle[t].inLuxury;
     cout << "                  ";
      cout << new_vehicle[t].inMpg;
      cout << "                     ";
      cout << new_vehicle[t].inType << endl;
        }
      }
    }

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Why do you put the break in the for loop?

    Code:
    void listVehicles()
    {
      int in;
    
      for (in=0; in<IN_VEHICLE; in++)
      {
        printVehicle();
        break; // Print 1 vehicle and quit?!?
      }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with data in allocated memory
    By supi in forum C Programming
    Replies: 3
    Last Post: 06-09-2008, 02:06 AM
  2. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-07-2007, 11:24 PM
  3. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  4. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  5. problem with printing a structed array using for loop
    By Prezo in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2002, 09:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21