Shortening and facilitating the creation of programs
Mar 31, 2020 at 11:56pm UTC
I made a program that helps me learn german.I speak just serbian,that you should not confuse about text in quotes,and not so good english.Is there any way to replace "a, a1, a2, a3" with something constant or at least variable and save time?
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
string answer;
a1:cout<<"isto-" ;
cin>>answer;
if (answer=="gleiche" ){
cout<<"Tacno!" ;
goto a2;
}else {
cout<<"Netacno!" ;
goto a1;
}
a2:cout<<"pronaci-" ;
cin>>answer;
if (answer=="finden" ){
cout<<"Tacno!" ;
goto a3;
}else {
cout<<"Netacno!" ;
goto a2;
}
a3:cout<<"kupaci-" ;
cin>>answer;
if (answer=="badeanzug" ){
cout<<"Tacno!" ;
goto a4;
}else {
cout<<"Netacno!" ;
goto a3;
}
Apr 1, 2020 at 12:09am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#include <map>
#include <string>
std::map<std::string, std::string> questions_answers = {
{"isto-" , "gleiche" },
{"pronaci-" , "finden" },
{"kupaci-" , "badeanzug" },
};
int score = 0;
for (auto &kv : questions_answers){
auto &question = kv.first;
auto &answer = kv.first;
std::cout << question;
std::string user_input;
std::getline(std::cin, user_input);
if (user_input == answer){
std::cout << "Tacno!\n" ;
score++;
}else
std::cout << "Netacno!\n" ;
}
std::cout << "Postici: " << score << std::endl;
Apr 3, 2020 at 12:01am UTC
Thank you very much!!!
Apr 3, 2020 at 11:36am UTC
Note that I made a mistake in my previous post. On line 13 it should read
auto &answer = kv.second;
Also, come to think of it an std::map was not the most ideal data structure for this problem.
Apr 4, 2020 at 11:27pm UTC
Could you tell me to re-enter the word if i am wrong?
Apr 4, 2020 at 11:45pm UTC
I'll let you figure that one out.
Topic archived. No new replies allowed.