My menu function looks like this and it works great:

int menu(string * teksty, int ile)
{
  static HANDLE h_we = GetStdHandle(STD_INPUT_HANDLE),
                h_wy = GetStdHandle(STD_OUTPUT_HANDLE);
  int wybor = 1;
  CONSOLE_CURSOR_INFO cci = {0};
  GetConsoleCursorInfo(h_wy, &cci);
  cci.bVisible = false;
  SetConsoleCursorInfo(h_wy, &cci);
  COORD p = {0};
  DWORD k;
  //FillConsoleOutputAttribute(h_wy, 0x07, 80 * 25, p, &k);
  //FillConsoleOutputCharacter(h_wy, 0x20, 80 * 25, p, &k);
  while(1)
  {
    INPUT_RECORD ipr = {0};
    DWORD p = 0;
    for (int i = 0; i < ile; i++)
    {
      COORD poz = {21, 5 + i};
      DWORD p;
      SetConsoleCursorPosition(h_wy, poz);
      SetConsoleTextAttribute(h_wy, (wybor == i + 1) ? 0x84 : 0x07);
      cout << teksty[i];
    }
    if (ReadConsoleInput(h_we, &ipr, 1, &p) && p == 1 &&
        ipr.EventType == KEY_EVENT && ipr.Event.KeyEvent.bKeyDown)
     switch (ipr.Event.KeyEvent.wVirtualKeyCode)
     {
       case VK_UP:
        wybor = (wybor == 1) ? ile : wybor - 1;
        break;
       case VK_DOWN:
        wybor = (wybor == ile) ? 1 : wybor + 1;
        break;
       case VK_RETURN:
        cci.bVisible = true;
        SetConsoleCursorInfo(h_wy, &cci);
        return wybor;
     }
  }
}

Unfortunatelly I dont define array of options for it, It reads whole array from file..and it also works great...

void load_config()
{
      ile_opcji=0;
      string filename,line;
      filename="config.ini";
      ifstream plik_wejsciowy (filename.c_str());
      if (plik_wejsciowy.is_open())
      while (! plik_wejsciowy.eof() )
            {
            ile_opcji++;
            getline (plik_wejsciowy,line);
            if (line == "") continue;
            opcje_global[ile_opcji]=line;
            }
       ile_opcji++;
       opcje_global[ile_opcji]="CUSTOM";
       ile_opcji++;
       opcje_global[ile_opcji]="REMOVE";
       ile_opcji++;
       opcje_global[ile_opcji]="DONE";
 }

Unfortunatelly, as far as I know..its impossible to make Case+variable statement. And I need to do that in order to create dependence between number of options and choices.
but...I dont know why only Case solution works...I dont know wht IF...else doesnt work (there is no error but when i choose option that correspons with number defined in if else statement, nothing happens) please HELP!

void system_affected()
{
     char input[ 45 ];
     int ai,az;
     int token;
     az=1;
     ai=1;
     bool wyjscie;
     wyjscie=0;
     system ("cls");
     load_config();
     gotoxy(1,15); cout << "Templater";
     while (wyjscie !=1)
     {
     system ("cls");
     gotoxy(1,1); cout << "Systems affected: ";
     //wyswteitla awarie systemow
     wyswietl(i);
     cout << token;
     // koniec wyswietlania
     switch (menu(opcje_global+1, ile_opcji))
     {
            case 1:
                 cout << "It works";
                 system ("pause");
                 break;
            if (menu(opcje_global+1, ile_opcji)==2)
               {
               cout << "It doesnt work - WHY?";
               system ("pause");
               break;                   
            }
            }
}

I have noticed one thing pretty weird in your code (the last bit). Why did you put an if-statement into a switch body? I bet that is the problem. Only use case's and default in your switch body.

One word of warning. It's an English forum. If you're posting some examples or bits of original code please try to use English names for variables. I'm Polish but I'm not the only person on the forum. :)

Edited 6 Years Ago by venomxxl: n/a

yeah You are right...I should have translated all variables..but now I cant find EDIT button to do that :/

Btw.. Thx friend for help...so how the code should look like (and where to put if statement) to make it actually work?
I will explain what I am looking for.
There is a file CONFIG.INI...with name of systems
-------------
WCP
INTERNET
SYSTEM 1
SYSTEM 2
etc.
-------------

load_config() reads all lines as a elements in array..
Menu function creates a menu with elements from that array.
now what I need to do is to create dependece between those menu items from array and if statements.
because number of IFs will vary depending on the number of items in array, I cannot use case statement.I need to make multiple IFs (in a for loop)...So where should i post my lovely IFs to make it actually work?

Thx one more time for your help...

Edited 6 Years Ago by VasquezPL: n/a

now what I need to do is to create dependece between those menu items from array and if statements.
because number of IFs will vary depending on the number of items in array, I cannot use case statement.I need to make multiple IFs (in a for loop)...So where should i post my lovely IFs to make it actually work?

