Just a few warnings that need to be killed.

Started by
5 comments, last by Dko 20 years, 3 months ago
This is some code I got some place (not shure) and I get some warrnings that seam to make it not run how can I rid my self of them. My little ommision to the code is to make clrscn work as its a borland C++ only thing.


#include <iostream.h> 
#include <conio.h> 
#include <stdlib.h> 
#include <time.h> 
#include <iomanip.h> 
#include <fstream.h> 
#include <ctype.h> 
#include <assert.h> 

const int Maxhp = 560; 
const int startingstamina = 100; 

// function prototypes 

void battleA(int,int,int,int,int); 
char battle_screen(int,int,int); 
void showmenu(int,int,int,int,int); 
int  check_health_monster(int,int*,int*,int*,int*); 
int giveplayeritems(int*); 
int check_health_player(int,int*,int*,int*); 
char attack_screen(); 
int coordinates_message(int,int); 
int ice1(int*,int,int,int); 
int fire1(int*,int,int,int); 
int potion(int,int*); 
int normal_attack(int*,int,int,int); 
// A little function added so everything works hunky dory :) *D-ko*

void clrscr( ) { system("cls"); }

struct character 
{ 
int hp; 
int stamina; 
}monster,player; 

ofstream fout; 
ifstream fin; 

int main() 
{ 
char command; 
int x,y,counter=1,grid_1=1; 
int num_potions=1; 
int  *def_monster, *num_potionsP = &num_potions; 
int loopc,end,battle_check; 
int battle_counter, turn, spell, item, attack; 
int *damage; 
int* grid_1P=&grid_1,*turnP=&turn,*counterP = &counter 





player.hp = Maxhp; 
monster.hp = Maxhp; 

x=0; 
y=0; 
loopc = 1; 
while(counter != 0) 
{ 

while(grid_1 != 0) 
{ 

 monster.hp = Maxhp; 
 srand(time(0)); 
 if(loopc == 1) 
 { 
  clrscr(); 
 } 
 loopc++; 
 cout<<endl<<"what do you want to do "; 
 command = getch(); 
 clrscr(); 
 battle_check = rand()%100; 

 if((battle_check <= 30) && (command != ''i'') && (command != ''q'') && (command != ''m'')) 
 { 
  grid_1 = 0; 
  turn = 1; 
  break; 
 } 
 if((command == ''n'')&&(y!=50)) 
 { 
  y++; 
  clrscr(); 
  coordinates_message(x,y); 
 } 
 else if((command == ''s'')&&(y!=-50)) 
 { 
  y--; 
  clrscr(); 
  coordinates_message(x,y); 
 } 
 else if((command == ''e'')&&(x!=50)) 
 { 
  x++; 
  clrscr(); 
  coordinates_message(x,y); 
 } 
 else if((command == ''w'')&&(x!=-50)) 
 { 
  x--; 
  clrscr(); 
  coordinates_message(x,y); 
 } 
 else if(command == ''m'') 
 { 
  showmenu(monster.hp,player.hp,x,y,num_potions); 
 } 
 else if(y==50) 
 { 
  y=-50; 
  clrscr(); 
  coordinates_message(x,y); 
 } 
 else if(y==-50) 
 { 
  y=50; 
  clrscr(); 
  coordinates_message(x,y); 
 } 
 else if(x==50) 
 { 
  x=-50; 
  clrscr(); 
  coordinates_message(x,y); 
 } 
 else if(x==-50) 
 { 
  x=50; 
  clrscr(); 
  coordinates_message(x,y); 
 } 
 else if(command==''q'') 
 { 
  counter=0; 
  grid_1 = 0; 
  clrscr(); 
 } 
else if(command ==''i'') 
 { 
  player.hp = potion(player.hp,num_potionsP); 
 } 
else 
{ 
clrscr(); 
} 
} 

// Player''s turn 

while(turn == 1) 
{ 
 command = battle_screen(player.hp,monster.hp,turn); 
 cout<<"                         "<<attack_screen(); 
 if( command == ''1'') 
 { 
  def_monster = &monster.hp 
  monster.hp =  normal_attack(def_monster,turn,player.hp,monster.hp); 

  check_health_monster(monster.hp,grid_1P,counterP,turnP,num_potionsP); 
 } 
 else if( command == ''2'') 
 { 
  clrscr(); 
  cout<<endl<<"------------------"<<endl 
      <<"     1.Fire1"<<endl 
      <<"     2.Ice1"<<endl 
      <<"------------------"<<endl; 
  spell = getch(); 
   if( spell == ''1'') 
   { 

   def_monster = &monster.hp 
   monster.hp = fire1(def_monster,turn,player.hp,monster.hp); 

   check_health_monster(monster.hp,grid_1P,counterP,turnP,num_potionsP); 

   } 
  else if( spell == ''2'') 
  { 
   def_monster = &monster.hp 
   monster.hp = ice1(def_monster,turn,player.hp,monster.hp); 
   check_health_monster(monster.hp,grid_1P,counterP,turnP,num_potionsP); 

  } 
 } 
 else if( command == ''3'') 
 { 
  if(num_potions <= 0) 
  { 
   num_potions = 0; 
  } 
  cout<<endl<<"-------------------"<<endl 
      <<"   1.potions "<<num_potions<<endl 
      <<"-------------------"<<endl; 
  item = getch(); 
  if(item == ''1'') 
  { 
   player.hp = potion(player.hp,num_potionsP); 
   turn= 2; 
  } 
 } 
 else if(command == ''4'') 
 { 
  turn = 0; 
  grid_1 = 1; 
  counter = 1; 
  clrscr(); 
 } 



} // while (turn == 1) 


// Monster''s turn 

while(turn == 2) 
{ 
 battle_screen(player.hp,monster.hp,turn); 
 attack = rand()%3; 

 def_monster = &player.hp 

 if(attack == 1) 
 { 
  player.hp = normal_attack(def_monster,turn,player.hp,monster.hp); 
  end = check_health_player(player.hp,grid_1P,counterP,turnP); 
 } 
 else if(attack == 2) 
 { 
  player.hp = ice1(def_monster,turn,player.hp,monster.hp); 
  end = check_health_player(player.hp,grid_1P,counterP,turnP); 
 } 
 else if(attack == 3) 
 { 
  player.hp = fire1(def_monster,turn,player.hp,monster.hp); 
  end = check_health_player(player.hp,grid_1P,counterP,turnP); 
 } 

   turn = 1; 
 if(end == 1) 
 { 
  turn = 0; 
  counter = 0; 
  grid_1 = 0; 
} // while (turn == 2) 


  } 
  } 



 return 0; 
} 

