having trouble displaying elements of an array

Hello I'm having trouble displaying the contents of an array. Heres my code and I cant seem to find the problem

// C++ program to fractions
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// Structure declaration
struct Car
{
string carMake;
string carModel;
int yearModel;
double cost;
};

// Function Protocols
void getCar(Car &);
void printCar(Car);

int main()
{
const int SIZE = 3;
Car arrCar[SIZE] = {
{"Ford", "Mustang", 2968, 20300},
{0,0,0,0},
{0,0,0,0},
};
int index;

cout << "Please input the info of car" << endl;

for (index = 1; index < SIZE; index++)
{
getCar(arrCar[index]);
}

// printCar function is called
printCar(arrCar[index]);

return 0;
}

// getCar function prompts user to input the information for the car
void getCar(Car &temp)
{
//Car temp;
cout << "Enter Make: ";
getline(cin, temp.carMake);
cout << "Enter Model: ";
cin >> temp.carModel;
cout << "Enter Year: ";
cin >> temp.yearModel;
cout << "Enter Cost: ";
cin >> temp.cost;
cout << endl;
// return temp;
}
// printCar function displays the information of the car*
void printCar(Car temp)
{
for (int index = 0; index < 3; index++)

cout << fixed << setprecision(2);
cout << "Make: " << temp[index].carMake << endl;
cout << "Model: " << temp[index].carModel << endl;
cout << "Year: " << temp[index].yearModel << endl;
cout << "Price: $" << temp[index].cost << endl << endl;
}
Please describe the "trouble" in detail. Error messages? Unexpected results?
Please, please, please use code tags!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;


// Structure declaration
struct Car
{
   string carMake;
   string carModel;
   int yearModel;
   double cost;
};


// Function Protocols
void getCar(Car &);
void printCar(Car);


int main()
{
   const int SIZE = 3;
   Car arrCar[SIZE] = { {"Ford", "Mustang", 2968, 20300},
                        {"0","0",0,0},                       // <==== strings at start, please
                        {"0","0",0,0},                       // <==== ditto
                                                         };
   int index;
   
   cout << "Please input the info of car" << endl;
   
   for (index = 0; index < SIZE; index++)               // <==== array starts at 0, not 1
   {
      getCar(arrCar[index]);
   }
   
   // printCar function is called
   for (index = 0; index < SIZE; index++)               // <==== need another loop
   {
      printCar(arrCar[index]);
   }
   
   return 0;
}

// getCar function prompts user to input the information for the car
void getCar(Car &temp)
{
   cout << "Enter Make: ";   getline(cin, temp.carMake);
   cout << "Enter Model: ";  cin >> temp.carModel;
   cout << "Enter Year: ";   cin >> temp.yearModel;
   cout << "Enter Cost: ";   cin >> temp.cost;           cin.ignore( 256, '\n');   // <==== just for safety
   cout << endl;
}

// printCar function displays the information of the car*
void printCar(Car temp)                                 // <==== removed index loop
{
   cout << fixed << setprecision(2);
   cout << "Make: " << temp.carMake << endl;
   cout << "Model: " << temp.carModel << endl;
   cout << "Year: " << temp.yearModel << endl;
   cout << "Price: $" << temp.cost << endl << endl;
} 



Input:
Vauxhall
Vectra
2001
7000
Ford
Escort
1999
3000
Motability
Scooter
2016
1000


Output:
Make: Vauxhall
Model: Vectra
Year: 2001
Price: $7000.00

Make: Ford
Model: Escort
Year: 1999
Price: $3000.00

Make: Motability
Model: Scooter
Year: 2016
Price: $1000.00
Use a range-based for loop instead, when iterating through elements. Though, there are some exceptions.

Example of range-based for loop:

1
2
3
4
for (const auto &i : arrCar)
{
    printCar(i);
}
Last edited on
lastchance wrote:
<==== array starts at 0, not 1

That might have been deliberate; the array was initialized with a Mustang at index 0. A getCar into 0 overwrites that.
That might have been deliberate; the array was initialized with a Mustang at index 0. A getCar into 0 overwrites that. 


Fair comment. But it wasn't the main issue.
Thanks a bunch for the feedback fellas. @keskiverto it was deliberately because I wanted the mustang to go first. @lastchance I appreciate your input my friend. I noticed I had to put another loop for the print function and had to put quotes around the first two elements of my array. And yes I will put more code tags!
Topic archived. No new replies allowed.