• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Jak przerobić ten program żeby działał na tablicach dynamicznych?

VPS Starter Arubacloud
0 głosów
245 wizyt
pytanie zadane 27 lutego 2019 w C i C++ przez MrChick888 Obywatel (1,020 p.)

Jak przerobić ten program żeby działał na tablicach dynamicznych?

#include <iostream>
#include <string>
#include <ctime>
#include <fstream>
#include <windows.h>

using namespace std;

void main_menu(int& main_menu_navigator);
void menu_option1(string& player_name, int& credits, int& color, int& credits_played, int& random_number, fstream& save, string& game_name);
void conditions_color_option(string player_name, int& variable);
void conditions_credits_option(string player_name, int credits, int& credits_played);
void draw_option(int& random_number);
void conditions_result_option1(string player_name, int& credits, int credits_played, int color, int random_number, fstream& save, string& game_name);
void win_option1(string player_name, int& credits, int credits_played, int multiplier);
void lose_option1(string player_name, int& credits, int credits_played, string correct_color);
void p_c();
void save_option1(fstream& save, string& game_name, string player_name, int credits);
void reading_from_a_file(string Save[], string Nick[], int Credits[], int &selected_save, string& player_name, int& credits);
void menu_option2(string player_name, int& credits, int color, int credits_played, int random_number, fstream& save, string game_name, int &selected_save, string Save[], string Nick[], int Credits[]);
void conditions_result_option2(string player_name, int& credits, int credits_played, int color, int random_number, string Save[], string Nick[], int Credits[], int& selected_save, string game_name, fstream& save);
void save_option2(fstream& save, string game_name, string player_name, int credits, string Save[], string Nick[], int Credits[], int selected_save);

