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's the error? Please help (C++)

+3 votes
asked Mar 1, 2023 by Eidnoxon (5,160 points)
I'm trying out new projects in c++, but now it has lots of errors and I can't find the reason why. I thought of a typo but no, there was no typo in the whole thing. If you can help, I'd appreciate it.

Source code: https://onlinegdb.com/K8YDhdhiBm

8 Answers

0 votes
answered Mar 1, 2023 by Pratik Jaiswal (140 points)
/*check this correction */

//Libraries I need

#include <iostream>

#include <stdlib.h>

#include <cstdlib>

//Functions

void showBallance();

void dePosit(double amount);

void WithDraw(double amount);

void quit();

using namespace std;

//Main Variable(The user's ballance)

double ballance = 0.00;

int main()

{

do{

int answer;

cout << "Choose what you want me to do:\n";

cout << "1. Show Ballance\n";

cout << "2. Deposit\n";

cout << "3. Withdraw\n";

cout << "4. Exit\n";

cin >> answer;

switch(answer){

case 1: showBallance();

break;

case 2:

double amount;

cout << "How much money do you want to deposit? (Max: $400)\n";

cin >> amount;

if (amount > 400)

{

cout << "You can't deposit more money than maximum. Try again.";

cin >> amount;

}

else {

dePosit(amount);

}

break;

case 3:

double yoanswer;

cout << "How many money do you want to withdraw from your bank account? (Your ballance currently is " << ballance << ")";

cin >> yoanswer;

WithDraw(double yoanswer);

break;

case 4:

quit();

break;

default: cout << "I can't understand you. Goodbye.";

quit();

break;

}

}while (answer != 4);

return 0;

}

void showBallance()

{

cout << "Your ballance is " << ballance << ". If you wish to add more money to your bank account there is an option to deposit.\n";

return 0;

}

void dePosit(double amount)

{

ballance += amount;

cout << "Your ballance currently is " << ballance << ".\n";

return 0;

}

void WithDraw(double amount)

{

ballance -= amount;

if (ballance < 0)

{

cout << "You can't withdraw more money that is on your bank account.\n";

}

return 0;

}

void quit()

{

exit();

}
0 votes
answered Mar 1, 2023 by hamzaking11 (420 points)

There are few errors in your source code that need correctifon.
The function exit() in the quit() function, The variable answer is declared inside the do-while loop, but it is being used outside of the loop in the while statement. The variable answer is declared inside the do-while loop, but it is being used outside of the loop in the while statement.

Following code are helpful to you it's fixed errors.

//Libraries I need
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
using namespace std;

//Functions
void showBallance();
void dePosit(double amount);
void WithDraw(double amount);
void quit();

//Main Variable(The user's ballance)
double ballance = 0.00;

int main()
{
    int answer; // Moved the variable outside the do-while loop
    do {
        cout << "Choose what you want me to do:\n";
        cout << "1. Show Ballance\n";
        cout << "2. Deposit\n";
        cout << "3. Withdraw\n";
        cout << "4. Exit\n";
        cin >> answer;

        switch (answer) {
        case 1:
            showBallance();
            break;
        case 2:
            double amount;
            cout << "How much money do you want to deposit? (Max: $400)\n";
            cin >> amount;
            if (amount > 400)
            {
                cout << "You can't deposit more money than maximum. Try again.";
                cin >> amount;
            }
            else {
                dePosit(amount);
            }
            break;
        case 3:
            double yoanswer;
            cout << "How many money do you want to withdraw from your bank account? (Your ballance currently is " << ballance << ")";
            cin >> yoanswer;
            WithDraw(yoanswer); // Fixed the function call
            break;
        case 4:
            quit();
            break;
        default:
            cout << "I can't understand you. Goodbye.";
            quit();
            break;
        }

    } while (answer != 4);


    return 0;
}

void showBallance()
{
    cout << "Your ballance is " << ballance << ". If you wish to add more money to your bank account there is an option to deposit.\n";

    // Removed "return 0;" since the function is of type "void"
}

void dePosit(double amount)
{
    ballance += amount;
    cout << "Your ballance currently is " << ballance << ".\n";

    // Removed "return 0;" since the function is of type "void"
}

void WithDraw(double amount)
{
    ballance -= amount;
    if (ballance < 0)
    {
        cout << "You can't withdraw more money that is on your bank account.\n";
    }

    // Removed "return 0;" since the function is of type "void"
}

void quit()
{
    exit(0); // Added parameter to exit function
}

commented Mar 2, 2023 by Eidnoxon (5,160 points)
Can you please make it a source code?
0 votes
answered Mar 2, 2023 by Peter Minarik (87,810 points)

I've seen you've been given help regarding fixing your code so it will compile and run.

I'd like to give some suggestions on the logic.

I'd recommend rewriting your Withdraw function like this:

void WithDraw(double amount)
{
    if (balance >= amount)
    {
        ballance -= amount;
    }
    else
    {
        cout << "You can't withdraw more money that is on your bank account.\n";
    }
}

This way the balance cannot go negative.

Also, since you're using C++, you could do a nice encapsulation with classes.

E.g. you'd have a Wallet class that would hold the balance field and the related manipulation functions (not the quit one though, that belongs to the application, not the wallet).

0 votes
answered Mar 3, 2023 by Kobitxe (150 points)
Te he hecho que funcione el código. He quitado las mayusculas de las variables para que sea todo mas legible. Además habian varias funciones mal definidas.

Te adjunto la manera en la que te lo he corregido:

//Libraries I need
#include <iostream>
#include <stdlib.h>
#include <cstdlib>

using namespace std;

//Functions
void showballance(double);
void deposit(double);
void withdraw(double, double, double);

