C++ Code not running properly and need some advice for my project! :)

Hello everyone. I'm a beginner, and need help with some code that wouldn't work when i ran it. Though when use the "run line by line" thing it works just fine. I've come with the conclusion that my computer was storing a value or creating one. I tried many different ways to make this program though it only run properly with "run line by line" thing. In the end i tried to comment every thing to understand what was the issue but i obviously didn't find out or i wouldn't be asking for someone's help. The program down below is about making a two characters. Each one has an int strength and int skill. each one of these attributes are initially set to ten. A twelve sided dice and a 4 sided dice are rolled, the both int are divided and rounded down( because their ints). This method is repeated each time with different random value to set the two characters strength and skill attribute's. If you could help me with this project that would mean the world to me, really, THANK YOU FOR YOUR PRECIOUS TIME.


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
148
149
150
151
152
153
154
 //
//  main.cpp
//  Exam


#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>

using namespace std;

int diceRollProcess()
{
    int twelveSidedDice;
    int fourSidedDice;
    float valueReturned;

    srand( time(0) );
    twelveSidedDice = rand() % 12;
    fourSidedDice = rand() % 4;
    
    if(twelveSidedDice > fourSidedDice)
      {
          valueReturned = twelveSidedDice / fourSidedDice;
    }
    else if(twelveSidedDice < fourSidedDice)
      {
        valueReturned = fourSidedDice / twelveSidedDice;
    }
    else
      {
        valueReturned = twelveSidedDice / fourSidedDice;
    }
    return valueReturned;
   
}

int diceRollProcess2()
{
    int twelveSidedDice;
    int fourSidedDice;
    int valueReturned;
    
    srand( time(0) );
    twelveSidedDice = rand() % 12;
    fourSidedDice = rand() % 4;
    
    if(twelveSidedDice > fourSidedDice)
    {
        valueReturned = twelveSidedDice / fourSidedDice;
    }
    else if(twelveSidedDice < fourSidedDice)
    {
        valueReturned = fourSidedDice / twelveSidedDice;
    }
    else
    {
        valueReturned = twelveSidedDice / fourSidedDice;
    }
    return valueReturned;
    
}

int diceRollProcess3()
{
    int twelveSidedDice;
    int fourSidedDice;
    int valueReturned;
    
    srand( time(0) );
    twelveSidedDice = rand() % 12;
    fourSidedDice = rand() % 4;
    
    if(twelveSidedDice > fourSidedDice)
    {
        valueReturned = twelveSidedDice / fourSidedDice;
    }
    else if(twelveSidedDice < fourSidedDice)
    {
        valueReturned = fourSidedDice / twelveSidedDice;
    }
    else
    {
        valueReturned = twelveSidedDice / fourSidedDice;
    }
    return valueReturned;
    
}

int diceRollProcess4()
{
    int twelveSidedDice;
    int fourSidedDice;
    int valueReturned;
    
    srand( time(0) );
    twelveSidedDice = rand() % 12;
    fourSidedDice = rand() % 4;
    
    if(twelveSidedDice > fourSidedDice)
    {
        valueReturned = twelveSidedDice / fourSidedDice;
    }
    else if(twelveSidedDice < fourSidedDice)
    {
        valueReturned = fourSidedDice / twelveSidedDice;
    }
    else
    {
        valueReturned = twelveSidedDice / fourSidedDice;
    }
    return valueReturned;
    
}

int main() {
    

    string again = "yes";
    
    while(again.compare("yes") == 0)
    {
        
        int initialAttributeValue = 10;

        
        
        int Character1Strength = initialAttributeValue + diceRollProcess();
        int Character1Skill = initialAttributeValue + diceRollProcess2();

        
        int Character2Strength = initialAttributeValue + diceRollProcess3();
    
        int Character2Skill = initialAttributeValue + diceRollProcess4();
    
    cout<<initialAttributeValue <<"\n";

    cout<<Character1Strength <<"\n";
    cout<<Character1Skill <<"\n";
    cout<<Character2Strength <<"\n";
    cout<<Character2Skill <<"\n";
        //cout<<"Charater 1:" <<"\n \n" <<Character1Strength <<"\n" <<Character1Skill <<"\n" <<"\n" <<"Charater 2: " <<"\n \n" <<Character2Strength <<"\n" <<Character2Skill ;
        
        
        
      //  cout<<" Do you want to do this again";
        //cin>>again;
    }

    
    return 0;
}
use the same function in main, you don't need 4 copies of it, that is the whole point of functions!! and don't srand but once in the program. It does not hurt but it is pointless to keep reseeding the generator.

rand % 12 is 0 to 11 value. You probably want +1 in there somewhere. You risk divide by 0 and invalid output. same for 4.

rand is a floating point value. 12/4 is 3. so you could just generate a value from 1 to 3.999999999999999999999 ? Not sure what your intent really is.

What exactly is it "not doing right"?

<cstdlib>, <ctime> ... try to avoid mix c and c++ headers.

If you can explain what it isn't doing, I can try to help more.

Last edited on
Topic archived. No new replies allowed.