How to read user input into a struct and add to a csv file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Laura Wilkinson
    New Member
    • Dec 2010
    • 17

    How to read user input into a struct and add to a csv file?

    Hi all,

    I am trying to use the variables I have in the struct to store user input about a car.

    Code:
    struct CarC
    {
    	string carRegistration;
    	string carOwner;
    	string carOwnerAddress;
    	string carManufacturer;
    	string carModel;
    	unsigned numCarDoors;
    	unsigned numCarSeats;
    	string carType;
    	string carInformation;
    };
    I have attempted this below, but I am not sure if what I am trying to do is possible. After the information is inputted by the user and stored, it needs to add to a csv file.

    My attempt;

    Code:
    int vehicleOption = 0;
        cout << "Automated Number Plate Identification System\n\n";
    	cout << "Select an Option\n\n";
    	cout << "1 = Car\n2 = Bus\n3 = Motorbike\n4 = Lorry\n";
    	cout << "Your Option: ";
    	cin >> vehicleOption;
    
    	if(vehicleOption==1)
    	{
    		int loop = 0;
    		int eloop = 9;
    		filestr.open ("Vehicles.csv", fstream::in | fstream::out | fstream::app);
    		if(loop!=eloop)
    
    		cout << "Car Registration: "; cin >> carRegistration;
    		cout << "Car Owner: "; cin >> carOwner;
    		cout << "Owner Address: "; cin >> carOwnerAddress;
    		cout << "Car Manufacturer: "; cin >> carManufacturer;
    		cout << "Car Model: "; cin >> carModel;
    		cout << "Number of Car Doors: "; cin >> numCarDoors;
    		cout << "Number of Car Seats: "; cin >> numCarSeats;
    		cout << "Car Type: "; cin >> carType;
    		cout << "Other Information: "; cin >> carInformation;
    
            filestr<<" "<<carRegistration<<" "<<carOwner<<" "<<carOwnerAddress<<" "<<carManufacturer<<" "<<carModel<<" "<<numCarDoors<<" "<<numCarSeats<<" "<<carType<<" "<<carInformation<<endl;
    	}
    Any advice/help would be appreciate..

    Thanks in advance!
  • Laura Wilkinson
    New Member
    • Dec 2010
    • 17

    #2
    No worries, I have figured it out now!!

    Code:
    if(vehicleOption==1)
    	{	
    		fstream filestr;
    
    		struct CarC car;
    
    		int loop = 0;
    		int eloop = 9;
    		filestr.open ("Vehicles.csv", fstream::in | fstream::out | fstream::app);
    		if(loop!=eloop)
    
    		cout << "\nCar Registration: "; cin >> car.carRegistration;
    		cout << "\nCar Owner: "; cin >> car.carOwner;
    		cout << "\nOwner Address: "; cin >> car.carOwnerAddress;
    		cout << "\nCar Manufacturer: "; cin >> car.carManufacturer;
    		cout << "\nCar Model: "; cin >> car.carModel;
    		cout << "\nNumber of Car Doors: "; cin >> car.numCarDoors;
    		cout << "\nNumber of Car Seats: "; cin >> car.numCarSeats;
    		cout << "\nCar Type: "; cin >> car.carType;
    		cout << "\nOther Information: "; cin >> car.carInformation;
    
            filestr<<", "<<car.carRegistration<<", "<<car.carOwner<<", "<<car.carOwnerAddress<<", "<<car.carManufacturer<<", "<<car.carModel<<", "<<car.numCarDoors<<", "<<car.numCarSeats<<", "<<car.carType<<", "<<car.carInformation<<endl;
    	}

    Comment

    Working...