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.
#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';
}
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.
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.
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 ==========
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?
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.