Need to fix switch statement and increasing an integer - c++

I'm making a very simple proof of concept for my parents and tried to create a simple management game, however, in my switch statement, the second case doesn't increase a material - stone. Any idea why? Any other advice would also be appreciated.
#include <iostream>
using namespace std;
int main()
{
bool alive = true;
int stone = 50;
int money = 20;
bool wall = false;
while(alive)
{
int MainChoice;
cout<<endl<<"stone: "<<stone<< " money: "<<money;
cout <<endl<<"You find yourself as the ruler of a castle what do you do?" << endl<<"1. Build a wall: cost 50 stone"<< endl<<"2. Send out miners:gets 50 stone"<< endl<<"3. Oppress the people"<<endl;
cin>>MainChoice;
//Main Menu Choices
switch(MainChoice)
{
//Build the wall
case 1:
if(stone>=50)
{
cout<<"You build the wall";
wall = true;
stone - 50;
}
else
{
cout<<"Insuffcient Stone";
}
break;
//Send out miners
case 2:
stone + 50;
cout<< "Gathered stone";
break;
// Oppress the people
case 3:
if(wall == false)
{
alive = false;
cout<<"You get killed in the riot";
}
else
{
cout<< "The wall saves you from death";
}
break;
default:
cout<<"im sorry that doesnt seem to be correct";
}
}
return 0;
}
Thank you, and sorry for the messy code.

For increasing your variable's value, you can use one of the concepts below:
stone += 50;
or
stone = stone + 50;

The problem is that these lines:
stone - 50;
...
stone + 50;
just calculate the result of the expression and then throws it away. There is no assignment. You could change them to:
stone -= 50;
...
stone += 50;

Related

Turning on random LEDs from a class

For a school project I need to randomly turn on LEDs in a color that is randomly chosen. So for example you can choose between red, blue, yellow and green. Then you need to pick one color randomly and randomly situated LEDs of that specific color on. The amount of LEDs that need to be turned on is input from the main document, I am trying to write these functions in a different class.
I need different arrays that contain the different LEDs of that color like:
int GrLeds[] = {LED_1, LED_5}; //the amount of LEDs can be changed
int ReLeds[] = {LED_2, LED_6};
int BlLeds[] = {LED_3, LED_7};
int GrLeds[] = {LED_4, LED_8);
Then one of these colors needs to be chosen randomly. I thought about putting the different color option in an array like the following:
int randomClr[] = {ledG, ledR, ledB, ledY};
But doing it like this would require me to link the ledG to GrLeds[] etc.
Is there a possibility to choose one of the arrays randomly, or something what would result in the same? I know Java has the option to use a list but that does not seem to be possible within c++.
What you are basically looking for is the random() function, which gives you a random number between an initial and final input numbers.
To integrate it within your code, as you are gonna manage more than one set of LEDs which is integrated by multiple LEDs, I would just create a matrix for that, and then choose a random row from that matrix (each row will represent a color), and turn on all the LEDs from that row.
Some pseudo-code that you can work with:
int randomClr[4][2] = {
{LED_1, LED_5},
{LED_2, LED_6},
{LED_3, LED_7},
{LED_4, LED_8}
};
// some code...
// Get a random number from 0 to 3
int randNumber = random(4);
for (int i = 0; i < 2; i++) {
// Your code to turn on the LEDs, for example:
digitalWrite(randomClr[randNumber][i], HIGH);
delay(100);
digitalWrite(randomClr[randNumber][i], LOW);
}
Your problem is kinda similar to an application that I developed some time ago which also involved some LEDs and randomness.
I wrote the following code for running some tests before migrating the functionalities to the Arduino ecosystem.
Feel free to reuse and adapt my code to your needs. Keep in mind that I wrote it to be tested on C++17 using Codelite and not for the Arduino platform, therefore you can replace the random function with the one from Arduino.
Hope it helps. If so, just show a bit of appreciation including the link to this answer in your code, for posterity ;)
#include <iostream>
#include <random>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
using namespace std;
#define MAX_COLORS 4
char textOut[100];
int cycles;
string colorNames[4] = { "RED", "BLUE", "GREEN", "PURPLE" };
typedef enum { RED, BLUE, GREEN, PURPLE } ColorList;
struct ColorsGroup {
ColorList colorCode;
string name;
};
ColorsGroup colorLED[4];
// Methods
int random(int, int);
ColorList retrieveColor(int);
void fillColors(void);
void printColors(int);
void setup()
{
fillColors();
cycles = 0;
}
int main()
{
cout << "********** Color picker *********" << endl;
setup();
while(cycles < 10) {
fillColors();
printColors(cycles);
cycles++;
}
return 0;
}
// From: https://stackoverflow.com/questions/7560114/random-number-c-in-some-range
int random(int min, int max)
{
static bool first = true;
if(first) {
srand(time(NULL));
first = false;
}
return min + rand() % ((max + 1) - min);
}
void fillColors(void)
{
for(int idx = 0; idx < MAX_COLORS; idx++) {
ColorList newColor = retrieveColor(random(0, MAX_COLORS - 1));
colorLED[idx].colorCode = newColor;
colorLED[idx].name = colorNames[newColor];
}
}
void printColors(int i)
{
sprintf(textOut, "%d. colorLED >> ", i);
cout << textOut;
for(int idx = 0; idx < MAX_COLORS; idx++) {
const char* nameStr = colorLED[idx].name.c_str(); // or &colorLED[idx].name[0];
sprintf(textOut, "%s[%d]", nameStr, colorLED[idx].colorCode);
cout << textOut;
if(idx <= MAX_COLORS - 2) {
sprintf(textOut, ", ");
cout << textOut;
}
else {
cout << ";" << endl;
}
}
}
ColorList retrieveColor(int col)
{
switch(col) {
case 0:
return RED;
break;
case 1:
return BLUE;
break;
case 2:
return GREEN;
break;
case 3:
return PURPLE;
break;
default:
return RED; // for the sake of completeness
break;
}
}
And this code spits out the following:

Problems creating Stack! (arcade game) in C++

im a beginner amateur in C++ and i recently had the idea to remake the popular arcade game Stack! in the C++ console.
The problem occours when the player has got to place the pad:
the pad places well, but if you make an error it doesn't resize properly (you'll understand better when you'll run the code).
Don't care about the graphic that sometimes bugs, because i can fix that on my own.
Please help me!
Here is the code:
#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <windows.h>
#include <string>
using namespace std;
bool bDirection = true; /* Bool for the direction:
true = dx, false = sx */
string sPad;
int nPadLenght = 6;
int x = 40, y =21; // Referement tile's position
int nSpeed = 200;
bool loop = true; // main loop
int nScore = 0; // score
int nPlaceX = 40;
int nTileX = 35, nTileY = 20; // Player's actual postition
int nEndTileX, nEndTileY;
void RenderLine(int *x, int *y);
int main();
// Void for the coordinates
void gotoxy (int x, int y){
COORD coord;
coord.X = x ;
coord.Y = y ;
SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void Victory(){
x = 10;
y = 4;
Beep(698.5, 300);
Beep(698.5, 100);
Beep(1047, 500);
system("color a");
gotoxy(x,y); cout << "You win!\n\n Score = " << nScore;
system("pause >nul");
}
void PadLenght(int *x){
// Each number is equal to a possible pad lenght
switch (*x){
case 6:
sPad = "[][][]";
nEndTileX = nTileX + 5;
break;
case 5:
sPad = "[][]]";
nEndTileX = nTileX + 4;
break;
case 4:
sPad = "[][]";
nEndTileX = nTileX + 3;
break;
case 3:
sPad = "[][";
nEndTileX = nTileX + 2;
break;
case 2:
sPad = "[]";
nEndTileX = nTileX + 1;
break;
case 1:
sPad = "[";
nEndTileX = nTileX;
break;
}
}
void SwitchDirection(bool *x){
// Switches the bool of the direction
switch (*x){
case true:
*x = false;
break;
case false:
*x = true;
break;
}
}
void Speed(){
// For each line from 500ms to 20ms speed increments of 10ms
if (nSpeed > 20)
nSpeed -= 20;
}
// void for placing the pad
void Place() {
int i = nPlaceX - nTileX;
if (i < 0)
i * -1;
nPadLenght -= i;
}
void collision(){
// Collisions with the border
if (nTileX > 45 || nTileX < 35)
SwitchDirection(&bDirection);
}
void movement(){
int nLastX = nTileX;
// Place the pad if pressing down arrow
if(GetKeyState(VK_DOWN) & 0x8000){
nTileY--;
Place();
Speed();
Beep(698.5, 50);
Beep(880.0, 50);
Beep(1047, 50);
nScore += 10;
Sleep(60);
}
// Movement of the pad
switch (bDirection){
case true:
gotoxy (nLastX, nTileY); cout << " ";
nTileX++;
break;
case false:
gotoxy (nLastX - nPadLenght, nTileY); cout << " ";
nTileX--;
break;
}
}
int main(){
system("color 0");
while (loop = true){
char a = '"';
gotoxy(x,y); cout << a << a << a << a << a << a;
collision();
PadLenght(&nPadLenght);
movement();
gotoxy (nTileX, nTileY); cout << sPad;
Sleep (nSpeed);
if (nScore > 160) {
Victory();
break;
}
}
return 0;
}
Here are some issues I found:
"using namespace std;"
This is bad. This brings in all of the identifier names from the std namespace. The preference is to use the std prefix (e.g. std::cout) or choose from the std namespace, (e.g. using std::cout;).
Global variables
Prefer to not have global variables. Create them in main and pass them to functions.
For example, you have global x and y, and you use x and y as parameters in functions. This may lead to confusion between you, readers and the compilers, as to which variables you are referring to.
Passing by pointer
Prefer not to use pointers.
Pass by value (without pointers) for variables that can fit into a processor's register, like float, double, int, bool and char.
For classes and structures, pass by reference. If you are not modifying the parameter, pass by constant reference.
Don't create functions for simple content
Functions require an overhead to execute, usually at least 3 instructions (save parameters, branch to function, return from function). Some operations can be performed with less statements:
// Switch directions:
direction = ! direction;
If you must use functions, give the compiler the hint that you want them inline. This means that the compiler will paste the content of the function where the function call is made. Some compilers may do this for optimizations, but you have to tell the compiler to optimize.
Boolean switch statements
Switch statements are kind of overkill for Boolean varibles, since there are only 2 outcomes. Common coding guidelines are to use if and else.
'=' in conditional expressions
Remember, one = for assignment, two for comparison.
The language allows for an assignment in a comparison, but most likely, you were not thinking of assignment, but testing for equality.
Declare variables closest to usage
Make life easier on you, the compiler and reader, by declaring variables closest to where they are used. For example, in main, there is a loop variable. As a reader, I have to scroll to the top of your source to find the definition, when a better idea is to declare it in the main function, where it is used.
One variable declaration per line
A common coding guideline is one variable per line. Multiple lines have a negligible effect on the build time. However, this makes modifications easier. And when declaring pointers, reduces injected defects. Let your compiler optimize the code, your task should be write clear (easily readable) and concise code.
Increase the compiler's warning level
Force your compiler to turn on the warning level to its highest level.
Resolve all warnings.
A clean compilation has zero errors and zero warnings.
With compiler warnings at full, you would have noticed some of the issues I have identified.

Can't figure out how to loop playerturns and moves Tic Tac Toe (C++) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
EDIT: Solved now thank you triple_r and AJNeufield for your help on this problem I was having.
I've looked around multiple websites and YouTube about this and I can't seem to find anything on what I am specifically looking for this as my format for the program is a good bit different than others. Therefore, it's hard to decipher where I need to put the things I do need that I know of.
Please note that I'm relatively new to C++ so I'd appreciate all the feedback or criticism you might provide me.
Also, note my code does compile and run it just does not allow me to put in more than one input and more than likely does not allow for a switch of player turns.
Quick Edit: Switched the code with the new setup suggested by triple_r but I seemed to have messed it up somewhere along the line and it does compile(with the exception of x and y not being utilized and one other error) but it always starts off with player 2 going first and as soon as it receives input it ends automatically with a segmentation fault.
#include <iostream>
#include <cstdlib>
using namespace std;
//////////////////////////////////////////////////////////
void initboard(char board[3][3])
{
int x,y;
for (x=0;x<3;x++)
for (y=0;y<3;y++)
board[x][y]=' ';
return;
}
//////////////////////////////////////////////////////////
void printboard(char board[3][3])
{
int x,y;
for (x=0;x<3;x++)
{
cout<<"\n";
for (y=0;y<3;y++)
{
cout<<" "<<board[x][y]<<" ";
if (y<2) cout<<"|";
}
if (x<2) cout<<"\n===========";
}
return;
}
//////////////////////////////////////////////////////////
void getmove(char board[3][3], int player)
{
return;
}
//////////////////////////////////////////////////////////
int main()
{
bool done=false;
char board[3][3];
int x,y,player=1,turn,playerchoice,playermark;
initboard(board);
turn=0;
do
{
if (player==1)
playermark='X';
else
playermark='O';
if (turn%2)
player=1;
else
player=2;
cout<<"Player "<<player<<" where do you want to move?: ";
cin>>playerchoice;
if (playerchoice==1)
{
board[0][0]=playermark;
}
else if (playerchoice==2)
{
board[0][1]=playermark;
}
else if (playerchoice==3)
{
board[0][2]=playermark;
}
else if (playerchoice==4)
{
board[1][0]=playermark;
}
else if (playerchoice==5)
{
board[1][1]=playermark;
}
else if (playerchoice==6)
{
board[1][2]=playermark;
}
else if (playerchoice==7)
{
board[2][0]=playermark;
}
else if (playerchoice==8)
{
board[2][1]=playermark;
}
else if (playerchoice==9)
{
board[2][2]=playermark;
}
else
{
cout<<"Invalid move ";
}
if (board[x][y]!=' ')
cout<<"Move is already taken.";
board[x][y]=playermark;
if(board[x][y]==' ')
turn++;
}while (!done);
void printboard(char board[3][3]);
return 0;
}
EDIT: based on the updated code
So, the first thing I can see is that you are using x and y in your program but you don't initialize them or assign any values to them. Also, try to use functions/classes/... yo make your code more readable. You already have a function for player move but you are not using it. You can move the large if statement inside that function and that will make your main code shorter and more readable.
Here are my comments on the main part of your program:
int main()
{
// add a new variable to see if the move was valid or not:
bool done=false, validmove = true;
char board[3][3];
int x, y, player = 1, turn = 0, playerchoice, playermark;
initboard(board);
do
{
// swap the two `if`s so you decide who`s turn it is then assign the player mark,
// also, reverse the condition to make sure turn '0' is player 1's turn.
if (!(turn % 2))
player = 1;
else
player = 2;
if (player == 1)
playermark = 'X';
else
playermark = 'O';
cout << "Player " << player << " where do you want to move?: ";
cin >> playerchoice;
// Assign `x` and `y` here instead of updating the board, because you want to make
// sure that the move is valid before putting the mark:
validmove = true;
if (playerchoice == 1)
{
x = 0; y = 0;
}
else if (playerchoice == 2)
{
x = 0; y = 1;
}
else if (playerchoice == 3)
{
x = 0; y = 2;
}
else if (playerchoice == 4)
{
x = 1; y = 0;
}
else if (playerchoice == 5)
{
x = 1; y = 1;
}
else if (playerchoice == 6)
{
x = 1; y = 2;
}
else if (playerchoice == 7)
{
x = 2; y = 0;
}
else if (playerchoice == 8)
{
x = 2; y = 1;
}
else if (playerchoice == 9)
{
x = 2; y = 2;
}
else
{
cout << "Invalid move, try again!";
// Make sure to mark the move as invalid so they get a chance to
// change their move:
validmove = false;
}
// check to see if the turn was valid:
if(validmove)
{
if (board[x][y] != ' ')
{
cout << "Move is already taken, try again";
}
else
{
board[x][y] = playermark;
turn++;
}
}
// have to make sure you have a condition for end of game. A simple
// one is to check if turn is less than `9`, otherwise the board is
// full:
if(turn == 9)
done = true;
// you probably want to add a few more checks to see who won the game.
}while (!done);
// when calling a function, no need to put the return type or parameter type:
printboard(board);
return 0;
}
========================================================================
There are two do-while loops in your program and both seem to be meant as a game loop. What I would do is:
initboard(...);
turn = 0;
do{
//this is the game loop
...;
if( validturn )
turn++;
}while(!done);
release_resources(...);
return 0;
so, you fold everything into one loop. In that loop, you want to:
find who's turn it is:
if (turn % 2)
player = 1;
else
player = 2;
get users input:
std::cin >> playerchoice;
...
convert player choice to grid location:
switch ( move )
{
case 0:
x = 0;
y = 0;
break;
case 1:
...;
...
default:
//invalid move
}
see if the move is valid:
if( board[x][y] != ' ' )
//already taken, invalid move
then apply the move:
board[x][y] = playermark;
I hope this helps.
Your cin >> playerchoice is outside your do { ... } while ( moves != 9); loop. Move it inside.

Adding split-screen multiplayer to c++ game

I am coding for the NDS in c++ with libnds, but this question is not NDS-Specific. I currently have a text-based game in which the top screen just displays a logo, and you play on the bottom screen.
So I want to add a type of single-DS multiplayer in which one player plays on the top screen, and the other on the bottom. I dont have a problem with setting up the text engine with both screens, I just need to find a method of efficiently coding in multiplayer. Below I wrote a summary or simplified version of it.
Note: consoleClear() clears the screen and the only spot where the game stops is att the pause function.
//Headers
void display(int x,int y,const char* output))
{
printf("\x1b[%d;%dH%s", y, x,output);
}
void pause(KEYPAD_BITS key) //KEYPAD_BITS is an ENUM for a key on the NDS
{
scanKeys();
while (keysHeld() & key)
{
scanKeys();
swiWaitForVBlank();
}
while (!(keysHeld() & key))
{
scanKeys();
swiWaitForVBlank();
}
return;
}
void pause() //Only used to simplify coding
{
pause(KEY_A);
return;
}
int main(void)
{
//Initializations/Setup
while (1)
{
if (rand()%2==1) //Say Hello
{
if (rand()%3!=1) //To Friend (greater chance of friend than enemy)
{
display(6,7,"Hello Friend!");
display(6,8,"Good greetings to you.");
pause();
consoleClear(); //Clears text
display(6,7,"Would you like to come in?");
pause();
//Normally more complex complex code (such as interactions with inventories) would go here
}
else //To enemy
{
display(6,7,"Hello enemy!");
display(6,8,"I hate you!");
pause();
consoleClear();
display(6,7,"Leave my house right now!!!");
pause();
}
}
else //Say goodbye
{
if (rand()%4==1) //To Friend (lesser chance of friend than enemy)
{
display(6,7,"Goodbye Friend!");
display(6,8,"Good wishes to you.");
pause();
consoleClear();
display(6,7,"I'll see you tomorrow.");
pause();
consoleClear();
display(6,7,"Wait, I forgot to give you this present.");
pause();
}
else //To enemy
{
display(6,7,"Goodbye enemy!");
display(6,8,"I hate you!");
pause();
consoleClear();
display(6,7,"Never come back!!");
pause();
consoleClear();
display(6,7,"Good riddance!"); //I think I spelt that wrong...
pause();
}
}
}
}
I know gotos are confusing and can be considered a bad habit, but I cant think of a better way. My version of integrating multiplayer:
//Headers and same functions
int game(int location)
{
switch (location)
{
case 1: goto one; break;
case 2: goto two; break;
case 3: goto three; break;
case 4: goto four; break;
case 5: goto five; break;
case 6: goto six; break;
case 7: goto seven; break;
case 8: goto eight; break;
case 9: goto nine; break;
case 10: goto ten; break;
default: break;
}
if (rand()%2==1) //Say Hello
{
if (rand()%3!=1) //To Friend (greater chance of friend than enemy)
{
display(6,7,"Hello Friend!");
display(6,8,"Good greetings to you.");
return 1;
one:;
consoleClear(); //Clears text
display(6,7,"Would you like to come in?");
return 2;
two:;
//Normally more complex complex code (such as interactions with inventories) would go here
}
else //To enemy
{
display(6,7,"Hello enemy!");
display(6,8,"I hate you!");
return 3;
three:;
consoleClear();
display(6,7,"Leave my house right now!!!");
return 4;
four:;
}
}
else //Say goodbye
{
if (rand()%4==1) //To Friend (lesser chance of friend than enemy)
{
display(6,7,"Goodbye Friend!");
display(6,8,"Good wishes to you.");
return 5;
five:;
consoleClear();
display(6,7,"I'll see you tomorrow.");
return 6;
six:;
consoleClear();
display(6,7,"Wait, I forgot to give you this present.");
return 7;
seven:;
}
else //To enemy
{
display(6,7,"Goodbye enemy!");
display(6,8,"I hate you!");
return 8;
eight:;
consoleClear();
display(6,7,"Never come back!!");
return 9;
nine:;
consoleClear();
display(6,7,"Good riddance!"); //I think I spelt that wrong...
return 10;
ten:;
}
return -1;
}
}
int main(void)
{
//Initializations/Setup
int location1 = -1, location2 = -1;
location1 = game(location1);
location2 = game(location2);
while (1)
{
scanKeys(); //Whenever checking key state this must be called
if (keysDown() & KEY_A) //A key is used to continue for player1
location1 = game(location1);
if (keysDown() & KEY_DOWN) //Down key is used to continue for player2
location2 = game(location2);
}
}
Aside from this method being a bad practice, in the actual source code, I have hundreds of gotos I would need to add which would be too time consuming.
Any help is appreciated. If anyone has the slightest of a question, or answer, please ask/reply.
Edit: Though it is not preferred to do so, I am willing to rewrite the game from scratch if someone has a method to do so.
Using if-else conditional statements for each case is a simple solution that comes first to mind.
For example:
int game(int i){
if(i == 1){
//first case code here.
}
else if(i == 2){
//second case code here.
}
//....
return 0;
}
The code in each case can even be put in other functions that will be invoked depending on each condition.
This will probably be enough for your case.
A more flexible solution (but much more complex) is a dispatch table.
The idea is to have separate functions with each desired functionality, and put pointers of them in an array. Then, you can call them by indexing the table, using those function pointers. This can be extremely helpful if you have a sequence of executions (function invokes) to be done and you want to set it done easily, or you want to have different results depending on your input, without changing your program.
There is an example below.
This code can be used in C too, if you replace std::cout with printf and iostream with stdio library.
#include <iostream>
using namespace std;
// Arrays start from 0.
// This is used for code
// readability reasons.
#define CASE(X) X-1
typedef void (*chooseCase)();
// Functions to execute each case.
// Here, I am just printing
// different strings.
void case1(){
cout<< "case1" << endl;
}
void case2(){
cout<< "case2" << endl;
}
void case3(){
cout<< "case3" << endl;
}
void case4(){
cout<< "case4" << endl;
}
//Put all the cases in an array.
chooseCase cases[] = {
case1, case2, case3, case4
};
int main()
{
//You can call each scenario
//by hand easily this way:
cases[CASE(1)]();
cout << endl;
//Idea: You can even set in another
// array a sequence of function executions desired.
int casesSequence[] = {
CASE(1), CASE(2), CASE(3), CASE(4),CASE(3),CASE(2),CASE(1)
};
//Execute the functions in the sequence set.
for(int i = 0; i < (sizeof(casesSequence)/sizeof(int)); ++i){
cases[casesSequence[i]]();
}
return 0;
}
This will print at the output:
case1
case1
case2
case3
case4
case3
case2
case1

