firstly here is the code i'm working on in visual studio

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

struct Car
{
string r_number,make_of_car,model_of_car;
int mileage;
int age_of_car;
};

void main ()
{
int i,num;
Car *car1;

cout << "Please enter the number of cars: ";
cin >> num;

try
{
car1= new Car[num];
}
catch(...)
{
cout <<"Could not allocate memory!";
}

for (i=0; i<num; i++)
{
cout << "\nCar " << (i+1) <<" Registration number: ";
cin >> car1[i].r_number;

cout << "Car " <<( i+1) <<" Make : ";
cin >> car1[i].make_of_car;

cout << "Car " << (i+1) <<" Model: ";
cin >> car1[i].model_of_car;

cout << "Car " <<( i+1) <<" Total mileage: ";
cin >> car1[i].mileage;
if (car1[i].mileage < 0)
// error if age of car is neagitive
printf("Fatal Error: A car cannot have negative mileage.\n\n");

cout << "Car " <<( i+1) <<" age: ";
cin >> car1[i].age_of_car;
if (car1[i].age_of_car < 0)
// error if age of car is neagitive
printf("Fatal Error: A car cannot have negative age number.\n\n");
}

int opt=0;
do
{
system("cls");
cout << "**** CAR EVALUATION SYSTEM ****\n";
cout << "1. Search For a Car\n";
cout << "2. Generate Performence Report\n";
cout << "3. Exit\n";
cout << "Please choose an option:";
cin >> opt;
switch(opt)
{
case 1: 
system ("cls");
cout << "Please enter car's registration number: ";break;

case 2: 
system ("cls");
cout<<"Car registration\tAnnual average mileage\tMileage type";break;

case 3: 
exit (0);
}
cin.ignore();cin.ignore();

}while(opt != 3);

}

basicaly where it says case 1 the program needs to clear the screen and ask the user to enter the car’s registration number that s/he wishes to retrieve from the system( which i've done.but how to do this bit,i tried doing it few times but failed:(..here it goes)----- If the car’s detail is found in the system, the program should display the car’s name, make, model, current total mileage, and age. Otherwise, the program should display an appropriate error message to inform the user that the registration number was not found in the system. When this task is finished, the program should return to the main menu.
any help wud be greatly appriciated..thanx in advance

Recommended Answers

All 2 Replies

do something like

int j = 0
while (j < num && strcmp(enteredNum, car1[j].r_number) !=0)
{
j++
}

if (j < num)
{
  cout << car1[j].milage << car1.[j].(whatever other data);
}
else 
{
  cout << "car not found\n"
}

of course that is a lot to cram in a switch statement so you may want to make the above into a function
also the enteredNum i am referencing is what ever variable the number the user enters to search. I would suggest accepting their data as a string, that way you can use strcmp

wait i'm thinking dumb i thought you had the r_number as a string.
change the strcmp(.....) part to

while (j < num && enteredNum != car1[j].r_number)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.