int main()
{
	int main_menu_navigator, credits, color, credits_played = 0, random_number, selected_save;
	string player_name, game_name;
	fstream save;
	string Save[4], Nick[4];//dynamika
	int Credits[4];//dynamika
	while (1)
	{
		main_menu(main_menu_navigator);
		switch (main_menu_navigator)
		{
		case 1:
			menu_option1(player_name, credits, color, credits_played, random_number, save, game_name);
			break;
		case 2:
			menu_option2(player_name, credits, color, credits_played, random_number, save, game_name, selected_save, Save, Nick, Credits);
			break;
		case 3:
			//kod
		default: break;
		}

	}
	system("PAUSE");
}
void main_menu(int& main_menu_navigator)
{
	cout << "ROULETTE GAME" << endl;
	cout << "=============" << endl;
	cout << "<<MAIN MENU>>" << endl;
	cout << "=============" << endl;
	cout << "1.Start a new game." << endl;
	cout << "2.Coutinue the game." << endl;
	cout << "3.Rule section." << endl;
	cout << "4.END." << endl;
	cout << "Enter: ";
	if (!(cin >> main_menu_navigator))
	{
		cout << "You can't enter anything other than a number here! " << endl;
		cout << "Restart program and try again." << endl;
		system("PAUSE");
		exit(0);
	}
	system("cls");
	return;
}
void menu_option1(string& player_name, int& credits, int& color, int& credits_played, int& random_number, fstream& save, string& game_name)
{
	credits = 50;
	cout << "WELCOME IN C++ CASINO." << endl;
	cout << "Enter your name: ";
	cin >> player_name;
	cout << "Good luck and have fun " << player_name << "." << endl;
	cout << "Your credits: " << credits << endl;
	cout << "Let's start the game!" << endl;
	cout << player_name << " choose color : " << endl;
	cout << "1.RED" << endl;
	cout << "2.GREEN" << endl;
	cout << "3.BLACK" << endl;
	conditions_color_option(player_name, color);
	while (color < 1 || color>3)
	{
		system("cls");
		cout << "This option dosn't exits!" << endl;
		cout << player_name << " choose color : " << endl;
		cout << "1.RED" << endl;
		cout << "2.GREEN" << endl;
		cout << "3.BLUE" << endl;
		conditions_color_option(player_name, color);
	}
	cout << "How many credits do you want to play? ";
	conditions_color_option(player_name, credits_played);
	conditions_credits_option(player_name, credits, credits_played);
	draw_option(random_number);
	conditions_result_option1(player_name, credits, credits_played, color, random_number, save, game_name);
	return;
}
void conditions_color_option(string player_name, int& variable)
{
	if (!(cin >> variable))
	{
		cout << player_name << " you can't enter anything other than a number here! " << endl;
		cout << "Restart program and try again." << endl;
		system("PAUSE");
		exit(0);
	}
	return;
}
void conditions_credits_option(string player_name, int credits, int& credits_played)
{
	while (credits < credits_played || credits_played <= 0)//można lepiej na dwa przypadki
	{
		system("cls");
		cout << player_name << " you have only " << credits << " credits! You can't play for " << credits_played << "!" << endl;
		cout << "How many credits do you want to play? " << endl;
		conditions_color_option(player_name, credits_played);
	}
	return;
}
void draw_option(int& random_number)
{
	system("cls");
	cout << "The drawing machine begins the countdown." << endl;
	for (int i = 5; i > -2; i--)
	{
		Sleep(500);
		system("cls");
		cout << i;
	}
	system("cls");
	srand(time(NULL));
	random_number = rand() % 99 + 1;
	return;
}
void conditions_result_option1(string player_name, int& credits, int credits_played, int color, int random_number, fstream& save, string& game_name)
{
	if (random_number >= 1 && random_number <= 48)
	{
		if (color == 1)win_option1(player_name, credits, credits_played, 2);
		else lose_option1(player_name, credits, credits_played, "Red");
		save_option1(save, game_name, player_name, credits);
	}
	if (random_number >= 49 && random_number <= 51)
	{
		if (color == 2)win_option1(player_name, credits, credits_played, 14);
		else lose_option1(player_name, credits, credits_played, "Green");
		save_option1(save, game_name, player_name, credits);
	}
	if (random_number >= 52 && random_number <= 99)
	{
		if (color == 3)win_option1(player_name, credits, credits_played, 2);
		else lose_option1(player_name, credits, credits_played, "Black");
		save_option1(save, game_name, player_name, credits);
	}
	return;
}
void win_option1(string player_name, int& credits, int credits_played, int multiplier)
{
	cout << "YOU WON!, Well played " << player_name << "." << endl;
	cout << "You won " << credits_played * multiplier << "." << endl;
	credits += (credits_played * multiplier) - credits_played;
	cout << "Your credits: " << credits << endl;
	return;
}
void lose_option1(string player_name, int& credits, int credits_played, string correct_color)
{
	cout << "YOU LOST! Correct color is " << correct_color << " . You must try one more time " << player_name << "." << endl;
	credits -= credits_played;
	cout << "Your credits: " << credits << endl;
	if (credits == 0)
	{
		cout << "You lose this game :(" << endl;
		system("PAUSE");
		exit(0);
	}
	return;
}
void p_c()
{
	system("PAUSE");
	system("cls");
	return;
}
void save_option1(fstream& save, string& game_name, string player_name, int credits)
{
	save.open("save.txt", ios::out | ios::app);
	if (save.good() == true)
	{
		cout << "Save you game. Enter game name: ";
		cin >> game_name;
		save << game_name << endl << player_name << endl << credits << endl;
		cout << "The game saved Successfully." << endl;
	}
	else
	{
		cout << "Program can't find save file :(";
		system("PAUSE");
		exit(0);
	}
	save.close();
	p_c();
	return;
}
void reading_from_a_file(string Save[], string Nick[], int Credits[], int& selected_save, string& player_name, int& credits)
{
	cout << "WELCOME IN C++ CASINO AGAIN!" << endl;
	cout << "Your save's: " << endl;
	std::ifstream save("save.txt");
	int i = 0;
	while (getline(save >> ws, Save[i]) && getline(save, Nick[i]) && save >> Credits[i] && i < 4)//zamiast 4 dynamika
	{
		cout << "[" << i + 1 << "]" << Save[i] << endl;
		++i;
	}
	cout << "Select which save do you want to play: ";//wywali jak się da coś wiecej niż jest savów
	cin >> selected_save;

	player_name = Nick[selected_save - 1];
	credits = Credits[selected_save - 1];
	return;
}
void menu_option2(string player_name, int& credits, int color, int credits_played, int random_number, fstream& save, string game_name, int &selected_save, string Save[], string Nick[], int Credits[])
{
	reading_from_a_file(Save, Nick, Credits, selected_save, player_name, credits);
	system("cls");
	cout << "Good luck and have fun " << player_name << "." << endl;
	cout << "Your credits: " << credits << endl;
	cout << "Let's start the game!" << endl;
	cout << player_name << " choose color : " << endl;
	cout << "1.RED" << endl;
	cout << "2.GREEN" << endl;
	cout << "3.BLACK" << endl;
	conditions_color_option(player_name, color);
	while (color < 1 || color>3)
	{
		system("cls");
		cout << "This option dosn't exits!" << endl;
		cout << player_name << " choose color : " << endl;
		cout << "1.RED" << endl;
		cout << "2.GREEN" << endl;
		cout << "3.BLUE" << endl;
		conditions_color_option(player_name, color);
	}
	cout << "How many credits do you want to play? ";
	conditions_color_option(player_name, credits_played);
	conditions_credits_option(player_name, credits, credits_played);
	draw_option(random_number);
	conditions_result_option2(player_name, credits, credits_played, color, random_number, Save, Nick, Credits, selected_save, game_name, save);
	return;
}
void conditions_result_option2(string player_name, int& credits, int credits_played, int color, int random_number, string Save[], string Nick[], int Credits[], int& selected_save, string game_name, fstream& save)
{
	if (random_number >= 1 && random_number <= 48)
	{
		if (color == 1)win_option1(player_name, credits, credits_played, 2);
		else lose_option1(player_name, credits, credits_played, "Red");
		save_option2(save, game_name, player_name, credits, Save, Nick, Credits, selected_save);
	}
	if (random_number >= 49 && random_number <= 51)
	{
		if (color == 2)win_option1(player_name, credits, credits_played, 14);
		else lose_option1(player_name, credits, credits_played, "Green");
		save_option2(save, game_name, player_name, credits, Save, Nick, Credits, selected_save);
	}
	if (random_number >= 52 && random_number <= 99)
	{
		if (color == 3)win_option1(player_name, credits, credits_played, 2);
		else lose_option1(player_name, credits, credits_played, "Black");
		save_option2(save, game_name, player_name, credits, Save, Nick, Credits, selected_save);
	}
	return;
}
void save_option2(fstream& save, string game_name, string player_name, int credits, string Save[], string Nick[], int Credits[], int selected_save)
{
	save.open("save.txt", ios::out | ios::trunc);
	Credits[selected_save - 1] = credits;
	if (save.good() == true)
	{
		cout << "You are saving game on save - " << Save[selected_save - 1] << "." << endl;
		for (int i = 0; i < 4; i++)//zamiast 4 dynamika
		{
			save << Save[i] << endl << Nick[i] << endl << Credits[i] << endl;
		}
	}
	else
	{
		cout << "Program can't find save file :(";
		system("PAUSE");
		exit(0);
	}
	save.close();
	p_c();
	return;
}

 