int coordinates_message(int x,int y) 
{ 
cout<<"Your coordinates are ("<<x<<","<<y<<")"; 
return 0; 
} 


int ice1(int* def_monster,int turn,int player, int monster) 
{ 
srand(time(0)); 

int damage,hit_check; 

damage = rand()%74+40; 

*def_monster -= damage; 
hit_check = 1; 
clrscr(); 
    battleA(damage,player,monster,hit_check,turn); 
return (*def_monster); 
} 
int fire1(int* def_monster,int turn, int player, int monster) 
{ 
  srand(time(0)); 

int damage,hit_check; 

damage = rand()%74+40; 

*def_monster -= damage; 
  clrscr(); 
  hit_check = 1; 
     battleA(damage,player,monster,hit_check,turn); 
return (*def_monster); 
} 
potion(int player, int* num_potionsP) 
{ 
 int i; 
 int Maxhp = 560; 
 if(*num_potionsP <= 0) 
 { 
  clrscr(); 
  cout<<"Sorry you have no potions to use"; 
  getch(); 
  clrscr(); 
  } 
 else 
 { 
  for(i = 1; i<= 100 ; ++i) 
  { 
   if(player == Maxhp) 
   { 
    cout<<"you healed "<<i<<" hp"; 
    break; 
   } 
   player += 1; 
  } 


  clrscr(); 
  cout<<"Your HP: "<<player; 
  getch(); 
  clrscr(); 

  *num_potionsP = *num_potionsP - 1; 

  } 
 return (player); 
} 


int normal_attack(int *def_monster,int turn,int player,int monster) 
{ 
  srand(time(0)); 
  int damage, rand_check, hit_check; 

  rand_check = rand()%100; 
  if(rand_check >= 20) 
  { 
   damage = rand()%84+53; 
   *def_monster -= damage; 
   hit_check = 1; 
   battleA(damage,player,monster,hit_check,turn); 
   } 
  else 
  { 
   hit_check = 0; 
   battleA(damage,player,monster,hit_check,turn); 
  } 
  return (*def_monster); 
} 

