Still stuck on menu

Instead of converting to SDL like I was told, I am being stubborn and trying to figure this problem out still. The following switch statement (menu):
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
using namespace std;

#define ESC 27
#define UP 72
#define LEFT  75
#define RIGHT 77
#define DOWN  80
#define ENTER 13


void X(const char *prompt)
{
   puts(prompt);
   for ( ;; )
   {
	  int ch = _getch();
	  switch(ch)
	  {
	  case EOF: {}
	  case ESC: {return;}
	  case UP:	{ cout << "UP";  break;}
	  case LEFT: { cout << "LEFT"; break;}
	  case RIGHT: { cout << "RIGHT"; break;}
	  case DOWN: { cout << "DOWN"; break;}
	  //case ENTER: puts("ENTER"); system("pause"); clr(); break;
	  default:{	if(isprint(ch)) putchar(ch); break;}
	  }
   }
}
int main(void)
{
   X("\n\n\n\n\t\t\t\tContinue\n\n\t\tLoad Game\t\t\tNew Game\n\n\t\t\t\tOptions");
   return 0;
}

Works in a sense, but I want it work in a way where you can only select one direction, and once the choice is selected that you are no longer able to use the arrow keys to change it up. Currently, I can hold the up button all day and it'll keep saying UP. How can I get it to select once and exit out of the switch statement? I feel like the answer is super easy I just can't put my programming fingers onto it. Any help or links or anything, is always appreciated! Thank you for reading atleast.
That's because the answer is easy, get rid of the for loop on Line 14.

What went wrong with SDL? If you need a hand I can help you with most of it.
I commented it out, but now it doesn't display anything at all like it used to. I am thoroughly confused. It just exits the program without displaying anything?
And I have no idea how to program with SDL, is there like a Hello World, instructions, hand book or something? I googled it a lot and the most I got was how to display an image but that was an example code. I have no clue how the media layer stuff works.
The for loop isn't the problem, the problem is that when you try to break, you're just breaking from the switch statement! You haven't asked the for loop to stop anywhere!

Perhaps this instead:

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
using namespace std;

#define ESC 27
#define UP 72
#define LEFT  75
#define RIGHT 77
#define DOWN  80
#define ENTER 13


void X(const char *prompt)
{
   puts(prompt);
   for (bool contin = true; contin; )
   {
	  int ch = _getch();
	  switch(ch)
	  {
	  case EOF: {}
	  case ESC: {return;}
	  case UP:	{ cout << "UP";  break; contin = false;}
	  case LEFT: { cout << "LEFT"; break; contin = false;}
	  case RIGHT: { cout << "RIGHT"; break; contin = false;}
	  case DOWN: { cout << "DOWN"; break; contin = false;}
	  //case ENTER: puts("ENTER"); system("pause"); clr(); break;
	  default:{	if(isprint(ch)) putchar(ch); break;}
	  }
   }
}
int main(void)
{
   X("\n\n\n\n\t\t\t\tContinue\n\n\t\tLoad Game\t\t\tNew Game\n\n\t\t\t\tOptions");
   return 0;
}


Forgive me, contin = false; may be needed elsewhere but you can figure that out whereas I can't figure out your program.

