For loop wont repeat

Oct 16, 2008 at 3:09am
For some reason my for loop is not repeating. I have been looking at this function for a couple hours now. I cant see where the error is. This if a function for my first program, and I am sure it is something very simple that I am over looking.


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
void fight()
{
void outcome();
turn = 0;

uhit = 0;
uhitDam = 0;
ucounter = 0;
ucounterDam =0;
umiss =0;

chit = 0;
chitDam = 0;
ccounter = 0;
ccounterDam = 0;
cmiss = 0;

    if (uhp > 0 && end > 0)
    {


void comp();
comp();

udam = (ustr - cdef);
cdam = (cstr - udef);

        cout << "Engage? yes/no \n";
        cin >> reply;
        if (reply == "yes")
        {
        
            cout << "Fight how many times \n";
            cin >> total;

            if (total > end)
            {
                cout << "You dont have enough endurance \n";
            } // 
            
            else
            {
            cout << "else fight \n;";
                for(c=0;c<total; c++)
                {
                    end -=1;
                    turn++;
                    comp();
                    if (uhp > 0 && end > 0)
                    {
                        
                        void ubat();
                        void cbat();
                        void uwin();

                        while (uhp > 0 && chp > 0)
                            {
                                uspd_act += uspd;
                                cspd_act += cspd;

                                if (uspd_act >= 50)
                                {
                                    ubat();
                                    uspd_act -= 50;
                                } // users turn

                                if (cspd_act >= 50)
                                {
                                    cbat();
                                    cspd_act -= 50;
                                } // comps turn

                            } // while hp > 0
        
                        if (uhp >= chp)
                        {
                            cout << "You won! \n";
                            uwin();
                        } // if win

                        if (uhp < chp)
                        {
                            cout << "You lost. =[ \n";
                            c = total;
                           // ulose();
                        } // if lose
                    } // for if hp > 0
                } // for total
            } // else total
                outcome();
        } // if engage
    } // uhp > 0

    else
    {
        cout << "You are tired, get some rest.";
    } // else

} // fight 


If it is too messy, forgive me. Let me know and I will try to clean it up.
Oct 16, 2008 at 4:39am
when c is declared in the for loop it must be type defined (unless your compiler supports defaulting to int?).
Oct 16, 2008 at 4:40am
What are you inputting for total? Also, why are you defining prototypes inside this function? Oo (lines 3/22)

EDIT: Psyphi is probably right...unless you have c as a global variable or something (not recommended)
Last edited on Oct 16, 2008 at 4:43am
Oct 16, 2008 at 5:22am
Firedraco, Dont I need to declare a prototype inside the function order to use it? I am still not very clear on how it works.

Believe me, at this point I am about as confused by the behavior of the C++ language as I can get. For example: I call the rand(time(0)) in my program. Yet I have not included ctime or cstdlib librarys.

All my variable are global at this point. I found it was easiest to open notepad, and make all my declarations there. I know that is not a good habit to start. But for right now, as I am just starting to learning how to think about the program as a whole; while simultaneously thinking about all the little pieces. It helps, when I decide to add a value, to be able to just move over, declare it, use it. Instead of thinking about every function that will use it, where do I need to first declare it.

For now, all my of my variables are int, except "reply" which is a string.
Oct 16, 2008 at 5:38am
If you are include stdafx.h or something similar, it is probably including ctime/the related libraries. Anyway, about function prototypes, you don't HAVE to, here are two examples:

1
2
3
4
5
6
7
8
int return_int() {
   return 2;
}

int main() {
   int a = return_int(); //works because return_int() is defined before it is used
   cout << a;
}


1
2
3
4
5
6
7
8
9
10
int return_int();

int main() {
   int a = return_int(); //works because prototype is defined before the function is used
   cout << a;
}

int return_int() { //have to define the function somewhere
   return 2;
}


On the subject of global variables...yes it very nice to be able to just do that, but eventually you are going to get into complex things where you are going to want many different variables, and having variables named: up, Up, uP, _UP, _up, etc isn't going to help very much...In you program it probably won't matter too much, but it is a good idea to not get into a bad habit.
Oct 16, 2008 at 6:19am
Nope, all I have are <iostream> and <string> unless I am forgetting one somewhere. I have been working on this program over a week. It already has 11 functions, more are likely to be added b4 it is finished.

Thank you firedraco, I will clean those prototypes up. Do you have any clues as to why that for loop wont repeat?

line 44
for(c=0 ; c<total ; c++)
Oct 16, 2008 at 8:25am
I fixed it.
Seems I used the same variable "int c" in a for loop within the comp() function. No wonder I couldnt find the problem.

The program worked great, comp() ran perfectly, the only problem was that it would only run thru the battle loop once. I figure the problem had to be in this function.

Thanks for your help firedraco.

This will be my first real program. Real in the sence that I took more than an hour or 2 to write. I am very proud of it, and will be adding to planetsourcecode.com when I have finished it. Would it be againt to rules, or would any one object, if I add a link to it here in the forum's lounge?

Many of yall have, and I hope will continue, to help me with it. I would love to hear some feed back from all of yall, after it is complete. Anything you might be able to suggest that would help me write better/cleaner programs.
Oct 16, 2008 at 8:49am
in places where you have used an 'if' conditional and there are only two possible conditions (i.e. greater than or less than) use an 'else' conditional instead of writing another 'if'.
Topic archived. No new replies allowed.