char attack_screen() 
{ 
  char command; 

  cout<<endl<<"-------------------"<<endl 
      <<"     1.Attack"<<endl 
      <<"     2.Spell"<<endl 
      <<"     3.Item"<<endl 
      <<"     4.Run"<<endl 
      <<"-------------------"<<endl; 



  return(command); 
} 
int check_health_player(int HP,int* grid_1P,int* counterP,int* turnP) 
{ 
if(HP <= 0) 
{ 
 clrscr(); 
 cout<<"You Lost"; 
 getch(); 
 *grid_1P = 0; 
 *counterP = 0; 
 *turnP = 3; 
 return (1); 
} 
else 
{ 
 return (0); 
} 
} 
int check_health_monster(int HP,int* grid_1P,int* counterP, 
            int* turnP,int* num_potionsP) 
{ 
if(HP <= 0) 
{ 
 clrscr(); 
 cout<<"You Beat The Monster"; 
 getch(); 
 giveplayeritems(num_potionsP); 
 *grid_1P = 1; 
 *counterP = 1; 
 *turnP = 0; 
 return (1); 
} 
else 
{ 
 *turnP = 2; 
 return (0); 
} 
} 
int giveplayeritems(int* num_potionsP) 
{ 
int i,num_items; 
srand(time(0)); 
num_items = rand()%10; 
for(i = 0; i < num_items; ++i) 
{ 
 *num_potionsP = *num_potionsP + 1; 
 clrscr(); 
 cout<<"Potion  "<<i; 
} 
getch(); 
return (0); 
} 