EDIT: Used continue instead of contin, forgot continue was a keyword!
Last edited on
@Veltas
When I put your code in I get these errors:
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
1
1>Compiling...
1>o.cpp
1>c:\documents and settings\45835\desktop\o\o\o.cpp(33) : warning C4091: '' : ignored on left of 'bool' when no variable is declared
1>c:\documents and settings\45835\desktop\o\o\o.cpp(33) : error C2059: syntax error : 'empty declaration'
1>c:\documents and settings\45835\desktop\o\o\o.cpp(33) : error C2143: syntax error : missing ';' before 'continue'
1>c:\documents and settings\45835\desktop\o\o\o.cpp(33) : error C2143: syntax error : missing ';' before 'continue'
1>c:\documents and settings\45835\desktop\o\o\o.cpp(33) : error C2143: syntax error : missing ')' before 'continue'
1>c:\documents and settings\45835\desktop\o\o\o.cpp(33) : error C2143: syntax error : missing ';' before '='
1>c:\documents and settings\45835\desktop\o\o\o.cpp(33) : error C2143: syntax error : missing ';' before '='
1>c:\documents and settings\45835\desktop\o\o\o.cpp(33) : error C2044: illegal continue
1>c:\documents and settings\45835\desktop\o\o\o.cpp(33) : error C2059: syntax error : ')'
1>c:\documents and settings\45835\desktop\o\o\o.cpp(40) : error C2143: syntax error : missing ';' before '='
1>c:\documents and settings\45835\desktop\o\o\o.cpp(40) : error C2044: illegal continue
1>c:\documents and settings\45835\desktop\o\o\o.cpp(40) : error C2143: syntax error : missing ';' before '='
1>c:\documents and settings\45835\desktop\o\o\o.cpp(41) : error C2143: syntax error : missing ';' before '='
1>c:\documents and settings\45835\desktop\o\o\o.cpp(41) : error C2044: illegal continue
1>c:\documents and settings\45835\desktop\o\o\o.cpp(41) : error C2143: syntax error : missing ';' before '='
1>c:\documents and settings\45835\desktop\o\o\o.cpp(42) : error C2143: syntax error : missing ';' before '='
1>c:\documents and settings\45835\desktop\o\o\o.cpp(42) : error C2044: illegal continue
1>c:\documents and settings\45835\desktop\o\o\o.cpp(42) : error C2143: syntax error : missing ';' before '='
1>c:\documents and settings\45835\desktop\o\o\o.cpp(43) : error C2143: syntax error : missing ';' before '='
1>c:\documents and settings\45835\desktop\o\o\o.cpp(43) : error C2044: illegal continue
1>c:\documents and settings\45835\desktop\o\o\o.cpp(43) : error C2143: syntax error : missing ';' before '='
1>Build log was saved at "file://c:\Documents and Settings\45835\Desktop\o\o\Debug\BuildLog.htm"
1>o - 20 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

EDIT: Disregard my errors I did not see you edited your post lol!
If I put my REAL code in, it might be confusing. To basically explain my crappy menu:
******* Option 1
Option 2 ****** Option 3
*******Option 4
The user is prompted to press an arrow key. If you press the UP arrow key, Option 1 will be selected and what I want is for after the key is selected it will exit the switch statement and you won't be able to use the arrow keys for the menu like that anymore.
Last edited on
Your switch is inside of a function, you could just return from it to exit the for loop.
Last edited on
Heres a look at my code if you think it's necessary:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

#include <iostream>
#include <io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <cstdlib>
#include <time.h>
#include <windows.h>
#include <fstream>
#include <string>
#include <tchar.h>
#include <windows.h>
#include <string>
#include "shlwapi.h"
#include <iomanip>
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

using namespace std;
#define GAME "C://GAME//"
#define DATA "DATA//"
#define CHARAC GAME DATA "CHARAC//"
#define MNSTR GAME DATA "MNSTR//"
#define SAVE GAME DATA "SAVES//"
#define CON1 SAVE GAME DATA "CON1//"
#define cx "continue[1].txt"
#define SAVEFAIL1 "The Saved Game File Does Not Exist"
#define ESC 27
#define UP 72
#define LEFT  75
#define RIGHT 77
#define DOWN  80
#define ENTER 13
void continu();
void load();
void newgame();
void opti();
void clr();
void install()
{
CreateDirectory(GAME, NULL);//Creates UserFolder
CreateDirectory(SAVE, NULL);
CreateDirectory(MNSTR, NULL);
CreateDirectory(CHARAC, NULL);
CreateDirectory(CON1, NULL);
}
void foo(const char *prompt, string menuA = "null")
{
   puts(prompt);
   //for ( ;; )
   {
	  int ch = _getch();
	  switch(ch)
	  {
	  case EOF: 
	  case ESC:   return;
	  case UP:	clr();  continu(); /*menuA = "con";*/ break;
	  case LEFT:  clr(); load();  /*menuA = "load";*/ break;
	  case RIGHT:  clr(); newgame(); /*menuA = "new";*/ break;
	  case DOWN:  clr(); opti(); /*menuA = "opti";*/ break;
	  //case ENTER: puts("ENTER"); system("pause"); clr(); break;
	  default:	if(isprint(ch)) putchar(ch); break;
	  }
	  /*if (menuA == "con")
	  {
continu();
   }
else if (menuA == "load")
   {
load();

   }
else if (menuA == "new")
   {

newgame();
   }
else if (menuA == "opti")
   {
opti();

   }
else
{

cout << "you suck.";
system("pause"); // Ugly
}*/
   }
}

