Need help!!

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
void kbc:: ask()
{
char ch201= 201;
char ch200= 200;
char ch187= 187;
char ch186= 186;
char ch188= 188;
char ch205= 205;
int c;
do
{clrscr();
 gotoxy(11,3);
 cputs(" W E L C O M E  T O  K A U N  B A N E G A  C R O R E P A T I \n");
 for(int g=6;g<=8;g++)
 {gotoxy(30,g);
 cout<<ch186<<endl;
 }
 textcolor(LIGHTGRAY);
 gotoxy(30,6);
 cout<<ch201<<endl;
 for(int x=31;x<=44;x++)
 {textcolor(LIGHTGRAY);
 gotoxy(x,6);
 cout<<ch205<<endl;
 }textcolor(LIGHTGRAY);
 gotoxy(45,6);
 cout<<ch187<<endl;
 for(int h=7;h<=8;h++)
 {textcolor(LIGHTGRAY);
  gotoxy(45,h);
 cout<<ch186<<endl;
 }
 textcolor(LIGHTGRAY);
 gotoxy(45,8);
 cout<<ch188<<endl;
 textcolor(LIGHTGRAY);
 gotoxy(30,8);
 cout<<ch200<<endl;
 for(int i=31;i<=44;i++)
 {textcolor(LIGHTGRAY);
 gotoxy(i,8);
 cout<<ch205<<endl;
 }
 textcolor(LIGHTGRAY);
 gotoxy(31,7);
 cputs(" 1. Play \n"); 
 for(int g1=10;g1<=12;g1++)      
 {
 textcolor(LIGHTGRAY);
 gotoxy(30,g1);
 cout<<ch186<<endl;
 }
 textcolor(LIGHTGRAY);
 gotoxy(30,10);
 cout<<ch201<<endl;
 for(int x1=31;x1<=45;x1++)
 {gotoxy(x1,10);
 cout<<ch205<<endl;
 }
 gotoxy(45,10);
 cout<<ch187<<endl;
 for(int h1=11;h1<=12;h1++)
 {gotoxy(45,h1);
 cout<<ch186<<endl;
 }
 gotoxy(45,12);
 cout<<ch188<<endl;
 gotoxy(30,12);
 cout<<ch200<<endl;
 for(int i1=31;i1<=44;i1++)
 {gotoxy(i1,12);
 cout<<ch205<<endl;
 }
 gotoxy(31,11);
 cputs(" 2. Score \n"); //guide ovr
 for(int g2=14;g2<=16;g2++)      // score strt
 {gotoxy(30,g2);
 cout<<ch186<<endl;
 }
 gotoxy(30,14);
 cout<<ch201<<endl;
 for(int x2=31;x2<=45;x2++)
 {gotoxy(x2,14);
 cout<<ch205<<endl;
 }
 gotoxy(45,14);
 cout<<ch187<<endl;
 for(int h2=15;h2<=16;h2++)
 {gotoxy(45,h2);
 cout<<ch186<<endl;
 }
 gotoxy(45,16);
 cout<<ch188<<endl;
 gotoxy(30,16);
 cout<<ch200<<endl;
 for(int i2=31;i2<=44;i2++)
 {gotoxy(i2,16);
 cout<<ch205<<endl;
 }
 gotoxy(31,15);
 cputs(" 3. Exit \n");
 gotoxy(25,20);
 textcolor(YELLOW);
 cputs("Press the number key to continue         \n");
 textcolor(YELLOW);
 gotoxy(58,20);
 cin>>c;
 switch (c)
 {case 1: y.loading(); break;
  case 2: sce(); break;
  case 3: clrscr();
    	 gotoxy(20,10);
	 cputs("Do you really want to exit the game y / n      \n");
	 gotoxy(40,12);
	 cin>>ch;
	 if(ch == 'y' || ch == 'Y')
	 {exit(0);}
	 else
	 {ask();} break;
 default: gotoxy(27,22);
	 cputs("Please press correct key \n"); getch(); break; 	  
}
}while(c!=1 && c!=2 && c!=3);  
}


Above is my function which display menu and ask user to input numbers.
i wrote something if wrong input is given and asks user again.

but this is not working properly if i input any alphabets except numbers.
please help me fast i need to submit my project

