|  | |
06-29-2009, 05:19 PM
|
#21 (permalink)
|
Join Date: Sep 2005 Location: Toronto, Canada Posts: 5,466
| Re: C++ Help Ah okay now you're getting into more advanced coding. Not too hard for your level though. Give me a few minutes and I'll post up an example of what you want. |
| |
06-29-2009, 05:47 PM
|
#22 (permalink)
|
*^[#@"'.,;:\|/:;,.'"@#]^* Join Date: Dec 2006 Location: Leicester, England Posts: 3,655
| Re: C++ Help Okay, thanks
*worried*
:o |
| |
06-29-2009, 10:05 PM
|
#23 (permalink)
|
Join Date: Sep 2005 Location: Toronto, Canada Posts: 5,466
| Re: C++ Help Sorry man forgot about something I had to do! Here it is: Code: #include <iostream>
using namespace std;
int main()
{
char answer[5];
while(strcmp("exit", answer))
{
cout << "Do you like sushi? Type yes or no (or exit to quit): ";
cin >> answer;
if(!strcmp("yes", answer))
{
cout << "\nWow, you like sushi!\n\n";
}
else if(!strcmp("no", answer))
{
cout << "\nGood, sushi's gross!\n\n";
}
else if(!strcmp("exit", answer))
{
cout << "\nExiting...\n\n";
}
else
{
cout << "\nWell if you can't even type yes or no..."
<< "\nhow do you expect me to trust your opinion?\n"
<< endl;
}
}
return 0;
}
I'm not sure if you've learned about while statements but they are extremely useful. It basically tells a portion of the program to keep looping until a certain case is reached. In this case it's if the user types "exit".
Last edited by Baez; 06-29-2009 at 10:09 PM.
|
| |
06-30-2009, 10:12 AM
|
#24 (permalink)
|
*^[#@"'.,;:\|/:;,.'"@#]^* Join Date: Dec 2006 Location: Leicester, England Posts: 3,655
| Re: C++ Help Thanks man. Yeh it's touched on While and Do-While in the book, wasn't to sure how to use them too well though.
That pretty much summed it up thoguh |
| |
07-01-2009, 07:34 PM
|
#25 (permalink)
|
*^[#@"'.,;:\|/:;,.'"@#]^* Join Date: Dec 2006 Location: Leicester, England Posts: 3,655
| Re: C++ Help Very odd problem. Me and my friends are just trying to make a console based game.
However, i have hit a very strange and confusing problem. After about 70 lines of code, i have a switch statement. However, i try to add two constants (i think they're called that..) that were defined earlier on outside the switch by console input, but the result is totally random and nothing like correct.
for example, outside the switch i have two constants, 'strength' and 'defence' they are both defined as 10 by console input just for testing. But if i try to define a new constant anywhere inside the switch like this:
total = strength + defence;
cout << total;
i get some extremely odd results. Such as 209, 204, 02.
I would paste the entire code but it's nearly 200lines long and some of it still needs sorting out. |
| |
07-01-2009, 09:40 PM
|
#26 (permalink)
|
Join Date: Sep 2005 Location: Toronto, Canada Posts: 5,466
| Re: C++ Help Alright can you post how you're defining the constants? And the function that calculates the stats? |
| |
07-02-2009, 10:23 AM
|
#27 (permalink)
|
*^[#@"'.,;:\|/:;,.'"@#]^* Join Date: Dec 2006 Location: Leicester, England Posts: 3,655
| Re: C++ Help I'll post the key bits of our game code: Code: #include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <cmath>
Code: srand(time(NULL));
char answer[4];
int stat, strength, defence, choice2, pot1, pot2, pot3, pot4, outcome, test;
char choice;
Code:
cout << "\nStrength: ";
cin >> strength;
cout << "\nDefence: ";
cin >> defence;
Code:
cout << "You reach a road, do you want to turn left or right ?\n\n";
cout << "1, Left: Danger, 30percent. Stat Potential Increase, 30ppercent\n\n";
cout << "2, Right: Danger, 60percent. Stat Potential, 70percent \n\n";
cout << "Enter choice (1 or 2): ";
cin >> choice2;
cout << "\n";
switch(choice2) {
Code: case 1:
pot1 = 5;
if(pot1 == 4)
{
cout << "You encounter no dangers, your stats remain the same\n\n";
}
else
{
cout << "-------Start of Battle------";
cout << "\n\n\n";
cout << "You encountered a Black Bear (Level 15)\n";
cout << "\nStrength is: " << strength << "...Defence is: " << defence;
stat = strength + defence;
cout << "\n\n";
cout << "Stats equal: " << stat;
Thats about halfway through the code, skipping bits which arn't particularly important.
In the last bit of code, pot1 is purposely set as 5 so the 2nd else executes not the first, it will actually be defined by a random number when i have this problem sorted. When i execute the code, you see where cout << stat; is ? well that stat should equal strength + defence. Instead it comes out with some random number from nowhere. |
| |
07-02-2009, 11:36 AM
|
#28 (permalink)
|
Join Date: Sep 2005 Location: Toronto, Canada Posts: 5,466
| Re: C++ Help Hmm I have a feeling I know what's causing it but i need to try something first. Can you post all the code from start to finish.
Don't worry just put it in a [code ] section and when it gets too long it will put a scroll bar on the side automatically. |
| |
07-02-2009, 02:37 PM
|
#29 (permalink)
|
*^[#@"'.,;:\|/:;,.'"@#]^* Join Date: Dec 2006 Location: Leicester, England Posts: 3,655
| Re: C++ Help Okay, but i must warn you we have goto statements in there from ages ago, we will be removing them shortly, so don't worry  And it's not finished, as it ends abruptly at the moment.
Also, sorry if how we've done it is terrible and totally not understandble. But we're nubs xD Code: #include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <cmath>
using namespace std;
int main()
{
srand(time(NULL));
char answer[4];
int stat, strength, defence, choice2, pot1, pot2, pot3, pot4, outcome, test;
char choice;
cout << "***Welcome to Mayple's Big Adventure***\n\n";
cout << "Lets start by setting your statistics: \n";
begin:
cout << "\nStrength: ";
cin >> strength;
cout << "\nDefence: ";
cin >> defence;
cout << "\nYour Strength is " << strength << " and your Defence is " << defence << "\n";
cout << "\nAre you happy with these stats? Yes or No: ";
choice = 'Yes' || 'No';
cin >> answer;
if(!strcmp("Yes", answer))
{
cout << "\n" << "Good, let us continue...\n\n";
goto start;
}
else
{
cout << "\n" << "Ok, lets try again...\n";
goto begin;
}
start:
cout << "Here are your final stats:\n\n";
cout << "Strength: " << strength << "\n\n";
cout << "Defence: " << defence << "\n\n";
system("pause");
cout << "\n\n -- THE ADVENTURE BEGINS --\n\n";
// PART ONE.
cout << "You reach a road, do you want to turn left or right ?\n\n";
cout << "1, Left: Danger, 30percent. Stat Potential Increase, 30ppercent\n\n";
cout << "2, Right: Danger, 60percent. Stat Potential, 70percent \n\n";
cout << "Enter choice (1 or 2): ";
cin >> choice2;
cout << "\n";
switch(choice2) {
case 1:
pot1 = 5;
if(pot1 == 4)
{
cout << "You encounter no dangers, your stats remain the same\n\n";
}
else
{
cout << "-------Start of Battle------";
cout << "\n\n\n";
cout << "You encountered a Black Bear (Level 15)\n";
cout << "\nStrength is: " << strength << "...Defence is: " << defence;
stat = strength + defence;
cout << "\n\n";
cout << "Stats equal: " << stat;
cout << stat;
if(stat > 15)
{
pot3 = rand() % 11;
printf("%d\n", pot3);
{ if(pot3 > 1)
{
cout << "\n You won the battle !";
outcome = 1;
}
else
{
cout << "\n You lost the battle !";
outcome = 0;
}
}
}
{
if(stat < 15)
{
pot4 = rand() % 11;
printf("%d\n", pot4);
{ if(pot4 < 8) {
cout << "\n You Lost the battle !";
outcome = 0;
}
else
{
cout << "\n You won the battle !";
outcome = 1;
}
}
cout << "\n\n\n";
cout << "-------end of battle-------";
cout << "\n\n\n";
if(outcome == 1) {
cout << "\n\n\Your win your first epic battle fight with a Bear !";
strength = strength + 3;
defence = defence + 3;
cout << "Your strength is now: " << strength << ",Your defence is now: " << defence;
}
else
{
cout << "You lost you first epic battle fight with a Bear !";
strength = strength + 0;
defence = defence + 0;
cout << "Your strength is now: " << strength << ",Your defence is now: " << defence;
}
}
break;
case 2:
pot2 = rand() % 11;
printf("%d\n", pot2);
cout << "\n";
if(pot2 < 4)
{
cout << "You encounter no dangers, your stats remain the same \n\n";
}
else
{
cout << "You encountered a Black Bear (Level 20) \n\n";
cout << "Your total stat is: " << strength + defence;
cout << "\n\n";
cout << "You win this battle.." << "Strength + 3, Defence + 3";
cout << "\n\n";
strength = strength + 3;
defence = defence + 3;
cout << "Your strength is now: " << strength << " Your defence is now: " << defence;
}
}
}
}
cout << "\n\n";
system("pause");
return 0;
}
And yeh i know the code is all over the place.. because we keep removing/adding/changing parts. We'll make it look neat when we get it working |
| |
07-03-2009, 01:50 AM
|
#30 (permalink)
|
Join Date: Sep 2005 Location: Toronto, Canada Posts: 5,466
| Re: C++ Help Alright I'll take a look at this tomorrow morning (2AM here right now) and see what's going wrong. |
| |  | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | |