comma and decimals

I have made a math game. Works fine mostly but there are 2 things that I cant make work as I want in the section where the user guessing division.

1. make the division only have 2 decimals, have tried whit set precision, it sort of work but It make it so the answer is shown for the user, and that is not really what I want..

2.change so the decimal is a comma instead of a dot, because where im from decimals are shown i comma.

here are the part in the code where the problem is.

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
case '3':
			srand(time(NULL));
			tal1 = rand() % 30 + 10;
			tal2 = rand() % 8 + 1;
			tal3 = tal1 / tal2;
			
			
			do
			{

			
			


			std::cout << " hur mycket är " << tal1 << "/" << tal2 << endl;
			cin >>  tal3;
			

			bfail = cin.fail();
			cin.clear();
			cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail == true);




			if (tal1 / tal2 == tal3)
			{
				std::cout << " rätt!!" << endl;
				ratt++;
				antal++;
				break;
			}

			else if (tal1 / tal2 != tal3)

			{

				std::cout << "Fel!" << endl;
				fel++;
				antal++;

				break;
			}

 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <cmath>
#include <iostream>
#include <locale>

//Helper class to replace decimal separator
template <class charT, charT sep>
class punct_facet: public std::numpunct<charT> {
protected:
    virtual charT do_decimal_point() const override { return sep; }
};

int main()
{
    double d = 3.14159265358;

    //Round to 2 decimals (multiply by 100, round, divide by 100):
    d = std::round(d*100) / 100.;

    //Make stream use comma for decimal separator
    std::cout.imbue(std::locale(std::cout.getloc(), new punct_facet<char, ','>));
    std::cout << d << '\n';
}
3,14
http://coliru.stacked-crooked.com/a/e382e3bd082f2486
Alternatively you could just imbue locale for your language, this will help with other stuff (like date and time format), but it is not as simple to make work on all compilers. Solution for Visual Studio:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <locale>

int main()
{
    std::cout.sync_with_stdio(false);

    double d = 3.14159265358;

    //Round to 2 decimals (multiply by 100, round, divide by 100):
    d = std::round(d*100) / 100.;

    //Imbue stream with sweden locale
    std::cout.imbue(std::locale("se-SE"));
    std::cout << d << '\n';
}
http://rextester.com/TJP56653

EDIT: made code to use round() for correct rounding
EDIT 2: Added proper example using platform-dependend locale names
Last edited on
hmmm .. I have still ni ide how to implement that ?

I can not do

double d = 3.14159265358;


cause then I print out things . I want the user to print out what the 2 values tal1 and tal2 becomes..

tried this but that didnt do the trick either


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
			srand(time(NULL));
			tal1 = rand() % 30 + 10;
			tal2 = rand() % 8 + 1;
			tal3 = tal1 / tal2;
			
			
			do
			{
				
				std::cout.sync_with_stdio(false);
				
				//Round to 2 decimals (multiply by 100, round, divide by 100):
				tal3 = std::round(tal3 * 100) / 100.;

				//Imbue stream with sweden locale
				std::cout.imbue(std::locale("se-SE"));
				std::cout << tal3 << '\n';
				
			
			


			
			cin >>  tal3;

		
			

			bfail = cin.fail();
			cin.clear();
			cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail == true);




			if (tal1 / tal2 == tal3)
			{
				std::cout << " rätt!!" << endl;
				ratt++;
				antal++;
				break;
			}

			else if (tal1 / tal2 != tal3)

			{

				std::cout << "Fel!" << endl;
				fel++;
				antal++;

				break;
			}


and I think its wrong doing

std::cout << tal3 << '\n';

because then its printing out the answer before the user has answered..
I can not do

double d = 3.14159265358;
This value is for example. You can use any other value.
You can even apply it to result of operations:
1
2
if( std::round(tal1/tal2*100) / 100.)
   //... 
However I would suggest to save result in variable, just for clarity:
1
2
3
double result = std::round(tal1/tal2*100) / 100.;
if(result == tal3)
   //... 


Also:
1
2
std::cout.sync_with_stdio(false);
std::cout.imbue(std::locale("se-SE"));
Should be first lines in entire application (that it, first two lines in main()) and never ever called again.

If you want to allow user to input comma as decimal separator, you will need to apply locale to input stream too: std::cin.imbue(std::locale("se-SE"));
Note that it is the solution for Visual Studio (and probably clang on Windows)!

and I think its wrong doing

std::cout << tal3 << '\n';
Well, that is what you doing. Remove this line if you do not want that.
so i made the decimal work.. and I could do

1
2
3
4
5
locale swedish("swedish");
	locale::global(swedish);

std::cin.imbue(std::locale("se-SE"));


or as you printed .

but whit decimal I have some problem. If I do

std::cout << tal3 << '\n';

it show whit the right decimals..


but if I want the user to write the same decimals it says that it is the wrong number

1
2
3
4
5
6
 tal3 = (long long)(tal3 * 100) / 100.;
		
			do
			{
				
				cin >> tal3;



the strange part is if I do like

1
2
3
4
5
6
 tal3 = (long long)(tal3 * 100) / 100.;
		
			do
			{
				std::cout << tal3 << '\n';
				cin >> tal3;



It prints out whit 2 decimals and when the user prints out the same the answer still is wrong?
Show complete code. Especially part where you are comparing input value to actual result.
Everything should work: http://rextester.com/IHJ77884
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295

#include "stdafx.h"
#include <iostream>
#include <cstdlib>						// random
#include <time.h>
#include <iomanip>
#include <conio.h>						// so I can use _getch
#include <cmath>

using namespace std;



int main()
{
	
	
	locale swedish("swedish");
	locale::global(swedish);
	
	
	
	std::cin.imbue(std::locale("se-SE"));
	
	
	bool bfail;
	char x = 0;
	int ratt = 0;
	double tal1, tal2;
	double tal3;
	int fel = 0;
	int antal = 0;
	
	

	


	

	
	
	
	std::cout << " gissa talen och se hur duktig du är " << endl;
	std::cout << endl;
	std::cout << endl;


	while (x !=1000000)
	{

		antal = 0;

	std::cout << endl;
	std::cout << " Välj mellan" << endl;
	std::cout << "=================================" << endl;
	std::cout << "[1]  Addition" << endl;
	std::cout << "[2]  subtraktion" << endl;
	std::cout << "[3]  division" << endl;
	std::cout << "[4]  multiplikation " << endl;
	std::cout << "[A]  Avslutar spelet" << endl;
	char x = _getch();
	
	
	


	while (antal < 5)
	{


		switch (x)
		{



		case'1':

			// make random numbers
			srand(time(NULL));
			tal1 = rand() % 20 + 1;
			tal2 = rand() % 20 + 1;										
			tal3 = tal1 + tal2;
			
			
			do
			{
			std::cout << " hur mycket är " << tal1 << "+" << tal2 << endl;
			std::cin >> tal3;
			
			
			bfail = cin.fail();
			cin.clear();
			cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail== true);

				if (tal1 + tal2 == tal3)
				{
					std::cout << " rätt!!" << endl;
					ratt++;
					antal++;
					break;
				}

				else if (tal3 != tal1 + tal2)

				{

					std::cout << "Fel!" << endl;
					fel++;
					antal++;

					break;
				}

			

		case '2':
			srand(time(NULL));
			tal1 = rand() % 30 + 10;
			tal2 = rand() % 10 + 1;
			tal3 = tal1 - tal2;
			
			
			do
			{

			
			std::cout << " hur mycket är " << tal1 << "-" << tal2 << endl;
			std::cin >> tal3;

			bfail = cin.fail();
			cin.clear();
			cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail == true);



			if (tal1 - tal2 == tal3 )
			{
				std::cout << " rätt!!" << endl;
				ratt++;
				antal++;
				break;
			}

			else if (tal3 != tal1 - tal2)

			{

				std::cout << "Fel!" << endl;
				fel++;
				antal++;

				break;
			}

		case '3':
			
			
			
			srand(time(NULL));
			tal1 = rand() % 30 + 10;
			tal2 = rand() % 8 + 1;
			tal3 = tal1 / tal2;
			
			
			std::cout << " hur mycket är " << tal1 << "/" << tal2 << endl;
			
			
			
			
		
			do
			{
				
				
					std::cin >>  tal3;

					bfail = cin.fail();
					cin.clear();
					cin.ignore(numeric_limits < streamsize>::max(), '\n');



				} while (bfail == true);



					if (tal1 / tal2 == tal3)
				
				{
					
				
				
					std::cout << " rätt!!" << endl;
					ratt++;
					antal++;
					break;
				}

				else
				{

				

					std::cout << "Fel!" << endl;
					fel++;
					antal++;

					break;
				}
			

		case '4':
			srand(time(NULL));
			tal1 = rand() % 20 + 1;
			tal2 = rand() % 20 + 1;
			tal3 = tal1 * tal2;

			do
			{

			

			std::cout << " hur mycket är " << tal1 << "x" << tal2 << endl;
			std::cin >> tal3;

			bfail = cin.fail();
			cin.clear();
			cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail == true);




			if (tal1 * tal2 == tal3)
			{
				std::cout << " rätt!!" << endl;
				ratt++;
				antal++;
				break;
			}

			else if (tal3 != tal1 * tal2)

			{

				std::cout << "Fel!" << endl;
				fel++;
				antal++;

				break;
			}
		case 'a':
		case 'A':
			std::cout << " Ok avslutar spelet" << endl;
			return 0;
			break;
		
		default:
			std::cout << " Inte ett giltigt val! testa igen" << endl;
			antal = 9;
			break;


		}
		
	}
	
	
	if (antal == 5)
	{
		std::cout << "Du fick " << ratt << " rätt och " << fel << " fel" << endl;
		std::cout << " bra jobbat!!" << endl;
		
		antal = 0;
		ratt = 0;
		fel = 0;
	}
	
	}

	

}





if I change to

1
2
3
4
std::cout.sync_with_stdio(false);
	std::locale sweden("se-SE");
	std::cout.imbue(sweden);
	std::cin.imbue(sweden);


and

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
	case '3':
			
			
			
			srand(time(NULL));
			tal1 = rand() % 30 + 10;
			tal2 = rand() % 8 + 1;
			tal3 = tal1 / tal2;
			double result = std::round(tal1 / tal2 * 100) / 100.;
			
			std::cout << " hur mycket är " << tal1 << "/" << tal2 << endl;
			
			
			
			
		
			do
			{
				
				
					std::cin >>  tal3;

					bfail = cin.fail();
					cin.clear();
					cin.ignore(numeric_limits < streamsize>::max(), '\n');



				} while (bfail == true);



					if (tal3 == result)
				
				{
					
				
				
					std::cout << " rätt!!" << endl;
					ratt++;
					antal++;
					break;
				}

				else
				{

				

					std::cout << "Fel!" << endl;
					fel++;
					antal++;

					break;
				}


then the program dont work at all?

but if i instead do tal3 = (long long)(tal3 * 100) / 100.;

works same way it did before whit right decimals but not if the user prints it out
then the program dont work at all?
Did it give you an error? You need to #include <cmath> for round.

Show what is shown to you, what are you trying to input and what reaction is.
Did you try a minimal example in my previous post? If it works (accepts 1,88 as input) then you might have some other problem. If it does not, then your compiler might not implement correct locale support yet.

By the way what you are using? Is it VS2015?
I use vs 2013 comunity

I have cmath

i tried to do
1
2
3
4
5
6
7
8
int main()
{
	
	std::cout.sync_with_stdio(false);
	std::locale sweden("se-SE");
	std::cout.imbue(sweden);
	std::cin.imbue(sweden);
	


and

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
srand(time(NULL));
			tal1 = 15; //rand() % 30 + 10;
			tal2 = 8; //rand() % 8 + 1;
			double result = std::round(tal1 / tal2 * 100) / 100.;

		
			std::cout << " hur mycket är " << tal1 << "/" << tal2 << endl;





			do
			{


				std::cin >> tal3;

				bfail = cin.fail();
				cin.clear();
				cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail == true);



			if (tal3 == result)
			{
				std::cout << "Answer is really " << tal3 << "!\n";
			}
			else
			{
				std::cout << "Wrong!\n";

				break;
			}
	


like you do and then I get building errors..


c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(84): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(126): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(172): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(213): error C2360: initialization of 'result' is skipped by 'case' label
1> c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(175) : see declaration of 'result'
1>c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(214): warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(256): error C2360: initialization of 'result' is skipped by 'case' label
1> c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(175) : see declaration of 'result'
1>c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(257): error C2360: initialization of 'result' is skipped by 'case' label
1> c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(175) : see declaration of 'result'
1>c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(262): error C2361: initialization of 'result' is skipped by 'default' label
1> c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(175) : see declaration of 'result'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1>c:\users\snok\documents\visual studio 2013\projects\matte\matte\matte.cpp(213): error C2360: initialization of 'result' is skipped by 'case' label
This is exactly what its says. You cannot just jump somewhere where you can access variable but after its initialization

Solution is to surround your cases with brases:
1
2
3
4
5
6
7
8
9
10
11
switch(c) {
  case 1: { //← Those braces
    //Do something
    break;
  } //←
  case 2: {
    //...
    break;
  }
  //...
}
Now it works! but got another thing instead. case 1,2,4 and aA works . case 3 to but it jumps from case 3 to case4 and then case3 every other . so first division then jumps to multiplication , then division and so on. But only if I guess right, if I guess wrong it still on division(case 3).. must be something in the if statement i guess?

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
case'1':{

			
			srand(time(NULL));
			tal1 = rand() % 20 + 1;
			tal2 = rand() % 20 + 1;
			tal3 = tal1 + tal2;


			do
			{
				std::cout << " hur mycket är " << tal1 << "+" << tal2 << endl;
				std::cin >> tal3;


				bfail = cin.fail();
				cin.clear();
				cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail == true);

			if (tal1 + tal2 == tal3)
			{
				std::cout << " rätt!!" << endl;
				ratt++;
				antal++;
				break;
			}

			else if (tal3 != tal1 + tal2)

			{

				std::cout << "Fel!" << endl;
				fel++;
				antal++;

				break;
			}

		}

		case '2':{
			srand(time(NULL));
			tal1 = rand() % 30 + 10;
			tal2 = rand() % 10 + 1;
			tal3 = tal1 - tal2;


			do
			{


				std::cout << " hur mycket är " << tal1 << "-" << tal2 << endl;
				std::cin >> tal3;

				bfail = cin.fail();
				cin.clear();
				cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail == true);



			if (tal1 - tal2 == tal3)
			{
				std::cout << " rätt!!" << endl;
				ratt++;
				antal++;
				break;
			}

			else if (tal3 != tal1 - tal2)

			{

				std::cout << "Fel!" << endl;
				fel++;
				antal++;

				break;
			}
		}
		case '3':{



			srand(time(NULL));
			tal1 = rand() % 30 + 10;
			tal2 = rand() % 8 + 1;
			double result = std::round(tal1 / tal2 * 100) / 100.;

			tal3 = tal1 / tal2;

			




			do
			{


				std::cout << " hur mycket är " << tal1 << "/" << tal2 << endl;
				std::cout << "Om det behövs avrunda till max 2 decimaler" << endl;

				
				std::cin >> tal3;

				bfail = cin.fail();
				cin.clear();
				cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail == true);



			if (tal3 == result)
			{
				std::cout << "Rätt!!" << endl;
				ratt++;
				antal++;
			}
			else
			{
				std::cout << "Fel!!" << endl;

				fel++;
				antal++;
				break;
			}


		}
		
		case '4':{
			srand(time(NULL));
			tal1 = rand() % 20 + 1;
			tal2 = rand() % 20 + 1;
			tal3 = tal1 * tal2;

			do
			{



				std::cout << " hur mycket är " << tal1 << "x" << tal2 << endl;
				std::cin >> tal3;

				bfail = cin.fail();
				cin.clear();
				cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail == true);




			if (tal1 * tal2 == tal3)
			{
				std::cout << " rätt!!" << endl;
				ratt++;
				antal++;
				break;
			}

			else if (tal3 != tal1 * tal2)

			{

				std::cout << "Fel!" << endl;
				fel++;
				antal++;

				break;
			}
		}
		case 'a':{
		case 'A':
			std::cout << " Ok avslutar spelet" << endl;
			return 0;
			break;
		}
		default:{
			std::cout << " Inte ett giltigt val! testa igen" << endl;
			antal = 9;
			break;
		}

		}
		
	}
YOu are missing a break in your third case.
Actually it is not a good idea to have duplicated code. Why do you have breaks in each branch of if statement? Just place one after it, and there will be no chance that you accidentally miss it in one of the branches.
hehe I dont know... thank you! that was stupid that i did that..


now everything works ..

I took away the dubbel breaks and put it like this in all cases in stead
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
case '3':{



			srand(time(NULL));
			tal1 = rand() % 30 + 10;
			tal2 = rand() % 8 + 1;
			double result = std::round(tal1 / tal2 * 100) / 100.;

			tal3 = tal1 / tal2;

			




			do
			{


				std::cout << " hur mycket är " << tal1 << "/" << tal2 << endl;
				std::cout << "Om det behövs avrunda till max 2 decimaler" << endl;

				
				std::cin >> tal3;

				bfail = cin.fail();
				cin.clear();
				cin.ignore(numeric_limits < streamsize>::max(), '\n');



			} while (bfail == true);



			if (tal3 == result)
			{
				std::cout << "Rätt!!" << endl;
				ratt++;
				antal++;
			}
			else
			{
				std::cout << "Fel!!" << endl;

				fel++;
				antal++;
				
			}
			break;

		}

cant thank you enough for your patince whit me !

hope you have a nice week and thanks again!!













Topic archived. No new replies allowed.