so this code is for a car garage, filling info of the accepted cars to be repaired everyday. and print the result for each car in a line.
i have two problems with my code. first is, starting the second time the "do...while... " loop runs, i cant have an input for "enter car name" second is the final output, i want it in straight neat lines but can't !
I'll be so grateful if someone gives me the edited and fixed version of my code!
#include <iostream>
using namespace std;
int D; //are you done?
int main()
{
int i = 0; //counting cars
string cars[10][7] = {};
string C = cars[i][1]; //car name
string N = cars[i][2]; //owner name
string Y = cars[i][3]; //car production year
string R = cars[i][4]; //car color
string K = cars[i][5]; //car life in km
string M = cars[i][6]; //car problem
string H = cars[i][7]; //time of arrival
do {
i ;
cout << endl;
cout << " enter car name ";
getline(cin, cars[i][1]);
cout << endl;
cout << " enter owner name ";
getline(cin, cars[i][2]);
cout << endl;
cout << " enter production year ";
getline(cin, cars[i][3]);
cout << endl;
cout << " enter car color ";
getline(cin, cars[i][4]);
cout << endl;
cout << " enter car life in km ";
getline(cin, cars[i][5]);
cout << endl;
cout << " enter car problem ";
getline(cin, cars[i][6]);
cout << endl;
cout << " enter time of arrival ";
getline(cin, cars[i][7]);
cout << endl;
cout << "this was car :" << i << endl;
cout << endl;
cout << "type '1' to continue. type '2' if you are done." << endl;
cin >> D;
} while (D == 1);
cout << endl << "ended ! here is today's list of cars :";
cout << endl;
cout << endl;
for (i = 0; i < 20; i ) {
for (int j = 0; j < 7; cout << "\t" << cars[i][j] && j );
{
}
cout << endl;
}
return 0;
}
CodePudding user response:
Here is the fixed code you're looking for:
#include <iostream>
#include <string>
#include <array>
int main( )
{
std::size_t idx { }; // counting cars
constexpr std::size_t maxCarCount { 20 };
constexpr std::size_t carAttributesCount { 7 };
std::array< std::array<std::string, carAttributesCount>, maxCarCount > cars { };
char isDone { }; // are you done?
do
{
std::cout << "\n enter car name: ";
std::getline( std::cin, cars[idx][0] );
std::cout << "\n enter owner name: ";
std::getline( std::cin, cars[idx][1] );
std::cout << "\n enter production year: ";
std::getline( std::cin, cars[idx][2] );
std::cout << "\n enter car color: ";
std::getline( std::cin, cars[idx][3] );
std::cout << "\n enter car life in km: ";
std::getline( std::cin, cars[idx][4] );
std::cout << "\n enter car problem: ";
std::getline( std::cin, cars[idx][5] );
std::cout << "\n enter time of arrival: ";
std::getline( std::cin, cars[idx][6] );
std::cout << "\nThis was car #" << idx 1 << '\n';
std::cout << "\nType '1' to continue, type '2' if you are done.\n";
std::cin >> isDone;
std::cin.ignore( ); // this solves one of your issues
idx;
} while ( isDone == '1' );
std::cout << "\nEnded! Here is today's list of cars:\n\n";
for ( std::size_t carIdx { }; carIdx < idx; carIdx )
{
for ( std::size_t carAttributeIdx { }; carAttributeIdx < carAttributesCount; carAttributeIdx )
{
std::cout << "\t" << cars[carIdx][carAttributeIdx];
}
std::cout << '\n';
}
}
Keep in mind that if the info for more than 20 cars is entered then the cars will overflow and cause undefined behavior. It has space for only 20 cars. If you want to remove this limitation then switch to std::vector< std::vetor<std::string> > cars( 20, std::vector<std::string>(7) );.
Sample input/output:
enter car name: 911
enter owner name: John
enter production year: 1999
enter car color: Yellow
enter car life in km: 3100
enter car problem: broken
enter time of arrival: 12:06
This was car #0
Type '1' to continue, type '2' if you are done.
1
enter car name: 718
enter owner name: Kate
enter production year: 2021
enter car color: Red
enter car life in km: 465
enter car problem: broken headlight
enter time of arrival: 13:45
This was car #1
Type '1' to continue, type '2' if you are done.
2
Ended! Here is today's list of cars:
911 John 1999 Yellow 3100 broken 12:06
718 Kate 2021 Red 465 broken headlight 13:45
