Full Program Variable

This is a discussion on Full Program Variable within the C++ Programming forums, part of the General Programming Boards category; I'm using Borland C++ 4.52. My program won't read a variable that I created earlier in the program. Does anyone ...

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Full Program Variable

    I'm using Borland C++ 4.52. My program won't read a variable that I created earlier in the program. Does anyone here know why?

    Code:
    #include <conio.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <time.h>
    int create(char nme[100])
    {
    	clrscr();
    	gotoxy(34,2);
    	cout << "STATS:" << endl;
    	cout << endl;
    	gotoxy(34,4);
    	cout << "NAME: " << nme;
    	gotoxy(34,5);
    	int lvl=1;
    	cout << "LEVEL: " << lvl << endl;
    	int hp=rand()%6;
    	hp=hp+10;
    	gotoxy(34,6);
    	cout << "HIT POINTS:" << hp << endl;
    	int at=rand()%5;
    	at=at+1;
    	gotoxy(34,7);
    	cout << "ARMOR: " << at << endl;
    	gotoxy(34,8);
    	int gp=0;
    	cout << "GOLD PIECES: " << gp << endl;
    	gotoxy(39,9);
    	getch();
       return 0;
    }
    int load()
    {
    	char nme[100];
    	int lvl;
    	int hp;
    	int at;
       int gp;
    	int lp4=5;
    	while(lp4!=0)
    	{
    		if(lp4==5)
    		{
    			ifstream b_file("nme.txt");
    			b_file >> nme;
    			b_file.close();
    		}
    		else if(lp4==4)
    		{
    			ifstream b_file("lvl.txt");
    			b_file >> lvl;
    			b_file.close();
    		}
    		else if(lp4==3)
    		{
    			ifstream b_file("hp.txt");
    			b_file >> hp;
    			b_file.close();
    		}
    		else if(lp4==2)
    		{
    			ifstream b_file("at.txt");
    			b_file >> at;
    			b_file.close();
    		}
    		else if(lp4==1)
    		{
    			ifstream b_file("gp.txt");
    			b_file >> gp;
    			b_file.close();
    		}
    		lp4=lp4-1;
    	}
    	return 0;
    }
    int sleep(float secs)
    {
    	clock_t delay = secs * CLOCKS_PER_SEC;
    	clock_t start = clock();
    	while (clock() - start < delay)
    	;
    	return 0;
    }
    int save(int hp,int lvl,int at,int gp,char nme[100])
    {
    	int lp=5;
    	while(lp!=0)
    	{
    		if(lp==5)
    		{
    			ofstream a_file("nme.txt", ios::app);
    			a_file << nme;
    			a_file.close();
    		}
    		else if(lp==4)
    		{
    			ofstream a_file("lvl.txt", ios::app);
    			a_file << lvl;
    			a_file.close();
    		}
    		else if(lp==3)
    		{
    			ofstream a_file("hp.txt", ios::app);
    			a_file << hp;
    			a_file.close();
    		}
    		else if(lp==2)
    		{
    			ofstream a_file("at.txt", ios::app);
    			a_file << at;
    			a_file.close();
    		}
    		else if(lp==1)
    		{
    			ofstream a_file("gp.txt", ios::app);
    			a_file << gp;
    			a_file.close();
    		}
    		lp=lp-1;
       }
    	return 0;
    }
    int tmdseq(float slpamt,char word[100])
    {
    	int lp2=5;
    	while(lp2!=0)
    	{
    		clrscr();
    		gotoxy(34,12);
    		cout << word;
    		sleep(slpamt);
    		cout << ".";
    		sleep(slpamt);
    		cout << ".";
    		sleep(slpamt);
    		cout << "." << endl;
    		sleep(slpamt);
    		lp2=lp2-1;
    	}
    	return 0;
    }
    int main(void)
    {
    	int lp3=1;
    	while(lp3!=0)
    	{
    		gotoxy(34,2);
    		cout << "Dungeon Explorer";
    		gotoxy(34,3);
    		cout << "Main Menu: ";
    		gotoxy(34,5);
    		cout << "1. New Game";
    		gotoxy(34,6);
    		cout << "2. Continue Game";
    		gotoxy(34,7);
    		char ch=getch();
    		if(ch=='1')
    		{
          	clrscr();
    			gotoxy(34,12);
    			char nme[100];
    			cout << "Name: ";
    			cin >> nme;
    			clrscr();
    			tmdseq(.50,"Creating");
    			create(nme);
    			lp3=0;
    		}
    		if(ch=='2')
    		{
    			clrscr();
    			tmdseq(.50,"Loading");
             load();
             lp3=0;
    		}
       int lp5=1;
    	while(lp5!=0)
    	{
    		clrscr();
    		gotoxy(34,2);
    		cout << "Dungeon Explorer";
    		gotoxy(34,3);
    		cout << "Town Menu: ";
    		gotoxy(34,5);
    		cout << "1. Save";
    		gotoxy(34,6);
    		cout << "2. Stats";
    		gotoxy(34,7);
    		cout << "3. Quit";
    		char ch2=getch();
    		if(ch2=='1')
    		{
    			tmdseq(.50,"Saving");
    			save(hp,lvl,at,gp,nme);
    		}
    		if(ch2=='2')
    		{
    			clrscr();
    			gotoxy(34,2);
    			cout << "STATS:" << endl;
    			cout << endl;
    			gotoxy(34,4);
    			cout << "NAME: " << nme;
    			gotoxy(34,5);
    			cout << "LEVEL: " << lvl << endl;
    			gotoxy(34,6);
    			cout << "HIT POINTS:" << hp << endl;
    			gotoxy(34,7);
    			cout << "ARMOR: " << at << endl;
    			gotoxy(34,8);
    			cout << "GOLD PIECES: " << gp << endl;
    			gotoxy(34,9);
    			getch();
    		}
    		if(ch2=='3')
    		{
          	lp5=0;
    		}
    	}
    	}
    	return 0;
    }

  2. #2
    Registered User xlnk's Avatar
    Join Date
    Mar 2002
    Posts
    186
    which variable?
    the best things in life are simple.

  3. #3
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Post Variables

    The Variables that you load in function load.

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Because of scope. You created the variables in main- they aren't accessible in any other function unless you pass them as arguments.

  5. #5
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    How?

    How would you pass them as arguments?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overlaying a Full Screen Program
    By SirLaMort in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2005, 08:00 PM
  2. counting vowels
    By trippedwire in forum C++ Programming
    Replies: 10
    Last Post: 10-01-2004, 11:58 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  4. hashing help
    By alokin in forum C Programming
    Replies: 17
    Last Post: 10-28-2002, 05:33 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21