Есть два класса - Menu и MainProgram
В MainProgram есть переменная m_command_file_len, которая после инициализации MainProgram становится равна 3.
В main() инициализирую Menu, а потом MainProgram инициализируется как объект в Menu, m_command_file_len выводится как 3. И после инициализации в любом месте, где я хочу вывести m_command_file_len его значение становится равно 1.
Тут мб переполнение памяти или че то типо? Где я туплю жутко, подскажите.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class MainProgram
{
public:
string m_name;
string m_version;
string* m_all_commands;
string* m_all_commands_information;
int m_command_file_len;
public:
MainProgram()
{
string line;
ifstream in("MainProgramData.txt");
if (in.is_open())
{
while (getline(in, line))
{
if (line == "name") getline(in, m_name);
else if (line == "version") getline(in, m_version);
}
} else cout << "File with MainProgramData cannot be opened" << endl;
in.close();
int m_command_file_len = 0;
ifstream inn("MainProgramCommands.txt");
if (inn.is_open())
{
while (getline(inn, line))
{
m_command_file_len ++;
}
}
inn.close();
m_command_file_len = m_command_file_len / 2;
ifstream innn("MainProgramCommands.txt");
if (innn.is_open())
{
m_all_commands = new string[m_command_file_len];
m_all_commands_information = new string[m_command_file_len];
for (int i = 0; i < m_command_file_len; i++)
{
getline(innn, line);
m_all_commands[i] = line;
getline(innn, line);
m_all_commands_information[i] = line;
}
}else cout << "File with MainProgramCommands cannot be opened" << endl;
innn.close();
cout << m_command_file_len << endl;
for (int i = 0; i < m_command_file_len; i++)
{
cout << m_all_commands[i] << " " << m_all_commands_information[i] << endl;
}
}
void print_full_information_about_program()
{
cout << "Full information about program" << endl;
print_name();
print_version();
}
void print_name()
{
cout << "Program name:\t\t" << m_name << endl;
}
void print_version()
{
cout << "Program version:\t" << m_version << endl;
}
void select_command(string entry)
{
cout << m_command_file_len << endl;
entry = entry.substr(3);
cout << entry << endl;
for (int i = 0; i < m_command_file_len; i++)
{
//if (entry == m_all_commands[i])
//{
cout << i << " " << m_all_commands_information[i] << endl;
// return;
//}
}
cout << "Command is not clear" << endl;
}
};
class Menu
{
public:
MainProgram MP;
public:
Menu()
{}
void StartMenu()
{
string entry = "good";
while (entry != "exit")
{
cout << "Entry: ";
getline(cin, entry);
if (entry.substr(0,2) == "MP") MP.select_command(entry);
}
cout << "Menu is closed" << endl;
}
};
int main()
{
Menu M;
M.StartMenu();
}