char battle_screen(int player,int  monster,int turn) 
{ 
char command; 
if(turn == 2) 
{ 
clrscr(); 
cout<<endl<<"--------------------------"; 
cout<<endl<<"--------------------------"; 
cout<<endl<<"-- Monster''s Turn       --"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"-- Monster              --"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"-- HP: "; 
cout<<setiosflags(ios::left); 
cout<<setw(17)<<monster<<"--"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"--------------------------"; 
cout<<endl<<"--------------------------"; 
cout<<endl<<"--------------------------"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"-- Player               --"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"-- HP: "; 
cout<<setiosflags(ios::left); 
cout<<setw(17)<<player<<"--"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"--------------------------"; 
getch(); 
} 
if(turn == 1) 
{ 
clrscr(); 
cout<<endl<<"--------------------------"; 
cout<<endl<<"--------------------------"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"-- Monster              --"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"-- HP: "; 
cout<<setiosflags(ios::left); 
cout<<setw(17)<<monster<<"--"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"--------------------------"; 
cout<<endl<<"--------------------------"; 
cout<<endl<<"--------------------------"; 
cout<<endl<<"-- Player''s Turn        --"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"-- Player               --"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"-- HP: "; 
cout<<setiosflags(ios::left); 
cout<<setw(17)<<player<<"--"; 
cout<<endl<<"--                      --"; 
cout<<endl<<"--------------------------"; 
cout<<endl 
      <<"     1.Attack"<<endl 
      <<"     2.Spell"<<endl 
      <<"     3.Item"<<endl 
      <<"     4.Run"<<endl 
      <<"-------------------"<<endl; 
command = getch(); 
} 

return (command); 
} 
void battleA(int damage,int player,int monster,int hit_check,int turn) 
{ 
if((hit_check == 1)&&(turn == 1)) 
{ 
 clrscr(); 
 cout<<endl<<"--------------------------"; 
 cout<<endl<<"--------------------------"; 
 cout<<endl<<"--                      --"; 
 cout<<endl<<"-- Monster              --"; 
 cout<<endl<<"--                      --"; 
 cout<<endl<<"-- HP: "; 
 cout<<setiosflags(ios::left); 
 cout<<setw(17)<<monster<<"--"; 
 cout<<endl<<"-- You did "<<setw(3)<< damage<<" dmg"; 
 cout<<setiosflags(ios::right); 
 cout<<setw(8)<<"--"; 
 cout<<endl<<"--------------------------"; 
 cout<<endl<<"--------------------------"; 
 cout<<endl<<"--------------------------"; 
 cout<<endl<<"-- Player''s Turn        --"; 
 cout<<endl<<"--                      --"; 
 cout<<endl<<"-- Player               --"; 
 cout<<endl<<"--                      --"; 
 cout<<endl<<"-- HP: "; 
 cout<<setiosflags(ios::left); 
 cout<<setw(17)<<player<<"--"; 
 cout<<endl<<"--                      --"; 
 cout<<endl<<"--------------------------"; 
 getch(); 
 } 
 if((hit_check == 1) && (turn == 2)) 
 { 
  clrscr(); 
  cout<<endl<<"--------------------------"; 
  cout<<endl<<"--------------------------"; 
  cout<<endl<<"--                      --"; 
  cout<<endl<<"-- Monster              --"; 
  cout<<endl<<"--                      --"; 
  cout<<endl<<"-- HP: "; 
  cout<<setiosflags(ios::left); 
  cout<<setw(17)<<monster<<"--"; 
  cout<<endl<<"--                      --"; 
  cout<<endl<<"--------------------------"; 
  cout<<endl<<"--------------------------"; 
  cout<<endl<<"--------------------------"; 
  cout<<endl<<"-- Player''s Turn        --"; 
  cout<<endl<<"--                      --"; 
  cout<<endl<<"-- Player               --"; 
  cout<<endl<<"--                      --"; 
  cout<<endl<<"-- HP: "; 
  cout<<setiosflags(ios::left); 
  cout<<setw(17)<<player<<"--"; 
  cout<<endl<<"-- monster did "<<setw(3)<<damage<<" dmg"; 
  cout<<setiosflags(ios::right); 
  cout<<setw(4)<<"--"; 
  cout<<endl<<"--------------------------"; 
  getch(); 
  } 
 if((hit_check == 0)&&(turn == 1)) 
{ 
 clrscr(); 
 cout<<endl<<"--------------------------"; 
 cout<<endl<<"--------------------------"; 
 cout<<endl<<"--                      --"; 
 cout<<endl<<"-- Monster              --"; 
 cout<<endl<<"--                      --"; 
 cout<<endl<<"-- HP: "; 
 cout<<setiosflags(ios::left); 
 cout<<setw(17)<<monster<<"--"; 
 cout<<endl<<"-- You missed           --"; 
 cout<<endl<<"--------------------------"; 
 cout<<endl<<"--------------------------"; 
 cout<<endl<<"--------------------------"; 
 cout<<endl<<"-- Player''s Turn        --"; 
 cout<<endl<<"--                      --"; 
 cout<<endl<<"-- Player               --"; 
 cout<<endl<<"--                      --"; 
 cout<<endl<<"-- HP: "; 
 cout<<setiosflags(ios::left); 
 cout<<setw(17)<<player<<"--"; 
 cout<<endl<<"--                      --"; 
 cout<<endl<<"--------------------------"; 
 getch(); 
 } 
 if((hit_check == 0) && (turn == 2)) 
 { 
  clrscr(); 
  cout<<endl<<"--------------------------"; 
  cout<<endl<<"--------------------------"; 
  cout<<endl<<"--                      --"; 
  cout<<endl<<"-- Monster              --"; 
  cout<<endl<<"--                      --"; 
  cout<<endl<<"-- HP: "; 
  cout<<setiosflags(ios::left); 
  cout<<setw(17)<<monster<<"--"; 
  cout<<endl<<"--                      --"; 
  cout<<endl<<"--------------------------"; 
  cout<<endl<<"--------------------------"; 
  cout<<endl<<"--------------------------"; 
  cout<<endl<<"-- Player''s Turn        --"; 
  cout<<endl<<"--                      --"; 
  cout<<endl<<"-- Player               --"; 
  cout<<endl<<"--                      --"; 
  cout<<endl<<"-- HP: "; 
  cout<<setiosflags(ios::left); 
  cout<<setw(17)<<player<<"--"; 
  cout<<endl<<"-- the monster missed   --"; 
  cout<<endl<<"--------------------------"; 
  getch(); 
  } 
} 
void showmenu(int monster, int player, int x, int y,int num_potions) 
{ 
char choice; 
int menuloop = 1, int i; 
char filename[30]; 
while(menuloop == 1) 
{ 
cout<<"----------------------------------"; 
cout<<endl<<"| 1.Save Game                    |"; 
cout<<endl<<"| 2.Exit Menu                    |"; 
cout<<endl<<"| 3.load game                    |"; 
cout<<endl<<"----------------------------------"; 
choice = getch(); 

if(choice == ''1'') 
{ 
 clrscr(); 
 cout<<"Enter a file name to save in "; 
 cin.getline(filename,sizeof(filename-1)); 



 fout.open(filename); 

 fout<<monster<<player<<x<<y<<num_potions; 

 clrscr(); 
 cout<<"Saving............"<<endl; 
 fout.close(); 
 clrscr(); 
 } 
if(choice == ''2'') 
{ 
 menuloop = 0; 
} 
if(choice == ''3'') 
{ 
   clrscr(); 
 cout<<"Enter a file name to load "; 
 cin.getline(filename,sizeof(filename-1)); 
 fin.open(filename); 
 assert(! fin.fail()); 
 fin>>monster>>player>>x>>y>>num_potions; 

 clrscr(); 
 cout<<"loading............"<<endl; 
 fin.close(); 
 clrscr(); 
} 
}}; 


