Switch statement help needed

I thought of this as a simple project to help my understanding of C++ better. I wish to let the console loop the output so I can effectively keep entering w, a, s and d and the console will keep outputting Up, Down, Left, and Right. Also if possible without having to hit the return key after each letter? Thanks.

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
#include <conio.h>
#include <iostream>

int main(int argc, char *argv[])
{
    char x;
    
    printf("-CONTROL IDEA-\n* W=Up    *\n* A=Left  *\n* S=Down  *\n* D=Right *\n\n");
    scanf("%c", &x);
    
        
    switch (x)
    {
           case 'w':
               printf("Up");
               break;
               
           case 'a':
               printf("Left");
               break;
               
           case 's':
               printf("Down");
               break;
               
           case 'd':
               printf("Right");
               break;
    }   
    
    getch();
    return EXIT_SUCCESS;
}
There is no way to do what you want in C++. You will have to use Platform-specific code or use a library.

To loop it, however, you may simple wrap lines 9-29 in a loop.
do you know why u are using print and scan over cout and cin?
@L B Ok thanks!
@ui uiho Is there a difference? Is one more efficient? Do explain I'm all ears.
std::cin is the standard C++ input stream object, and std::cout, std::clog, and std::cerr are the standard C++ output, logging, and error streams, respectively. They are far easier to use than their C counterparts. For example:
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
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

int main()
{
    cout << "Enter the number of pennies you have: ";
    int nPennies;
    while(!(cin >> nPennies) || nPennies < 0)
    {
        cin.clear();
        cin.sync();
        cout << "Try again: ";
    }
    int *years = new int[nPennies];
    int *dirtyFactors = new int[nPennies];
    for(int p = 0; p < nPennies; ++p)
    {
        cout << "Enter the year and dirty factor for penny " << p+1 << ": ";
        cin >> years[p] >> dirtyFactors[p]; //note: not safeguarded for bad input
    }
    cout << endl << "You have " << nPennies << " pennies, each as follows:" << endl;
    for(int p = 0; p < nPennies; ++p)
    {
        cout << "Penny #" << p+1 << " was made in " << years[p] << " and has a dirty factor of " << dirtyFactors[p] << endl;
    }
    delete[] years;
    delete[] dirtyFactors;
}
what L B said. printf is faster but cannot be easily overloaded. std::cout can be easily overloaded and is alot more flexible.
@robkavanagh
Here's a small program I wrote that lets you use the arrow keys to move an X around. Adapt it to your needs.
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
// Using ARROW keys.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"  // Used with MS Visual C++. Remove, if not needed
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

void Draw(int, int, int, int, int , int  ); // Create a graphic box on screen
void gotoXY(int, int); // Place on screen in position you choose
void gotoXY(int, int, string); // Place on screen , text, at location you choose

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

int main(void)
{
	int x,y,i;
	x=20;
	y=10;
	char yn = 'y';
	int wins = false;
	gotoXY(10,6,"To move, use the arrow keys.");
	gotoXY(10,7,"'Enter' to change 'X' to 'o'");
	Draw(1,1,1,78,23,0);
	Draw(2,15,9,15,12,0);
	gotoXY(22,14,"H");
	do
	{
		if (x==22 && y==14)
		{ 
			gotoXY(19,12,"Yippee!!");
			gotoXY(18,13,"I'm HOME!!");
			for (i=0;i<8;i++)
			{
				gotoXY(x,y,"\\");
			Sleep(140);
				gotoXY(x,y,"/");
			Sleep(140);
			}
			wins = true;
		}
		gotoXY(10,24);
		cout << "The 'X' is at column " << x-15 << " row " << y-9 << " "; 
		gotoXY(x,y,"X");

		//Movement by pressing the arrow keys
		if(GetKeyState(VK_LEFT)<0)
		{
			if (x-1>15)
			{
				gotoXY(x,y," ");
				x--;					
			}
			else
			{
				gotoXY(x-6,y,"Ouch!");
				Sleep(500);
				gotoXY(x-6,y,"     ");
			}
		}
		if(GetKeyState(VK_RIGHT)<0)
		{
			if (x+1< 29)
			{
				gotoXY(x,y," ");	
				x++;
			}
			else
			{
				gotoXY(x+2,y,"Ouch!");
				Sleep(500);
				gotoXY(x+2,y,"     ");
			}				
		}
		if(GetKeyState(VK_UP)<0)
		{
			if (y-1>9)
			{
				gotoXY(x,y," ");	
				y--;
			}
			else
			{
				gotoXY(x-2,y-2,"Ouch!");
				Sleep(500);
				gotoXY(x-2,y-2,"     ");
			}
		}
		if(GetKeyState(VK_DOWN)<0)
		{
			if (y+1<20)
			{
				gotoXY(x,y," ");	
				y++;
			}
			else
			{
				gotoXY(x-2,y+2,"Ouch!");
				Sleep(500);
				gotoXY(x-2,y+2,"     ");
			}
		}
		if(GetKeyState('\x0D')<0)
		{
			gotoXY(x,y,"o");
			Sleep(800);
			gotoXY(x,y,"X");
		}

		Sleep(100);
	}while ( !wins );
	gotoXY(10,22);
}

