Click here to Skip to main content
14,970,251 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

typedef struct {
    string type;
    string plate;
    string model;
    int period;
    double price;
} Car;

void display(const Car &car) {
    cout << "Type: " << car.type << endl;
    cout << "Plate: " << car.plate << endl;
    cout << "Model: " << car.model << endl;
    cout << "Period: " << car.period << endl;
    cout << "Price: " << car.price << endl;
}

void insert(vector<Car> &cars) {
    Car temp;
    cout << "Enter car type: ";
    getline(cin, temp.type);
    cout << "Enter car plate: ";
    getline(cin, temp.plate);
    cout << "Enter car model: ";
    getline(cin, temp.model);
    cout << "Enter period of rental: ";
    cin >> temp.period;
    cout << "Enter rental price: ";
    cin >> temp.price;
    fflush(stdin);
    cars.push_back(temp);
}

void remove(vector<Car> &cars) {
    string plate;
    cout << "Enter car plate to remove: ";
    cin >> plate;
    vector<Car>::iterator i;
    for (i = cars.begin(); i<cars.end(); ++i) {
        if ((i->plate == plate)) {
            cars.erase(i);
            break;
        }
    }
}

void search(vector<Car> &cars) {
    string plate;
    cout << "Enter car plate to search: ";
    cin >> plate;
    vector<Car>::iterator i;
    for (i = cars.begin(); i<cars.end(); ++i) {
        if ((i->plate == plate)) {
            display(*i);
            return;
        }
    }
    cout << "Searched car was not found." << endl;
}

void update(vector<Car> &cars) {
    string plate;
    cout << "Enter car plate to update: ";
    cin >> plate;
    vector<Car>::iterator i;
    for (i = cars.begin(); i<cars.end(); ++i) {
        if ((i->plate == plate)) {
            cout << "Match found!!" << endl;
            Car temp;
            cout << "Enter new car type: ";
            getline(cin, temp.type);
            cout << "Enter new car plate: ";
            getline(cin, temp.plate);
            cout << "Enter new car model: ";
            getline(cin, temp.model);
            cout << "Enter new period of rental: ";
            cin >> temp.period;
            cout << "Enter new rental price: ";
            cin >> temp.price;
            fflush(stdin);
            cars.erase(i);
            cars.insert(i, temp);
            return;
        }
    }
}

void displayCars(vector<Car> &cars) {
    vector<Car>::iterator i;
    for (i = cars.begin(); i<cars.end(); ++i) {
        display(*i);
        cout << "-------------------------------------" << endl;
    }
}

bool cmp(const Car& c1, const Car& c2) {
    return (c1.price > c2.price);
}

void sortByPrice(vector<Car> &cars) {
    vector<Car> temp(cars);
    sort(temp.begin(), temp.end(), cmp);
    cout << "Cars sorted by price high to low: " << endl;
    displayCars(temp);
}

int main() {
    vector<Car> cars;
    int choice {0};
    while(choice!=7) {
        cout << "1. Add new record" << endl;
        cout << "2. Remove existing record" << endl;
        cout << "3. Search record" << endl;
        cout << "4. Update record" << endl;
        cout << "5. Display records" << endl;
        cout << "6. Display from price high to low" << endl;
        cout << "7. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;
        fflush(stdin);
        if (choice == 1) {
            insert(cars);
            cout << "Record successfully added!" << endl;
        } else if (choice == 2) {
            remove(cars);
        } else if (choice == 3) {
            search(cars);
        } else if (choice == 4) {
            update(cars);
        } else if (choice == 5) {
            cout << "Following cars are present in the database: " << endl;
            displayCars(cars);
        } else if (choice == 6) {
            sortByPrice(cars);
        } else if (choice != 7) {
            cout << "Wrong choice entered. Enter again" << endl;
        }
    }
    
    return 0;
}


What I have tried:

i tried to do change this code form c++ to c programming but i got problem
Posted
Updated 20-Jun-21 4:57am
Comments
Richard MacCutchan 20-Jun-21 9:54am
   
No, because it uses classes that you cannot create in pure C. Don't waste your time with it, but write your assignment by following the instructions.
Greg Utas 20-Jun-21 10:32am
   
The bizarre thing is typedef struct, a blatant C-ism mixed in with all this other C++ stuff.
Richard MacCutchan 20-Jun-21 11:36am
   
Yet another example of why most of these samples are such poor code.

You haven't tried anything. This isn't a question, but a request for us to do your work.

All the #includes are C++. Take them out and find C equivalents for what no longer compiles. For example, the C equivalent of iostream is stdio.h. See here[^]. You will need to find equivalents for vector, cin, cout, getline, iterator, sort, and probably other things that I didn't notice while quickly scanning this code.

This isn't an exercise for a beginner. Most C++ to C conversion requests posted here have a lot less work to do than this, so you must have some experience. Good luck.
   
v2
Quote:
Hello everyone can you convert this code to C programming

Yes I can, but I will not.
It is like for a car, when you need some repair that you don't know how to do, you resort to a mechanic and in the end you pay him for the job.
Since converting a program from C++ to C is a though task because of missing features, why do your homework in wrong language in first place ?
It would be easier to do the program in C from scratch.
   
This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900