-3

I'm making a simple program to count user balance whenever there's a positive integer input between 1-5 from user. I only make the code for the first option, but something weird is happening from the output, here's my code :

#include <iostream>
using namespace std;
int balance = 100000;
int iNet = 59900;

int internet(int iNet, int code, int remain){
        if(code == 1){
            remain = balance - iNet;
            cout << "Price = " << iNet << endl;
            cout << "Remaining credit = " << remain <<endl;
        } 
}

int main(){
    int code, remain;
    
    cin >> code;
    cout << internet(iNet, code, remain);
    return 0;
}

But when i run the program, it shows like this :

user input

1 

program output

Price = 59900
Remaining credit = 40100
4745728

I have no idea where's the 4745728 coming from.

  • 3
    You never `return` anything from `internet()` function. May I suggest getting [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? Your current learning source seems to fail at explaining the basics of coding. – Yksisarvinen Apr 29 '21 at 21:13
  • Already tried to put `return 0;`, the result is the ouput also put `0` which is not what i want. – Hasbiallah Al Amin Apr 29 '21 at 21:14
  • Then what do you want? Note that you must always return exactly 1 integer. – drescherjm Apr 29 '21 at 21:14
  • @drescherjm remove the `4745728` thing – Hasbiallah Al Amin Apr 29 '21 at 21:15
  • `return 0;` removes the `4745728`. Maybe you wanted to declare the function as void and not return a value. In that case you would also change `cout << internet(iNet, code, remain);` to `internet(iNet, code, remain);` – drescherjm Apr 29 '21 at 21:15
  • @drescherjm i tried to do that too! But the program turns out printing the `1` on the console – Hasbiallah Al Amin Apr 29 '21 at 21:16
  • `cout << internet(iNet, code, remain);` this `cout` here says print the integer that the function `int internet(int iNet, int code, int remain){` returns. – drescherjm Apr 29 '21 at 21:17
  • LOL it worked! just remove the cout :D Newbie here, pardon for being so dumb :) @drescherjm – Hasbiallah Al Amin Apr 29 '21 at 21:18
  • 1
    You need to change the function to `void internet(int iNet, int code, int remain){` also. Otherwise your program has undefined behavior. – drescherjm Apr 29 '21 at 21:19

1 Answers1

2

If you don't want to return anything from a function, make the return type void:

void internet(int iNet, int code, int remain){
// ^
        if(code == 1){
            remain = balance - iNet;
            cout << "Price = " << iNet << endl;
            cout << "Remaining credit = " << remain <<endl;
        } 
}

Then compiler will not allow you to print a rubbish value anymore and you will have to fix your main:

int main(){
    int code, remain;
    
    cin >> code;
    internet(iNet, code, remain);
    //^ don't print anything, just call the function
    return 0;
}
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52