//Main Variable(The user's ballance)
double ballance = 0.00;

int main()
{
    int answer = 0;
    while (answer != 4)
    {
        
        cout << "Choose what you want me to do:\n";
        cout << "1. Show Ballance\n";
        cout << "2. Deposit\n";
        cout << "3. Withdraw\n";
        cout << "4. Exit\n";
        cin >> answer;
        
        switch(answer){
            
            case 1: showballance(ballance);
                break;
                
            case 2:
                double amount;
                    cout << "How much money do you want to deposit? (Max: $400)\n";
                    cin >> amount;
                    if (amount > 400)
                        {
                            cout << "You can't deposit more money than maximum. Try again.";
                            cin >> amount;
                        }
                    else
                        {
                            deposit(amount);    
                        }
                break;
                
            case 3:
                double yoanswer;
                cout << "How many money do you want to withdraw from your bank account? (Your ballance currently is " << ballance << ")";
                cin >> yoanswer;
                withdraw(ballance, amount, yoanswer);
                break;
                
            case 4:
                return 0;
                break;
                
            default: cout << "I can't understand you. Goodbye.";
            return 0;
            
            break;
        
                
    }
    
}
    
    return 0;
}

void showballance(double ballance){
    cout << "Your ballance is " << ballance << ". If you wish to add more money to your bank account there is an option to deposit.\n";
}

void deposit(double amount){
    ballance += amount;
    cout << "Your ballance currently is " << ballance << ".\n";
}

void withdraw(double ballance, double amount, double yoanswer){
    ballance == ballance - amount;
    if (ballance < 0)
    {
        cout << "You can't withdraw more money that is on your bank account.\n";
    }
}
0 votes
answered Mar 3, 2023 by Piyush Agrawal (150 points)
https://onlinegdb.com/fp7Lh_GzB

this is showing no error and executed easily.
0 votes
answered Mar 13, 2023 by Nurul Iman (180 points)
//Libraries I need

#include <iostream>
#include <stdlib.h>
#include <cstdlib>

void showBallance();
void dePosit(double amount);
void WithDraw(double amount);
void quit();

using namespace std;

double ballance = 0.00;

int main()
{
    int answer;
    do {
        cout << "Choose what you want me to do:\n";
        cout << "1. Show Balance\n";
        cout << "2. Deposit\n";
        cout << "3. Withdraw\n";
        cout << "4. Exit\n";
        cin >> answer;

        switch(answer) {
            case 1:
                showBallance();
                break;
            case 2:
                double amount;
                cout << "How much money do you want to deposit? (Max: $400)\n";
                cin >> amount;
                if (amount > 400) {
                    cout << "You can't deposit more money than maximum. Try again.";
                    cin >> amount;
                } else {
                    dePosit(amount);
                }
                break;
            case 3:
                double yoanswer;
                cout << "How much money do you want to withdraw from your bank account? (Your balance currently is " << ballance << ")\n";
                cin >> yoanswer;
                WithDraw(yoanswer);
                break;
            case 4:
                quit();
                break;
            default:
                cout << "I can't understand you. Goodbye.\n";
                quit();
                break;
        }

    } while (answer != 4);

    return 0;
}

void showBallance()
{
    cout << "Your balance is " << ballance << ". If you wish to add more money to your bank account there is an option to deposit.\n";
}

void dePosit(double amount)
{
    ballance += amount;
    cout << "Your balance currently is " << ballance << ".\n";
}

void WithDraw(double amount)
{
    ballance -= amount;
    if (ballance < 0) {
        cout << "You can't withdraw more money that is on your bank account.\n";
    }
}

void quit()
{
    exit(0);
}
0 votes
answered Apr 12, 2023 by Anushka Arora (140 points)
Hello
I have checked your code ,and it contain some valid errors that is in the function showbalance , deposit , and withdraw you have initiated them with void that means no return type and then you have used return 0;
That is something not going with so pleease remove this .
And also for exit() i think you should use exit function with the parameter exit(0) or exit(1)
0 votes
answered May 2, 2023 by Ff Gamer (140 points)
#include <iostream>

#include <stdlib.h>

#include <cstdlib>

//Functions

void showBallance();

void dePosit(double amount);

void WithDraw(double amount);

void quit();

using namespace std;

//Main Variable(The user's ballance)

double ballance = 0.00;

int main()

{   int answer;

do{

cout << "Choose what you want me to do:\n";

cout << "1. Show Ballance\n";

cout << "2. Deposit\n";

cout << "3. Withdraw\n";

cout << "4. Exit\n";

cin >> answer;

switch(answer){

case 1: showBallance();

break;

case 2:

double amount;

cout << "How much money do you want to deposit? (Max: $400)\n";

cin >> amount;

if (amount > 400)

{

cout << "You can't deposit more money than maximum. Try again.";

cin >> amount;

}

else {

dePosit(amount);

}

break;

case 3:

double yoanswer;

cout << "How many money do you want to withdraw from your bank account? (Your ballance currently is " << ballance << ")";

cin >> yoanswer;

WithDraw(yoanswer);

break;

case 4:

quit();

break;

default: cout << "I can't understand you. Goodbye.";

quit();

break;

}

}while (answer != 4);

}

void showBallance()

{

cout << "Your ballance is " << ballance << ". If you wish to add more money to your bank account there is an option to deposit.\n";

}

void dePosit(double amount)

{

ballance += amount;

cout << "Your ballance currently is " << ballance << ".\n";

}

void WithDraw(double amount)

{

ballance -= amount;

if (ballance < 0)

{

cout << "You can't withdraw more money that is on your bank account.\n";

}

}

void quit()

{

}
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.
...