Calling a specific value in a function?

I've composed a code solely for practice, and I'm having trouble on calling a specific value in a function. Under the 'wallet' while loop, where I call the function with w+job(1), it loads the entire function rather than just adding the value of G. How can I just use the value of G, instead of loading the entire function?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
#include <cstdlib>

using namespace std;

float job (float g);

int main()
{
    float b;
    b=0;
    float a;
    a=0;
    float w;
    w=0;
    string sto;
    string stt;
    string wal;
    
    cout << "Welcome to Consbank!\n";
    cout << "Type 'withdraw', 'deposit', 'balance', 'job', or 'wallet'.\n";
    cout << "'balance' shows the current amount of money in the bank.\n";
    cout << "'deposit' adds money of choice to your account.\n";
    cout << "'withdraw' subtracts money from your account.\n";
    cout << "'job' gets you a job and makes money.\n";
    cout << "'wallet' shows the current amount of money in your wallet\n";
    cout << "Type 'exit' to exit the program.\n";
    cout << "\n";
    
    do{getline (cin,sto);
    
    while (sto=="balance"){
          b=b*100;
          b= (int) b;
          b= (float) b;
          b= b/100.0;
       cout << "$" << b;
       cout << "\n";
       sto = "c";
       cout << "\n";
       break;
       }
       
    while (sto=="wallet"){
          w=w+job(1);
          cout << "$" << w;
          cout << "\n";
          sto = "c";
          cout << "\n";
          break;
          }
          
    while (sto=="job"){
          cout << "You got a job!\n";
          job(1);
          sto = "c";
          cout << "\n";
          break;
          }
       
    while (sto=="deposit"){
       cout << "How much would you like to deposit?\n";
       getline (cin,stt);
       stringstream(stt) >> a;
       cout << "\n";
       if (a<=w)
          b = b+a;
          sto = "c";
       
       if (a>w)
          cout << "You don't have that much money!\n\n";
          
          break;
       }
       
    while (sto=="withdraw"){
       cout << "How much would you like to withdraw?\n";
       getline (cin,stt);
       stringstream(stt) >> a;
       cout << "\n";
       if (a<=b)
          b = b-a;
          w = a+w;
          sto = "c";
       
       if (a>b)
          cout << "You don't have that much money!\n\n";
          
          break;
       }
       
    while (b<=-1){
       cout << "You can't have negative money!\n\n";
       b=0;
       }
          
    while (b>=999999){
          cout << "Get a better bank account! You can't store more than 999999!\n";
          b=999999;
          break;
          }
       
       } while (sto!="exit");
       return 0;
}

float job (float g=0)
{
      srand((unsigned)time(0));
      int jsel;
      jsel = rand() % 1+4;
      g=0;
      
      while (jsel==1){
            cout << "You made $10.64!\n";
            g=g+10.64;
            jsel= rand() % 1+3;
            break;
            }
            
      
      while (jsel==2){
            cout<< "You made $25.00!\n";
            g=g+25;
            jsel= rand() % 1+4;
            break;
            }
      
      while (jsel==3){
            cout<< "You made $64.92!\n";
            g=g+64.92;
            jsel= rand() % 1+2;
            break;
            }
            
      
      while (jsel==4){
            cout<< "You made $34.50!\n";
            g=g+34.50;
            jsel= 1;
            break;
            }
            
}
Last edited on
Line 114: jsel = rand() % 1+4; //this generates 4 or 5
If you need to see more examples of the rand function look here: http://cplusplus.com/reference/clibrary/cstdlib/rand/
Well, I just fixed the randomizer, now all I have to do is figure out how to add g to w without activating the entire function.
Your compiler should give you warning that job() doesn't return anything, and I'm curious on what did w = w + job(1); (line 48) produce... :|

- hope you've put srand() in main() already
- float b; b = 0; can (and usually is) be declared in one line float b = 0;
Last edited on
Topic archived. No new replies allowed.