-3

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;
}
mch
  • 9,424
  • 2
  • 28
  • 42
  • 6
    `string H=cars[i][7] ; //time of arrival` is out of bounds. Indices are from 0 to size-1. – Retired Ninja Jan 08 '22 at 20:54
  • use inline "\n" or "\t" in your lines. \n is new line. \t is tab. – apollosoftware.org Jan 08 '22 at 20:54
  • 1
    You should also read [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Retired Ninja Jan 08 '22 at 20:55
  • 2
    With `string C=cars[i][1] ; //car name` you will make a copy of the *empty* string `cars[0][1]`. Then you never use this variable `C` again (or the other variables beyond `i` and `cars`) again. What use is those unused variables? – Some programmer dude Jan 08 '22 at 21:16
  • 1
    Does this answer your question? [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – BoP Jan 08 '22 at 21:47
  • this was actually my first time coding. After doing 2 other ones. then came back to edit this one and changed somethings. so these variables were used in the previous version and i couldn't delete them cause i was getting errors. so if it isn't broken, why fix it :))) of course i have to learn more...@Someprogrammerdude – zzZArianZzz Jan 09 '22 at 14:04

1 Answers1

0

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

digito_evo
  • 3,216
  • 2
  • 14
  • 42