1 odpowiedź

+1 głos
odpowiedź 27 lutego 2019 przez niezalogowany
wybrane 27 lutego 2019 przez MrChick888
 
Najlepsza

Po przejrzeniu kodu proponuję użycie std::vector. Oczywiście możesz używać tablicy dynamicznej (link), ale napotkasz wiele problemów. Klasa std::vector oferuje wygodny dostęp do rozmiaru tablicy, dodawanie i usuwanie elementów, ustawianie wartości domyślnych w konstruktorze i wiele wiele innych.

Przykład:

#include <vector>

// ...

std::vector<int> credits(4); 

// ...

for (int i = 0; i < save.size(); i++)//zamiast 4 dynamika
{
	save << Save[i] << endl << Nick[i] << endl << Credits[i] << endl;
}

Przykład użycia w funkcji:

void print_size(const std::vector<int>& vec) {
    std::cout << vec.size() << "\n";
}

 

Trochę komplikujesz sobie kod, ale to wynika z braku wiedzy. Postaraj się ją uzupełnić i porobić troszkę mniejszych projektów. Wtedy będziesz mógł wygodniej i przyjemniej wykonać coś większego. Mam jeszcze kilka uwag odnośnie reszty kodu. Funkcja z 8 parametrami to coś złego - jest ich zdecydowanie za dużo. Nie musisz w każdej niezwracającej wartości funkcji używać return - taka funkcja sama zakończy swoje działanie. Rób to tylko kiedy w jakimś wariancie potrzebujesz przerwania jej działania. Praktycznie wszystkie std::string powinny być przesyłane przez referencję, ale to niekoniecznie tyczy się intów. Kod jest w tym kontekście trochę źle rozplanowany. Funkcja srand powinien znajdować się w main. Brakuje nagłówka <cstdlib>. W C++ do losowania używamy <random>. Do nazywania tablic używamy liczby mnogiej. Nie wszystkie nazwy są czytelne (np save, Save). Postaraj się porzucić używanie std::exit.

komentarz 27 lutego 2019 przez MrChick888 Obywatel (1,020 p.)

Dzięki wielkie, zastosuje porady i za parę dni wyśle kod.  smiley

Podobne pytania

0 głosów
2 odpowiedzi 390 wizyt
pytanie zadane 10 lutego 2019 w Sprzęt komputerowy przez MiszczuBD Mądrala (5,320 p.)
0 głosów
0 odpowiedzi 329 wizyt
pytanie zadane 11 stycznia 2019 w Sprzęt komputerowy przez WojtaZio Nowicjusz (150 p.)
0 głosów
1 odpowiedź 104 wizyt

92,963 zapytań

141,928 odpowiedzi

321,161 komentarzy

62,297 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Wprowadzenie do ITsec, tom 2

Można już zamawiać tom 2 książki "Wprowadzenie do bezpieczeństwa IT" - będzie to około 650 stron wiedzy o ITsec (17 rozdziałów, 14 autorów, kolorowy druk).

Planowana premiera: 30.09.2024, zaś planowana wysyłka nastąpi w drugim tygodniu października 2024.

Warto preorderować, tym bardziej, iż mamy dla Was kod: pasja (użyjcie go w koszyku), dzięki któremu uzyskamy dodatkowe 15% zniżki! Dziękujemy zaprzyjaźnionej ekipie Sekuraka za kod dla naszej Społeczności!

...