These are the warnnings: c:\my documents\c++ programs\turnbased\main.cpp(46) : warning C4101: ''battle_counter'' : unreferenced local variable c:\my documents\c++ programs\turnbased\main.cpp(47) : warning C4101: ''damage'' : unreferenced local variable c:\my documents\c++ programs\turnbased\main.cpp(595) : warning C4101: ''i'' : unreferenced local variable c:\my documents\c++ programs\turnbased\main.cpp(344) : warning C4700: local variable ''damage'' used without having been initialized c:\my documents\c++ programs\turnbased\main.cpp(362) : warning C4700: local variable ''command'' used without having been initialized
Advertisement
Warnings don''t cause compliation errors (i.e. the program will run with warnings)

The program should run, if not you must have an error or linkage error.

Btw, those warnings basic say that your declaring the variables but not using them, with is perfectly fine for compiling.
-Pac "The thing I like about friends in my classes is that they can't access my private members directly." "When listening to some one tell about their problem (whether it's code or not), don't listen to what went right or wrong, but what they assumed....."
''battle_counter'' : unreferenced local variable
''damage'' : unreferenced local variable
''i'' : unreferenced local variable:
All of these were because you didn''t use them in function main(). As far as I can see from the code you have supplied, you don''t need these variables and probably should exclude them from your declarations.

local variable ''damage'' used without having been initialized
local variable ''command'' used without having been initialized:
Basically, you didn''t give these a value and it''s not very good coding practice to not initialize your variables. Just set them to NULL if you don''t have need of any immediate value for these.

Now, even in light of these warnings, they should not stop your program from running. I don''t know exactly why your program doesn''t work or where it doesn''t work because you didn''t give me enough details. But, I''ll bet you that it wasn''t those warnings.

"There are 10 kinds of people in this world, those who know binary, and those who don''t"
----------------------------------"War does not determine who is right, only who is left." -Bertrand Russell
The two warnings that you should really pay attention to here are the last two. See those two line numbers, 344 and 362? That''s where those variables were read from WITHOUT EVER HAVING BEEN WRITTEN TO. After glancing at your code, it''s clear that that actually is your problem (well, two of them, anyway).

"Sneftel is correct, if rather vulgar." --Flarelocke
Ok got rid of the unused vars and NULLed the others. Everything is working fine its just that when it runs you have to press a key beffore anything is displayed
Thanks fot the help.

[edited by - Dko on January 13, 2004 11:20:17 PM]
It may not have been running because he/she set the compilier to treat warnings as errors. Not a bad idea, either.
quote:Original post by Dko
Ok got rid of the unused vars and NULLed the others. Everything is working fine its just that when it runs you have to press a key beffore anything is displayed

Don''t just NULL them without looking and seeing what''s going on first. Look at this program:
int main(){    int i, j;    std::cout << "Enter variables for i and j: ";    std::cin >> i >> i;    std::cout << "You entered " << i << " and " << j << ".\n";}

Notice how the programmer accidentally entered ''i'' twice on the line that takes input. That''s the sort of thing that will be flagged by the compiler with the "uninitialized variable" warning. And blindly setting j to 0 or NULL won''t do a thing to make it work properly.

"Sneftel is correct, if rather vulgar." --Flarelocke

This topic is closed to new replies.

Advertisement