Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What is wrong with my code? (c++)

+1 vote

1 Answer

0 votes
answered Mar 10, 2023 by Peter Minarik (89,130 points)

You're using rand() wrong. Check the specification of how it's supposed to be used (it doesn't take any arguments). You would probably want to have different random numbers every time too (see srand()).

You should also rather use std::endl instead of '\n' as std::endl is platform independent, while '\n' is Unix-based system specific.

You also don't need to use the exit() statement, you can just let your program leave through the regular code path (leave the loop, leave the main() function).

I've fixed up your code, see it below:

//Car rental system.
//Main objective: set a global variable named "balance" and then assign it to a random value between 60k-500k.
//Cars: BMW, Mercedes, Bugatti, Lamborghini.
//Times: 5 hrs, 10 hrs, 1 day.
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

//Main variable (Ballance)
int ballance;

int randomBalance(int min, int max)
    int range = max - min;
    return min + rand() % range;

int main()
    int ballance = randomBalance(60000, 500000) + 1;
    int answer;
        cout << "Welcome to my car rental system project! (Created in c++)" << endl;
        cout << "How can I help you?" << endl;
        cout << "1. Show ballance" << endl;
        cout << "2. Rent Car" << endl;
        cout << "3. Exit." << endl;
        cin >> answer;
        switch (answer)
            case 1:
                cout << "Your ballance is " << ballance << "!" << endl;
            case 2:
                //Not yet
            case 3:
                // Nothing to do here.
    } while (answer != 3);
    return 0;
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.