Thank you
Hi there,

Try this:

1
2
3
4
5
6
7
8
9
10
11
12
cputs("Press the number key to continue         \n");
 textcolor(YELLOW);
 gotoxy(58,20);
//this part is modified:
 if( !(cin>>c) ) //if cin fails
 {
    cin.clear();  //clear cin errorstate
    cin.ignore(200, '\n');  //ignore the inputed character
    c = 5;  //set c to an integer value that will make the loop re-occur
 }
//end modified section
 switch (c)


All the best,
NwN
Last edited on
@smile58

It looks like most of the routine is used to create a box on screen. Try my function and see if it is quicker, and easier to add new boxes and text, in whatever colors and location, you need. Specify shadows also.
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
// Boxes.cpp : main project file.

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;
  
void Box(int style, int across, int down, int amount, int rows, int b_color, int f_color, int shadow, int shadow_bc, int shadow_fc);
void gotoXY(int x, int y);
void gotoXY(int x, int y, int);
void gotoXY(int x, int y, string);
void gotoXY(int x, int y, string, int, string);
void gotoXY(int x, int y, string, int, string, int c, string a2);

char Shadow[5] = { ' ','\xB0','\xB1','\xB2',' ' };
char  Style[5][11] = { 
        {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ' },
        {' ','\xDA','\xC4','\xBF','\xB3','\xC0','\xD9','\xC3','\xB4','\xC2','\xC1'},
        {' ','\xC9','\xCD','\xBB','\xBA','\xC8','\xBC','\xCC','\xB9','\xCB','\xCA'},
        {' ','\xD5','\xCD','\xB8','\xB3','\xD4','\xBE','\xC6','\xB5','\xD1','\xCF'},
        {' ','\xD6','\xC4','\xB7','\xBA','\xD3','\xBD','\xC7','\xB6','\xD2','\xD0'}
};

enum {
  black,          //  0
  dark_blue,      //  1
  dark_green,     //  2
  dark_cyan,      //  3
  dark_red,       //  4
  dark_magenta,   //  5
  dark_yellow,    //  6
  light_gray,     //  7
  dark_gray,      //  8
  light_blue,     //  9
  light_green,    // 10
  light_cyan,     // 11
  light_red,      // 12
  light_magenta,  // 13
  light_yellow,   // 14
  white           // 15
  };

int main()
{
	Box(2,0,0,80,25,light_cyan,black,0,0,0);
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),176 );
	gotoXY(11,3,"W E L C O M E  T O  K A U N  B A N E G A  C R O R E P A T I");
	Box(1,14,7,50,9,light_gray,dark_gray,2,light_cyan,dark_cyan);
	gotoXY(21,9,"1. Play....");
	gotoXY(21,11,"2. Score...");
	gotoXY(21,13,"3. Exit....");
	
	Box(4,20,20,35,3,dark_blue,light_yellow,2,light_cyan,dark_cyan);
	gotoXY(21,21);
}

void Box(int style, int across, int down, int amount, int rows, int b_color, int f_color, int shadow, int shadow_bc, int shadow_fc)
{
int color = (f_color+(b_color*16));
int shadow_color = (shadow_fc+(shadow_bc*16));
int x;
string BoxLine(amount-2, Style[style][2]);
string BoxBody(amount-2, ' ');
string ShadowLine(amount, Shadow[shadow]);
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color );

gotoXY(across,down);
    cout << Style[style][1] << BoxLine << Style[style][3];
for (x=1; x<rows; x++)
{
    gotoXY(across,down+x);
    cout << Style[style][4] << BoxBody << Style[style][4];
     if (shadow>0)
     {
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),shadow_color);
		cout << Shadow[shadow];
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color );
     }
    
}
gotoXY(across,down+rows-1);
   cout << Style[style][5] << BoxLine <<  Style[style][6];
   if (shadow>0)
     {
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),shadow_color);
		cout << Shadow[shadow];
		gotoXY(across+1,down+rows);
		cout << ShadowLine;
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color );
	}
}

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;
}
Last edited on
Is there any way to increase font size in c++ (which are displayed on screen).
@smile58

Using console, I don't believe so. You're pretty much stuck with the default. If there is, I haven't seen anyone use it. Sorry..
Topic archived. No new replies allowed.