-4

I have just started to learn C++ and am fluent at coding in Python. I wanted to make one of my old python programs again in C++ and it's a word jumble one. In the Python program, you can choose the mode (easy, medium or hard). That aside each mode has it's own dictionary of words which the program chooses randomly. Paired to each word is a hint, hence using the dictionary.

Could someone explain how I would do this in C++?

Thank You

PS. I've got here so far, but I get errors

//Word Jumble - Ben
#include <iostream>
#include <cstdlib>
#include <map>
#include <string>
using namespace std;

int computer(){
    cout << "\nYOU ARE PLAYING AGAINST THE COMPUTER";
    cout << "COMPUTER IS JUBMBLING WORD...";
    system("PAUSE");
    system("CLS");
    map easy
    easy = {{"Ship","A large boat"}, {"Ladder","A Piece of Apparatus that helps you climb things"},{"Water","What the human body is mainly composed of"}};
int main(){
    cout << "\n\n               WELCOME TO WORD JUMBELE";
    cout << "\n                      BY BEN";
    computer()}

Errors:

C:\Users\Ben\SkyDrive\Documents\C++\Word Jumble\Word Jumble.cpp:12:9: error: missing template arguments before 'easy'

C:\Users\Ben\SkyDrive\Documents\C++\Word Jumble\Word Jumble.cpp:12:9: error: expected ';' before 'easy'

C:\Users\Ben\SkyDrive\Documents\C++\Word Jumble\Word Jumble.cpp:17:15: error: expected '}' at end of input
Jason
  • 2,278
  • 2
  • 17
  • 25
Benno_S
  • 69
  • 1
  • 8
  • Paste the errors you get, please. – PaperBirdMaster Aug 07 '14 at 09:59
  • 2
    There is a `;` missing after `map easy` and possibly a `}` before `int main`. Also a `;` missing after `computer()`. – nwp Aug 07 '14 at 10:00
  • You need to provide the types contained in the map. `map = {{"key", "value"}};` – Neil Kirk Aug 07 '14 at 10:01
  • 2
    I'd start by realizing that C++ != Python and by reading [one of these books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Shoe Aug 07 '14 at 10:06
  • I know they aren't the same... thanks for the books but all I wanted to do was to re-do all the excersises I did in python because they worked so well... – Benno_S Aug 07 '14 at 10:08
  • Also, a subtle detail: why do you have a whitespace in your filename? Also, before you use maps, you need to learn about templates first. – Nasser Al-Shawwa Aug 07 '14 at 10:09
  • @user3262355 Yes, I understand that. Now, trust me: there's no such a thing as "just picking C++ up for a project". Either you are committed in learning this language or just don't use it. [Obligatory comic](http://static.fjcdn.com/pictures/Learn+C+++in+21+days_7ee339_3181601.jpg). – Shoe Aug 07 '14 at 10:10
  • I'll also add: as someone who works with both languages, here's a tiny piece of advice: be patient. When learning python, most teaching materials start with tuples, lists and dicts early, whereas in C++, before learning about the STL, you should spend some time with a good beginner tutorial/book (as @Jefffrey mentioned) to understand the nature of the language before trying to copy Python code. Heck; even the notion of a variable doesn't have the same meaning in Python as it does in C++, so you could imagine how different they are. And finally, welcome to Stack Overflow. – Nasser Al-Shawwa Aug 07 '14 at 10:13
  • Thank you, I learnt Python from a scheme of work and am just coping it for C++, is there a proper C++ Scheme of work on the web that you would recommend? – Benno_S Aug 07 '14 at 10:16
  • @user3262355 You mean "framework"? Yes, there are frameworks for the web (Wt for example). Really though, C++ is not made for the web (for most web applications). It's going to be painful. If you mean "framework"s in general, then it really depends on what you want to do. – Shoe Aug 07 '14 at 10:20
  • There's plenty of resources on the internet, but I personally would recommend a good book. "C++ Primer" is my choice for beginners. For reference, go to http://cppreference.com . Also, there's tons of stuff on youtube, but not all of it is reliable, so you have to tread lightly. – Nasser Al-Shawwa Aug 07 '14 at 10:22
  • Yeah sorry I mean framework... But not like codecademy but like a pdf with explanations and exercises of topics in a chronological order, so once I've finished I will be confident in C++ – Benno_S Aug 07 '14 at 10:23

3 Answers3

0

for this

map easy

At first you need to specified the value of map, e.g. map< string,string> easy.

Then you can use

easy["abc"] = "efg";   //like this

I guess you need

string a[100][2] = {{"Ship","A large boat"}, {"Ladder","A Piece of Apparatus that helps you climb things"},{"Water","What the human body is mainly composed of"}};

map<string,string> easy;
for(int i=0; i<3; i++)
    easy[a[i][0] ] = a[i][1];

Then you can use easy["Ship"] like this.

mickeyandkaka
  • 1,452
  • 2
  • 11
  • 21
  • Thanks, but now I get ERROR: NO MATCH FOR 'OPERATOR=' IN 'EASY = {{"Ship","A large boat"}, {"Ladder","A Piece of Apparatus that helps you climb things"},{"Water","What the human body is mainly composed of"}};' – Benno_S Aug 07 '14 at 10:03
  • try to use easy["abc"] = "efg"; Don't forget a ";" after a statement – mickeyandkaka Aug 07 '14 at 10:07
  • the map varibale : easy. You can use it like dict in python. – mickeyandkaka Aug 07 '14 at 10:28
0

As the first error suggests, map easy is missing template arguments. In C++ you need to specify what types the map holds. I think what you want is map<string, string> easy.


I also see that you are missing a semicolon after map easy and computer(). All variable definitions and function calls in C++ must end in a semicolon.


Besides this, you are also missing a closing brace on computer().

Incidentally, computer() returns int, and because you don't have a return statement in computer() that will also cause an error. You could add one, but because you are not using the return value for anything, you may want to simply replace int with void, which will remove the need for a return statement.


I actually had to copy your code and compile it myself to find all the errors. That would not have been necessary if you had not attempted to format your code as if it were Python. In C++, a better way to format this code is as follows:

int computer(){
    cout << "\nYOU ARE PLAYING AGAINST THE COMPUTER";
    cout << "COMPUTER IS JUBMBLING WORD...";
    system("PAUSE");
    system("CLS");
    map easy
    easy = {
        {"Ship","A large boat"},
        {"Ladder","A Piece of Apparatus that helps you climb things"},
        {"Water","What the human body is mainly composed of"}
    };

int main(){
    cout << "\n\n               WELCOME TO WORD JUMBELE";
    cout << "\n                      BY BEN";
    computer()
}

Now it is obvious where the missing brace is.

Michael Dorst
  • 8,210
  • 11
  • 44
  • 71
0

First of all, map is so-called template class. Which means that you have to pass concrete types in map<key_type, value_type> type in order to specify for which types compiler should generate the code. Note that C++ is strictly typed language.

In order to fill map, you should use:

concreteMap[key] = value 

More about it here: http://www.cplusplus.com/reference/map/map/

GuardianX
  • 515
  • 11
  • 29
  • There's nothing wrong with how the OP populated the map. – Michael Dorst Aug 07 '14 at 10:38
  • sure if he wanted to utilize initialization list. Since his intentions are not certain, because his question is not clear enough (he utilizes the assignment operator) I linked the proper way of using it. – GuardianX Aug 07 '14 at 12:18