void Draw(int style, int col, int row, int length,int height, int shadow )
{
	// Draws a 1 or 2 line box 
	int a;
	style--;
	//style=b*6;
	char box[2][6];
	box[0][0] = '\xDA';
	box[0][1] = '\xBF';
	box[0][2] = '\xC0';
	box[0][3] = '\xD9';
	box[0][4] = '\xB3';
	box[0][5] = '\xC4';
	box[1][0] = '\xC9';
	box[1][1] = '\xBB';
	box[1][2] = '\xC8';
	box[1][3] = '\xBC';
	box[1][4] = '\xBA'; 
	box[1][5] = '\xCD';
	char tl,tr,bl,br,side,edge;
	tl = box[style][0];
	tr = box[style][1];
	bl = box[style][2];
	br = box[style][3];
	side = box[style][4];
	edge = box[style][5];

	string Line(length-2,edge);
	string Shadow(length,'\xB0');
	gotoXY(col,row);
	cout << tl << Line << tr;
	for (a = 1; a <height-1;a++)
	{
		gotoXY(col,row+a);
		cout << side;
		gotoXY(col+length-1,row+a);
		cout << side;
		if (shadow)
			cout << "\xB0";
	}
	gotoXY(col,(height+row)-1);
	cout << bl << Line << br;
	if (shadow)
	{
		cout << "\xB0";
		gotoXY(col+1,row+height , Shadow );
	}
}

void gotoXY(int x, int y) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}

void gotoXY(int x, int y, string text) 
{ 

	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition);
	cout << text;
}
Thanks for all the help, I'll try get my head around all of this!
@ui uiho In what context do you mean by overloaded?
you can output function return values by referencing the function and do calculations within the statements. for example we have a function that outputs a number called mynumber() we output it like this
std::cout << mynumber();
you can do that same with printf but it gets more complicated. you can also do functions. lets say we have integer x. x = 5. we want to output x divided by 2 as a double. we could make a double variable or we can just do this
std::cout << x/2.0;
you can do similar things with printf but again, it gets more complicated.
Last edited on
ui uiho, both things can be done with the same effort for both cout and printf.
1
2
3
std::cout << GetInteger() << std::endl;
//is
printf("%d\n", GetInteger());
1
2
3
std::cout << SomeInt/2 << std::endl;
//is
printf("%d\n", SomeInt/2);


The real power with the stream objects comes not from not having to remember the syntax of the format string, but rather the ability to overload them so they may be used exactly the same way on non-primitive types:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Person
{
    unsigned bmonth, bday, byear;
    std::string nfirst, nmiddle, nlast; //or char arrays for C
public:
    //...cosntructors...

    //...member functions...

    friend std::ostream &operator<<(std::ostream &out, const Person &p)
    {
        out << p.nfirst+' '+p.nmiddle+' '+p.nlast << ", born " << p.bmonth<<'-'<<p.bday<<'-'<<p.byear;
    }
};
1
2
3
4
5
Person p (/**/);
//...
std::cout << p << std::endl;
//Above: C++ way, Below: C way
printf("%s %s %s, born %d-%d-%d", p.FirstName(), p.MiddleName(), p.LastName(), p.BirthMonth(), p.BirthDay(), p.BirthYear());


You could make a function to call printf with a Person object, but it would not be very pretty because of the way the stream object syntax works.
Last edited on
tried to keep it simple but yeh, that is what i was going for.
Topic archived. No new replies allowed.