Can someone tell me what wrong with this code
i am supposed to create an array of struct of cars

and then add 1 to the car sold
then find the inventory value comprising of all car values

this is my infile
lexus IS 35000 10
bmw 740 90000 5

and this is my code

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

struct carType
{
       string make;
       string model;
       int cost;
       int numberOnHand;
};

void init (carType cars[], int currentCarCounter);
void print (carType cars[], int currentCarCounter, int Max_car_types);
void addToInventory (carType cars[], int currentCarCounter);
int inventoryValue(carType cars[], int currentCarCounter);

int main()
{

int Max_car_types;
cout<<"Please enter the maximum amount of number in the dealership"<<endl;
cin>>Max_car_types;
carType cars[Max_car_types];
int currentCarCounter=0;
init(cars,currentCarCounter);
print(cars,currentCarCounter,Max_car_types);

cout<<"Value of all cars in inventory is $"<<inventoryValue(cars,currentCarCounter)<<endl;
cout<<endl;


cout<<"********* Adding a car to our inventory list **********"<<endl;
cout<<endl;

addToInventory(cars,currentCarCounter);

cout<<"Value of all cars in inventory is $"<<inventoryValue(cars,currentCarCounter)<<endl;

print(cars,currentCarCounter,Max_car_types);
system("pause");
return 0;
}

void init (carType cars[], int currentCarCounter)
{
ifstream infile;
infile.open("Lab5.txt");
while(infile)
{
infile>>cars[currentCarCounter].make>>cars[currentCarCounter].model>>cars[currentCarCounter].cost>>cars[currentCarCounter].numberOnHand;
currentCarCounter++;
}
infile.close();
}

void print (carType cars[], int currentCarCounter, int Max_car_types)
{
     cout<<" Car Inventory"<<endl;
     int i=0;
     currentCarCounter=1;
     while(i<Max_car_types)
     {
cout<<currentCarCounter<<" "<<cars[i].make<<" "<<cars[i].model<<" "<<cars[i].cost<<" "<<cars[i].numberOnHand<<endl;
i++;
currentCarCounter++;
 }
cout<<endl;
cout<<endl;
}

void addToInventory (carType cars[], int currentCarCounter)
{
     int i;
cout<<"Which car did you just purchase to add to our inventory? "<<endl;
cout<<endl;
cout<<"Enter the number listed to the left to select a car."<<endl;
cin>>i;
i--;
cars[i].numberOnHand+=1;

cout<<"Just purchased another "<<cars[i].make<<" to add to our sales room"<<endl;
cout<<"Had 5, now there are "<<cars[i].numberOnHand<<endl;
cout<<endl;
}

int inventoryValue(carType cars[], int currentCarCounter)
{
int sum=0;
int i=0;
while(i<2)
{
sum+=cars[i].cost*cars[i].numberOnHand;
}
return sum;
}

it just stops running after it prints the first inventory

Edited 6 Years Ago by TinhornAdeniyi: n/a

The problem lies in the while() loop in inventoryValue(); the index variable, i, is never getting incremented, so it loops indefinitely on the zeroth element of the array. I would recommend changing it to a for() loop to make the intent clearer, in any case:

int inventoryValue(carType cars[], int currentCarCounter)
{
    int sum=0;
    for(int i = 0; i<currentCarCounter; i++)
    {
        sum+=cars[i].cost*cars[i].numberOnHand;
    }
    return sum;
}
This question has already been answered. Start a new discussion instead.