[C++]Empty Variable

Skilled Illusionist
Joined
Jan 14, 2005
Messages
395
Reaction score
2
Hello,
i finally got fixed my other error but when i pasted a piece of the code in { } than i get empty
variable how i fix it ?
this is my code
Code:
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main() {
system("TITLE Wizard Fight");

cout << "Wizard Fight" << endl << endl;
cout << "Made by Pascal Wever" << endl;
cout << "version 0.2" << endl << endl;

system("PAUSE");     
system("CLS");

int lvl = 0;

cout << "Please Select your Character" << endl << endl;
Sleep(450);
cout << "1 - Wizard" << endl;
Sleep(450);
cout << "2 - Archer" << endl;
Sleep(450);
cout << "3 - Gunner" << endl;
Sleep(300);
cout << "please select a Character !"<< endl;
cin >> lvl;

if (lvl = 1) {
goto mage;
}
if (lvl = 2) {
goto archer;                             
}
if (lvl = 3) {
goto gunner;        
}
//begin mage

mage:
     system("CLS");
{
string wepName[8];

		wepName[0] = "Sword";
		wepName[1] = "Scythe";
		wepName[2] = "Daggers";
		wepName[3] = "Throwing Star";
		wepName[4] = "Knife";
		wepName[5] = "Axe";
		wepName[6] = "Spear";
		wepName[7] = "Shield";
}
goto fight;
//end mage

//begin archer
archer:
cout << "archer" << endl;
return(0);
//end archer

//begin gunner
gunner:
cout << "gunner" << endl;
return(0);
//end gunner


system("PAUSE");
fight:
{    
string wepName[8]; 
cout << "0 - " << wepName[0] << endl;
cout << "1 - " << wepName[1] << endl;
cout << "2 - " << wepName[2] << endl;
cout << "3 - " << wepName[3] << endl;
cout << "4 - " << wepName[4] << endl;
cout << "5 - " << wepName[5] << endl;
cout << "6 - " << wepName[6] << endl;
cout << "7 - " << wepName[7] << endl;
system("PAUSE");
goto end;
}
end:
    return(0);
}
hope you understand it better now :sq_yellow
 
Your english is wonderful. What error do you get? *sigh*
 
i dont get a error read better i sayd i get empty variable
so no errors

I read it, but I barely understand what you try to say. WHAT variable is empty? Provide more information or use a translator to make yourself clear...
 
these ones if this script is compiled
its wont store sword in wepName[0]
wont store scythe in wepName[1]
etc

i think its becouse the {}


wepName[0] = "Sword";
wepName[1] = "Scythe";
wepName[2] = "Daggers";
wepName[3] = "Throwing Star";
wepName[4] = "Knife";
wepName[5] = "Axe";
wepName[6] = "Spear";
wepName[7] = "Shield";
 
Try taking the variables out of the mage section, try setting them up at the beginning.
and then in the fight section, don't redeclare your array. Take out the string wepName[8]; out of the fight section.
 
I suggest you read more tutorials >_> So many things at this code is irking me.

First of all, in the beginning you said "if (var = val)" when it should be "if (var == val)". Notice the ==, not the =. Second, LOL GOTO. Any book you're reading that doesn't tell you that you should probably never use goto isn't worth reading I'm guessing. Very few occasions actually warrant using it.

I didn't read the entire thing, just wanted to point out a few things I saw. Read some more, then come back to this.
 
I completely agree with RepublicOfAstra, the code looks bad and is written badly. The 'goto' statements should be replaced by while loops, or something similair.

republicofasta i already did fix that 5 days ago i dont go post everytime my source when i did edit it ..

Ever noticed the Edit button on the bottom of your post?
 
Well, I took a look at it and corrected some stuff. If I did the wrong thing Im sorry.

Code:
#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

int main() 
{
	
int lvl ( 0 );

string wepName[8];
	wepName[0] = "Sword";
	wepName[1] = "Scythe";
	wepName[2] = "Daggers";
	wepName[3] = "Throwing Star";
	wepName[4] = "Knife";
	wepName[5] = "Axe";
	wepName[6] = "Spear";
	wepName[7] = "Shield";

system("TITLE Wizard Fight");

cout << "Wizard Fight" << endl << endl;
cout << "Made by Pascal Wever" << endl;
cout << "version 0.2" << endl << endl;

system("PAUSE");     
system("CLS");

cout << "Please Select your Character" << endl << endl;
Sleep(450);
cout << "1 - Wizard" << endl;
Sleep(450);
cout << "2 - Archer" << endl;
Sleep(450);
cout << "3 - Gunner" << endl;
Sleep(300);
cout << "please select a Character !"<< endl;
cin >> lvl;

if (lvl == 1) 
{
	goto mage;
}
else if (lvl == 2) 
{
	goto archer;                             
}
else 
{
	goto gunner;        
}


//begin mage
mage:
cout << "Wizard" << endl;
     system("CLS");
goto fight;
//end mage

//begin archer
archer:
cout << "archer" << endl;
return(0);
//end archer

//begin gunner
gunner:
cout << "gunner" << endl;
return(0);
//end gunner


system("PAUSE");
fight:
{    
//string wepName[8]; 
cout << "0 - " << wepName[0] << endl;
cout << "1 - " << wepName[1] << endl;
cout << "2 - " << wepName[2] << endl;
cout << "3 - " << wepName[3] << endl;
cout << "4 - " << wepName[4] << endl;
cout << "5 - " << wepName[5] << endl;
cout << "6 - " << wepName[6] << endl;
cout << "7 - " << wepName[7] << endl;
system("PAUSE");
goto end;
}
end:
    return(0);
}
 
Back