Simple Function Question (i think)

closed account (2wC9GNh0)
Hi peeps,

Heres some code i'm writing up at the mo. ive come across a funny little problem i can't get my head round. All i want to do is i want my 2 functions to keep hold of a value for me based on if it's higher or lower than (when i send other values to it)

Heres my code:

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
#include <iostream>  
#include <string>        
#include <sstream>
#include <windows.h>
#include <mmsystem.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int hstik = 0;
int datik = 0;

int keeplowestdarts (int darts) { int lowestdarts; if (datik == 0) lowestdarts = 500; if (darts < lowestdarts) lowestdarts = darts; return lowestdarts; }

int keephighscore (int score) { int highscore; if (hstik == 0) highscore = 0; if (score > highscore) highscore = score; return highscore; }

int main ()
{
 int scored;
 int darts;

 int gethighscore;
 int getlowestdarts;

 scored = 60;
 keephighscore (scored);
 hstik++;

 darts = 9;
 keeplowestdarts (darts);
 datik++;

 scored = 50;
 keephighscore (scored);

 darts = 15;
 keeplowestdarts (darts);

 getlowestdarts = keeplowestdarts (500);
 gethighscore = keephighscore (0);

 cout<<"\n\n        "<<gethighscore<<"   "<<scored<<"     "<<getlowestdarts<<"   "<<darts<<endl;
 system("pause");
    return 0;
}


I expected on screen " 60 50 9 15 ".

But instead i get " 15 50 15 15 ".

I can't find the wrong bit in the code and there are no compiling errors or warnings either..ide love someone to show me whats wrong here?

PS: The keephighscore function works fine on its own..its been doing this since i made the keeplowestdarts function...Oh, and please ignore the not needed header files.

Thanks peeps.

Benny
Last edited on
I assume the problem is in lines 37, 31, 35, 38. They don't do anything. You need to assign the result to some variable. Otherwise the functions don't change anything.
closed account (2wC9GNh0)
Errr yeah, got what you were saying and made it this way

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
#include <iostream>  
#include <string>        
#include <sstream>
#include <windows.h>
#include <mmsystem.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int hscore = 0;
int ldart = 500;

int keeplowestdarts (int darts) { int l;  l = 0; if (darts < ldart) ldart = darts; return l; }

int keephighscore (int score) { int h; h = 0; if (score > hscore) hscore = score; return h; }

int main ()
{
 int scored;
 int darts;

 scored = 60;
 keephighscore (scored);

 darts = 11;
 keeplowestdarts (darts);

 scored = 100;
 keephighscore (scored);

 darts = 9;
 keeplowestdarts (darts);

 scored = 99;
 keephighscore (scored);

 darts = 10;
 keeplowestdarts (darts);

 cout<<"\n\n        "<<hscore<<"   "<<scored<<"     "<<ldart<<"   "<<darts<<endl;
 system("pause");
    return 0;
}


Works fine now...Thanks Hamsterman
Last edited on
Topic archived. No new replies allowed.