I have been writing a, seemingly, simple game,but I run into a problem once every two tries. The problem is that once out of two I get a garbage value(for my cell_energy variable)
main.cpp
Code:
#include <iostream>
#include "cells.h"
#include "shop.h"
using namespace std;
bool quit = 1;
char choice = ' ';
int main(){
#include "instances_creation.h"
player.check_existance();
while (quit == 1){
//Show the game menu
cout<<"Cells:\t"<<player.get_cells()<<"\n"
<<"Energy:\t"<<player.get_energy()<<"/"<<player.get_max_energy()<<"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t"
<<"[S]plit cells\n\t"
<<"[E]at\n\t"
<<"S[h]op\n\t"
<<"[Q]uit\n\t"
<<"Answer with a capital letter\n\t";
//get the users choice
cin>>choice;
//Analize the information(of the choice)
switch (choice) {
case 'S':
player.split();
break;
case 'E':
player.set_energy(player.get_max_energy());
break;
case 'H':
mall.show_items(player.get_jaws(),player.get_claws(),player.get_horns());
break;
case 'Q':
cout<<"Are you sure you want to quit? Y/N\n\t";
cin>>choice;
switch (choice){
case 'Y':quit = 0;break;
case 'N':quit = 1;break;
default: quit = 0;break;
}
break;
default:
cout<<"Unusable answer!\n\t";
break;
}
}
// player.save();
}
cells.h
Code:
#ifndef CELLS_H_INCLUDED
#define CELLS_H_INCLUDED
#include <string>
using namespace std;
class cells{
public:
void new_game();
void check_existance();
void balance_stats();
/** Set **/
void split();
void set_group_name(string to_set);
void set_cells(int to_set);
void change_energy(int change);
void set_energy(int to_set);
void set_max_energy();
void set_title(int cells);
/** Get **/
string get_group_name();
int get_cells();
int get_energy();
int get_max_energy();
int get_jaws();
int get_claws();
int get_horns();
/** In/Out put **/
string get_string();
int get_int();
bool get_bool();
void display(string to_show);
void display(int to_show);
/** Save/Load **/
void save();
void load();
private:
string cell_group_name;
string cell_group_title;
float cell_count;
float cell_energy;
float cell_max_energy;
// Needed for shop
short jaws;
short claws;
short horns;
};
#endif // CELLS_H_INCLUDED
cells.cpp
Code:
#include <iostream>
#include <fstream>
#include <string>
#include "cells.h"
using namespace std;
/** New Game **/
void cells::new_game(){
cout<<"\nWe will now set other variables!\n";
set_cells(1);
set_max_energy();
set_energy(get_max_energy());
cout<<"Done!! You may now start playing...";
}
void cells::check_existance(){
string to_check;
ifstream file( "GAME_SAVE.SSG" );
cout<<"What is the name of your cell group?\n\tIf it doesn't exist, yet, a new game will be started!\n\t";
set_group_name(get_string());
file >> to_check;
if(to_check == get_group_name()){load();}
else{new_game();}
for(int counter=0;counter>25;counter++){cout<<'\n';}
}
/** Set **/
void cells::split(){
if(get_energy()>=20){
cell_count = (get_cells()*2);
change_energy(-20);
set_max_energy();
}
else{cout<<"Could not split, insuficient energy!";}
}
void cells::set_cells(int to_set){cell_count = to_set;}
void cells::set_group_name(string to_set){cell_group_name = to_set;}
void cells::change_energy(int change){cell_energy = cell_energy+change;}
void cells::set_energy(int to_set){cell_energy = to_set;}
void cells::set_max_energy(){cell_max_energy = get_cells()*10+30;}
void cells::set_title(int cells){
if(get_cells()==1){cell_group_title = "unicelular";}
else if(get_cells()==2){cell_group_title = "bicelular";}
else if(get_cells()>=3 && get_cells()<1000){cell_group_title = "celular grouping";}
else if(get_cells()>=1001 && get_cells()<1000000){cell_group_title = "big celular grouping";}
else if(get_cells()>1000001){cell_group_title = "huge celular grouping";}
}
/** Get **/
string cells::get_group_name(){return cell_group_name;}
int cells::get_cells(){return cell_count;}
int cells::get_energy(){
return cell_energy;
}
int cells::get_max_energy(){return cell_max_energy;}
int cells::get_jaws(){return jaws;}
int cells::get_claws(){return claws;}
int cells::get_horns(){return horns;}
/** In/Out put **/
string cells::get_string(){
string input;
getline(cin, input, '\n');
cout<<'\n';
return input;
}
int cells::get_int(){
int input;
cin>>input;
cout<<'\n';
return input;
}
bool cells::get_bool(){
bool input;
cin>>input;
cout<<'\n';
return input;
}
void display(string to_show){cout<<to_show;}
void display(int to_show){cout<<to_show;}
/** Save/Load **/
void cells::save(){
ofstream save_file ("GAME_SAVE.SSG");
save_file << get_group_name() << " " << get_cells() << " " << get_energy();
}
void cells::load(){
string name;
int cells;
int energy;
ifstream file("GAME_SAVE.SSG");
file >> name;
file >> cells;
file >> energy;
set_cells(cells);
set_title(get_cells());
set_max_energy();
if(energy > cell_max_energy){cell_energy = cell_max_energy;}
}
I think the problemis in cells::get_energy(), as that is what gives me the value.