int main(void)
{
	install();
   foo("\n\n\n\n\t\t\t\tContinue\n\n\t\tLoad Game\t\t\tNew Game\n\n\t\t\t\tOptions");
   return 0;
}
void clr()
 {
 HANDLE                     hStdOut;
 CONSOLE_SCREEN_BUFFER_INFO csbi;
 DWORD                      count;
 DWORD                      cellCount;
 COORD                      homeCoords = { 0, 0 };

 hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
 if (hStdOut == INVALID_HANDLE_VALUE) return;

 /* Get the number of cells in the current buffer */
 if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
 cellCount = csbi.dwSize.X *csbi.dwSize.Y;

 /* Fill the entire buffer with spaces */
 if (!FillConsoleOutputCharacter(
   hStdOut,
   (TCHAR) ' ',
   cellCount,
   homeCoords,
   &count
   )) return;

 /* Fill the entire buffer with the current colors and attributes */
 if (!FillConsoleOutputAttribute(
   hStdOut,
   csbi.wAttributes,
   cellCount,
   homeCoords,
   &count
   )) return;

 /* Move the cursor home */
 SetConsoleCursorPosition( hStdOut, homeCoords );
 }
void continu()
{
	const WORD colors[] =
		{
		0x1A, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F,//Note to self: Check what all these colors are...Aka what are all the color combos.
		0xA1, 0xB2, 0xC3, 0xD4, 0xE5, 0xF6
		};
	HANDLE hstdin  = GetStdHandle( STD_INPUT_HANDLE  );
	HANDLE hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
	WORD   index   = 0;

	CONSOLE_SCREEN_BUFFER_INFO csbi;
	GetConsoleScreenBufferInfo( hstdout, &csbi );
	/*/ofstream myfilex ("C://GAME//CON//continue[1].txt");
  if (myfilex.is_open())
  {
    myfilex << "Michael Quick LVL 22 2011APR20";
    myfilex.close();
  }
  else cout << "Unable to open file";*/
//The following checks to see if folder CON1 exists []
string strPath = CON1;
if (_access( strPath.c_str(), 0 ) == 0 )
 {
     struct stat status;
     stat( strPath.c_str(), &status );
          if ( status.st_mode && S_IFDIR )
          {
SetConsoleTextAttribute( hstdout, 0x3E);
			  cout << "$" << endl ;
			 
          }

          else
          {
			  SetConsoleTextAttribute( hstdout, 0x35);
			  cout << "$F" << endl;
			//sixteen = "false";
          }

 }

  else
   {
	   SetConsoleTextAttribute( hstdout, 0x35);
	   cout << "$0" << endl;
//sixteen = "false";
 }
//[]
	
	string contx;
	ifstream myfile (GAME cx);
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,contx);
      cout << "LOAD:" << contx << endl;
    }
    myfile.close();
  }
  else
  {
SetConsoleTextAttribute( hstdout, 0x35); 
cout << SAVEFAIL1;
  }
FlushConsoleInputBuffer( hstdin );
		SetConsoleTextAttribute( hstdout, csbi.wAttributes ); //reset colors
}
void load()
{
	cout << "load";
}
void newgame()
{
cout << "newgame";
}

void opti()
{
cout << "options";
}

I have a lot of saves so I don't know if this is the correct one that I want to show you guys. It's nothing special. Just jumbles of code together. Really ugly code. Other peoples sexy code, made ugly.
@Computergeek
I don't understand what you mean by return from it?..
@Veltas
It does the same thing as my old code.
I want to take a break and emphisis using SDL one more time. It's a lot to take in, but you'll be better off for everything you learn here. This guy has some pretty good coding pratices: http://www.sdltutorials.com/sdl-tutorial-basics/
My only complant is that he doesn't include any error useful error checking so you may get stuck for a long time on some thing dumb, but don't get discouraged.
Thank you Computergeek, I will attempt at SDL again. I do most of my programming at school and that website is blocked here. I don't know if I've been there yet or not.. Once I go home, maybe tomorrow, to get my laptop I can REALLY program with SDL. I am unable to look at the website right now. I have just been trying to solve this code even though it is being stupid and by stupid I mean too smart for me. I will push the SDL later, more than likely I will be C++ pseudo coding on my ipod in a matter of hours.
If it is blocked, try pinging the website via cmd prompt, and then paste the ip that gives you the reply into your web browser. ;p

Of course, it won't work on most websites
Last edited on
They already put me on watch for getting one of the computers to only play Carmen Sandiego in spanish on start, and being able to reverse it. Not planning on getting kicked out right before graduation though a valid suggestion haha.
Solved my answer;
I was using break "correctly" but it wasn't what I was looking for I guess,
I actually wanted to use return apparently and didn't realize it.
Thanks for all the help.
Topic archived. No new replies allowed.