Help with returning

Pages: 12
Heya, I'm new to the forums and to C++ (4 days FTW) and I need some help with this code. It's basically a jumbled mess, but I follow its odds and ends to make sure it works, but I'm having problems with one code. If anyone can help, please help! ='(

Operating System: Windows 7


Warning, the following may hurt your eyes from organization.
Also, this is a segment of a longer code.
My Goal: after submitting Def, the program returns to int again = 0 on line 16.
Problems: after submitting Def, the program ends.


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
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    string name;
    double Def;
     cout<<"Welcome to Konrad's Battle Royale! Please enter in your name."
    <<endl
    <<endl;
    getline (cin, name);


    int again=0;





    cout<<"Defence:";
    cin>>Def;
    cout<<Def;

    again = 1;
    getch();

}


    int again=0;


    double Def;


    cout<<"Defence:";
    cin>>Def;
    cout<<Def;

    again = 1;
    getch();

}
Last edited on
Before we start helping you with the code, it would be helpful if you put it in code tags:
[code]int main()[/code] -> int main()

My Goal: after submitting Def, the program returns to int again = 0 on line 15.

So you want your program to loop back to the start? Try using a while loop :)
http://cplusplus.com/doc/tutorial/control/
Last edited on
Seriously, next time pls use the code tag to post ur code..
and as xander said, i think you just need to use the while loop in the program.. refer to the tutorial given above..
Because you've admitted to being new I won't get on your case about using objects for this kind of stuff, but seriously do the entire tutorial on this site and pay extra attention in the "classes" section of it. This will be time VERY well spent.

That being said, stuff like this is why I encourage people to go Old-School-Geek and read up on a few Pencil Paper RPG's, it avoids nasty looking combat systems like this. You have the user entering way more variables then in necessary, "HP" "damage" and "pain" should all be functions of your "Def" variable, and you don't even need all of them. Simularly, "attack" should come from "Str". By automating stuff like this you spend less time on set up and more time on playing the game.
Hey, thanks for the tips, like I said that was just a segment of the whole program, and sorry about not using the codes, like I said, I'm new. As for the game I have (had), thank you very much to Xander314 because the while loop works, but I still have 1 problem. At the very end of my code is a
1
2
3
4
5
6
cout<<"Do you want to play again?";
cin>>PlayAgain;
if (PlayAgain = "yes")
again = 1;
else
return 0;


This is where I mainly need that again system to work.
Line 3 is your problem, a single '=' character means assignment. You want to use a double "==" character for comparison. In both cases do not use the quotes.
fixed it, Computergeek01, but it won't go back to the start. It seems to overlook again = 1. Is there a specific directive I need (if that's what you call them)?

Or am I doing something wrong with my int again=0; line?
Last edited on
Have you used a loop?
The if statment should be inside of the while loop.
Yeah, I used a while loop on the statistics, but can I while loop something inside a do loop? Like, I have a do/while loop for my combat sequences. So, can I make a while loop that long, and into a do function, without effecting anything to drastically?
Last edited on
What makes you think that

again=1

will make the code go back to the start of main?

again isn't some magic word; you have to do something to make code repeat.

In pseudo-code, something like

1
2
3
4
5
6
7
8
if (again ==1)
{
  // Do not end but instead start again
}
else
{
  // Finish
}
Last edited on
What you want to do is encase your program in a while loop with 'again' as a paramter. So something like this:

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
bool again = true;
string name, PlayAgain;
double Def;
while(again)
{
    // Welcome
    cout << "Welcome to Konrad's Battle Royale! Please enter in your name." <<endl;
    cin >> name;

    // Enter Defense
    cout<<"Defense:";
    cin >> Def;
    cout << Def << endl;

    cout << "Do you want to play again?";
    cin >> PlayAgain;
    if (PlayAgain.compare("yes"))
    {
        again = true;
    }
    else
    {
         again = false;
    }
}
Last edited on
I got the again line from some guy on Yahoo answers, and it doesn't have to be again, it can be any word as long as you go int (variable) = (number);

Although, yahoo answers isn't that trustworthy.

Anyways, the current state is I need to get you to restart the program without having to reenter your name, and I don't think the While loop will work ='(

Moschops, try:
return main ();
Last edited on
Then move the "enter name" segment of code out of the while loop. A while loop will definitely work for your case. Why wouldn't it?
It's so hard to explain it because it's not a small code, LoL, but it might work, IDK.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
While Loop{

}

Do{

While{

}

While{

}

While{

}

}While


With that being said, can I add another While to fit over the do, and if so, how?

On another note, is there a place I can upload the program when it is finished?
Last edited on
The code in your original post. Is that a segment of code from the whole program? I don't understand exactly what you are trying to achieve but from what I speculate is that you want your program to jump from the end to the beginning (or near the beginning). There is no jump statements in C++ so the best way to iterate again until the user does not input "yes" would be using a while loop.
Last edited on
@ OP: This is called an "Embedded Loop" and is really easy in C++, there's no magic trick to it:
1
2
3
4
5
6
7
8
while(/*These Parameters Are True*/) /*Start First While Loop*/
   {
       while(/*These Other Parameters Are True*/) /*Start Second While Loop*/
       {  
        /*Run This Code*/
         } /*End Of Second While Loop*/

    } /*End Of First While Loop*/
Last edited on
Yes, GodPyro, that is a small segment of a larger code.

I think I got it, Computergeek01 put in that last piece of the puzzle. Let's just see if it works =)
SUCCESS! Thank you everyone! Now that the play again function is working, I just need to set up a scoring system =D, again, Thank you!
You're welcome. :) Glad it got sorted out.
Pages: 12