Hmm, I don't know if I understand what you mean by saying "create dependence" and I don't really understand the code (not into Windows API console stuff).

My guess is: You want to turn the return from menu function call menu(opcje_global+1, ile_opcji) into a string corresponding to the selection you made.

If that is so, try to do that:

// If type of opcje_global is std::string.
std::string Selection = opcje_global[menu(opcje_global+1, ile_opcji)];

Correct me if I'm wrong.

You may use if..else statement inside a case of a switch. For example,

int i = 2;
bool a = true;
switch(i)
{
   case 1: cout << "Case is 1";
     break;
   case 2:
     if(a)
        cout << "Case 2 and boolean a is true";
     else
        cout << "Case 2 and boolean a is false";
     break;
   default: cout << "Default case";
}

I will try to explain:
transslation of variables:
opcje_global = options_global
ile_opcji = how many options;

function load_config loads MENU ITEMS and put them into array. Number of MENU ITEMS may vary (it depends how many lines i put into config.ini file)..
how many items are in files are written into ile_opcji (how many options) variable.
all MENU ITEMS are stored in opcje_global array.

void load_config()
{
      ile_opcji=0;
      string filename,line;
      filename="config.ini";
      ifstream plik_wejsciowy (filename.c_str());
      if (plik_wejsciowy.is_open())
      while (! plik_wejsciowy.eof() )
            {
            ile_opcji++;
            getline (plik_wejsciowy,line);
            if (line == "") continue;
            opcje_global[ile_opcji]=line;
            }
       ile_opcji++;
       opcje_global[ile_opcji]="CUSTOM";
       ile_opcji++;
       opcje_global[ile_opcji]="REMOVE";
       ile_opcji++;
       opcje_global[ile_opcji]="DONE";
 }

switch (menu(opcje_global+1, ile_opcji)) creates a menu from array of MENU ITEMS which are stored in opcje_global variable, ile opcji tells function how many elements are there....It creates a menu and gives an output in a number of chosen option...So if I choose first element it gives 1, when i choose 2 option it returns 2 etc....
random number of items in my menu (depends on what i put in config.ini file - there can be 2 elements, or 12) results in sitation where I cannot use case statement...I need to use IF statement + for loop (it has to check IFS for every element in my menu)..I know how to do FOR loop, but I dont know how to make IFs..ELSE which should be used to replace CASE..Case Return_number_of_option_from_menu works, the same thing with IF...else...doesnt....
thats the major problem for me...

Ok I have found partial solution :)

menu(opcje_global+1, ile_opcji);
    for (z = 0; z <= ile_opcji; z++)
           {
           if (menu(opcje_global+1, ile_opcji)==z)
                                    {
                             awaria[i]=opcje_global[z];
                             cout << opcje_global[z];
                             i++;
                             }
           }

I have removed switch statement completly :) meh :P
Now I have diffrent problem..I dunno why but i must choose option 3 times to make it actually work...there is sth wrong with my loop?
Thx!

Edited 6 Years Ago by VasquezPL: n/a

int ret = menu(opcje_global+1, ile_opcji);
string selected_item;

for(int i = 1; i <= ile_opcji; i++)
{
    if(i == ret)
    {
        selected_item = opcje_global[i - 1];
    }
}

is the same as

string selected_item = opcje_global[menu(opcje_global+1, ile_opcji) - 1];

> Now I have diffrent problem..I dunno why but i must choose option 3 times to make it actually work...there is sth wrong with my loop?

In your code you call menu function before the loop, and twice in the loop (you probably selected option 1). If you wanted to select option 2 you'd have to select it 4 times. You can try that out with your code.

Edited 6 Years Ago by venomxxl: n/a

THAT WORKS MATE!
I owe you a beer :)

My serious problem is that I started to program in C++ 2 months ago..and I learn by writting that program and adding new functions :)...I have several books but sometimes I cannot find soulutions for my problem...
Thx for your help mate!

one more thing (probably last)
how to change your function to do diffrent things for menuitems "CUSTOM","REMOVE","DONE"
for example when you choose custome - it should cout << "your custom option";
For remove sth simmilar
and for done - break;

that would be last thing :)
thx once again!

OK I found the solution :)

THX GUYS!

for(int z = 1; z <= ile_opcji; z++)
    {
            if (z == ile_opcji-3)
            {
                gotoxy(1,16);
                cout << "Please enter affected system name:";
                cin.getline( input, 45 );
                awaria[i]=input;
                i++;
                system ("cls");
                break;
            }
            else
            if(z == ret)
            {
            selected_item = opcje_global[z];
            cout << selected_item;
            awaria[i]=selected_item;
            i++;
            break;
            }
This article has been dead for over six months. Start a new discussion instead.