How to efficiently structure a terminal application with multiple menus?

I'm writing a console based program for my coursework, and am wondering how best to structure it so that it is both stable and efficient. I currently have
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int choice;
do
{
cout << "\E[H\E[2J" // Clear the console
<< "Main menu" << endl << endl
<< "Please select one of the following options by entering it's "
<< "number in the prompt below and pressing [ENTER]. "
<< endl << endl
<< "1. Pay my bill as a guest" << endl
<< "3. Log in" << endl
<< "2. Create an account" << endl
<< "4. Quit program" << endl;
cin >> choice;
switch (choice)
{
case 1: // Pay the bill as a guest to the system
case 2: // Log in to the system
case 3: // Create an account with the system
case 4: // Quit the program
default: // Prompt the user to choose again
}
} while !(default);
// Await user input to terminate the program
cout << "Please press [ENTER] to continue...";
cin.get();
return 0;
}
The purpose of the above code is to provide a list of options for the user to choose from, with the do-while loop working alongside the default statement in the switch to catch any unexpected input. Each case would call a function that presented another menu with it's own list of options, which would be structured using the same do-while, switch method. My concern is that as my program grows, the number of function calls being nested within other functions is going to increase, so that I would eventually end up with a function being called from within a function being called from within a function and so on. This would obviously have severe implications for the maintainability of the program, with the function calls moving further and further away from main(), and the output of these functions weaving a tangled path about the program.
Is it possible to structure my program in such a way as to return execution to main() as often as possible, or is the problem described above simply a consequence of this kind of programming?
NB: I ask this question in the understanding that user-defined functions are supposed to be ancillary to main(), and that they should perform a task before returning control to main() as the earliest possible convenience. I've only been at this a couple of months now so please bear with my ignorance/misunderstanding. Also, ignore any potential compiler errors in the code, I've not tested it yet and it's only provided as an aide to my conceptual question.
I would apply some OO-design and create a menu-class which basically stores items/sub-menus in a vector. This would make it easy to extemd to hierarchical menus
There is nothing particularly wrong with what you've done.
I don't see why it harms maintainability to have functions called from functions and so on. If anything it AIDS maintainability as you can move common code operations into seperate functions. This way you make a fix in one place and instantly fix the rest of the places its used as well.
Well, you can implement your menu structure as a state-machine, so you will be almost always in your main loop. But this can bring your code to the lower level, because you will be effectively programming not in C++ but in your state-machine processor-near code. If your state machine will be good enough, this is not a problem.
Or you can a simple menu-runner class, which will output as a result a request for submenu, so you will just exchange (perhaps using a stack) the description of the currently running menu.
By the way, I don't see any problems in deep nesting of the functions.
Another possible approach is to make a class defined as a list of (menu_option, function) pairs and the know-how to turn them into menus. Then the function can be a call to another class instance's menu or it can do some operation on your database. That lets you keep your data organized away from the business "how to display this menu" logic and add menus and menu items easily.
Don't worry about that or your current approach spending too much time away from main though. As you've structured it, your program won't automatically turn itself into a horrible mess just because you're calling functions from functions. More functions will tend to add to maintainability, as long as you keep them focused.
Think of it this way: a function does one thing, but at a higher level of abstraction than its body. So main() runs your program. create_account() will create an account, which is part of running the program. create_account itself calls several other things that do the building blocks necessary for creating an account. Is determining the new account's name one thing? It goes in its own function. Determining the index of the new account in the database? Too low-level. Put it in the "stuff it in the database" function.
The complexity of the code will correlate to the functionality offered by the program. I would not worry about this right now, revisit refactoring once you have two or three hundred lines.
for use this code you must add:
st_menues[cnt].pos = cnt;
st_menues[cnt].function_pointer = tmp_func;
strcpy_s(st_menues[cnt].menu_string, "kharid");
memcpy(st_menues[cnt++].layer, "100000", sizeof(st_menues[cnt].layer));
this block code for insert menu item .
for change item to subitem you must change:
"100000"
to
"010000"
for do a difference work per every item you must define multiple:
void tmp_func(char* str)
void tmp_func1(char* str)
void tmp_func2(char* str)
...
and insert in
st_menues[cnt].function_pointer = tmp_func1;
...
st_menues[cnt].function_pointer = tmp_func2;
...
and insert your source code in these functions.
this source can compile in vc++.
i didn't test its in linux . but perhups works.
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
char title_str[20] = " main menu \n";
void print_this(bool with_title,const char* str, ...)
{
if(with_title)printf(title_str);
printf(str);
}
void clear()
{
#ifdef _WIN32
system("cls");
#else
std::cout << "\033[2J\033[1;1H";
#endif
print_this(true,"");
}
struct def_struct_menu
{
void (*function_pointer)(char*);
char menu_string[24];
char layer[7];
int pos;
};
void set_title(char* str)
{
sprintf(title_str," %s \n",str);
}
void tmp_func(char* str)
{
clear();
set_title(str);
printf("calc okokok");
_getch();
}
def_struct_menu st_menues[100] = { 0 };
def_struct_menu st_cur_menues[100] = { 0 };
void back_to_main_menu(int& highlight_line, int& cur_layer, int& start)
{
highlight_line = 0;
cur_layer = 0;
start = 0;
set_title((char*)"main menu");
}
int main()
{
int cnt = 0;
st_menues[cnt].pos = cnt;
st_menues[cnt].function_pointer = tmp_func;
strcpy_s(st_menues[cnt].menu_string, "kharid");
memcpy(st_menues[cnt++].layer, "100000", sizeof(st_menues[cnt].layer));
{
st_menues[cnt].pos = cnt;
st_menues[cnt].function_pointer = tmp_func;
strcpy_s(st_menues[cnt].menu_string, "asan");
memcpy(st_menues[cnt++].layer, "010000", sizeof(st_menues[cnt].layer));
st_menues[cnt].pos = cnt;
st_menues[cnt].function_pointer = tmp_func;
strcpy_s(st_menues[cnt].menu_string, "shenase");
memcpy(st_menues[cnt++].layer, "010000", sizeof(st_menues[cnt].layer));
}
st_menues[cnt].pos = cnt;
st_menues[cnt].function_pointer = tmp_func;
strcpy_s(st_menues[cnt].menu_string, "sharj");
memcpy(st_menues[cnt++].layer, "100000", sizeof(st_menues[cnt].layer));
{
st_menues[cnt].pos = cnt;
st_menues[cnt].function_pointer = tmp_func;
strcpy_s(st_menues[cnt].menu_string, "ramz");
memcpy(st_menues[cnt++].layer, "010000", sizeof(st_menues[cnt].layer));
{
st_menues[cnt].pos = cnt;
st_menues[cnt].function_pointer = tmp_func;
strcpy_s(st_menues[cnt].menu_string, "yekbarmasraf");
memcpy(st_menues[cnt++].layer, "001000", sizeof(st_menues[cnt].layer));
st_menues[cnt].pos = cnt;
st_menues[cnt].function_pointer = tmp_func;
strcpy_s(st_menues[cnt].menu_string, "qrcode");
memcpy(st_menues[cnt++].layer, "001000", sizeof(st_menues[cnt].layer));
}
st_menues[cnt].pos = cnt;
st_menues[cnt].function_pointer = tmp_func;
strcpy_s(st_menues[cnt].menu_string, "mostaghim");
memcpy(st_menues[cnt++].layer, "010000", sizeof(st_menues[cnt].layer));
}
const int ST_SIZE = cnt;
int input = 0;
int highlight_line = 0;
int cur_layer = 0;
int start = 0;
while (input != -1)
{
int size = 0;
memset(st_cur_menues, 0, sizeof(def_struct_menu) * ST_SIZE);
for (int i = start; i < ST_SIZE; i++)
{
if (cur_layer > 0)
{
if (st_menues[i].layer[cur_layer - 1] == '1')
{
break;
}
}
if (st_menues[i].layer[cur_layer] == '1')
{
memcpy(&st_cur_menues[size++], &st_menues[i], sizeof(def_struct_menu));
}
}
clear();
if (size == 0)
{
back_to_main_menu(highlight_line, cur_layer, start);
}
for (int i = 0; i < size; i++)
{
if (highlight_line == i)
print_this(false,"*");
else
print_this(false," ");
print_this(false,st_cur_menues[i].menu_string);
print_this(false,"\n");
}
//print_this("enter number\n");
input = _getch();
switch (input)
{
case 'x':
{
exit(0);
}
case 27://escape button
{
back_to_main_menu(highlight_line, cur_layer, start);
break;
}
case 13://enter button
{
if (size == 0)
{
back_to_main_menu(highlight_line, cur_layer, start);
break;
}
st_cur_menues[highlight_line].function_pointer(st_cur_menues[highlight_line].menu_string);
start = st_cur_menues[highlight_line].pos + 1;
cur_layer++;
highlight_line = 0;
}
break;
case 72://up arrow key
{
if (highlight_line == 0)
highlight_line = (size - 1);
else
highlight_line--;
}
break;
case 80://down arrow key
{
if (highlight_line == (size - 1))
highlight_line = 0;
else
highlight_line++;
}
break;
default:
break;
}
}
return 0;
}
tnx