c++ programming help

hey i'm new to c++ , i am making a program which uses switch case, can anyone tell me how should I code this switch part so that whatever choice I enter calls the corresponding function (or prints the output related to that particular choice) and not the choice I have entered.

an example would be a great help.
its still of no help, i don't want my entered choice to get printed .
There's no way in which you can delete a line in a console window after the user pressed enter.
Of course it won't give you exactly what you want, as is. It just shows you how switch statements work.

Post your code to get more help.
There's no way in which you can delete a line in a console window after the user pressed enter.


You can do it this way:

Create the following function:
1
2
3
4
5
6
void gotoxy(int x,int y)
{
        HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
        COORD position = {x, y};
        SetConsoleCursorPosition(handle, position);
}


Set the cursor to the position of the line where the user made a choice, cout spaces and return the cursor:
1
2
3
4
5
6
gotoxy( 0, 2 );

for( int i = 0; i < 20; i++ )
    std::cout << ' ';

gotoxy( 0, 3 );
This is like the most basic C++ stuff there is.. I made a basic template of what you need and you can edit the code yourself. If this is for school though you should really understand how it works, it's not that complicated. I commented it all for you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream> //Include "input output stream" to use cout and cin functions.
using namespace std; //So we don't have to do std::cout/cin every time we use it

int main()
{
     //All an enum does is take your variables and apply a number to them.. if you
     //remove =1 after FirstChoice then it will start at 0 instead of 1
     enum Choice { FirstChoice = 1, SecondChoice, ThirdChoice, FourthChoice };
     //Sets up a new int variable named myChoice
     int myChoice;

     //Outputs "enter your choice" to the console and then inputs your response to myChoice
     cout << "Enter your choice: ";
     cin >> myChoice;

//Starts the switch statement for the myChoice variable
switch(myChoice)
    {
  //Remember FirstChoice was set to "1" in our enumeration, so if you enter 1
  //then this code executes.. the break; line ends the switch
  case FirstChoice:
    cout << "You entered 1." << endl;
    break;
  case SecondChoice:
    cout << "You entered 2." << endl;
    break;
  case ThirdChoice:
    cout << "You entered 3." << endl;
    break;
  case FourthChoice:
    cout << "You entered 4." << endl;
    break;
  //The default code will come up if you enter any number other than the ones above
  default:
    cout << "ERROR! You entered an invalid number." << endl;
    break;
    }
  return 0;
}
Last edited on
Topic archived. No new replies allowed.