Rating system

Hey I was just wondering if there was any way to implement a system where you can say somehthing like:
Add "Rambo" 5
And that would save and then you could say
Get "Rambo"
and it would return
Rambo has a rating of 5
and then if you wanted to remove it you would say:
Remove "Rambo"
There are numerous ways to implement this.
Can you not suggest a way to create a system like this?
i have been trying to implement it through adding a linked list but it just keeps giving me wrong output
Last edited on
May You show your code to us?... I am sure there is anybody out here to help You to improve your code:)...
you can use std::map<std::string,int> to hold the data. write a loop where in each iteration you get a string command. use a switch control structure to check the input. if the command is "quit" you stop the program. if the command is "get" or "remove" you read one more string which is the parameter for the command. if the command is "add" you read one more string and an int.

info on std::map -> http://cplusplus.com/reference/stl/map/
info on switch -> http://cplusplus.com/doc/tutorial/control/ (scroll down)
info on std::strings -> http://cplusplus.com/reference/string/
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
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
#include <map>
#include <iostream>
#include <string>
using namespace std;


// Value-Defintions of the different String values
static enum StringValue { evNotDefined, 
                          evStringValue1, 
                          evStringValue2, 
                          evStringValue3, 
                          evEnd };

// Map to associate the strings with the enum values
static map<std::string, StringValue> s_mapStringValues;

// User input
static char szInput[_MAX_PATH];

struct Film{
	string Title;
	int rating;
};


void Initialize(string i = "", string num = "")
{
	string film;
  film = "Add \"";film.append( i );film.append( "\"" );film.append( num );
  s_mapStringValues[film] = evStringValue1;
  s_mapStringValues["Second Value"] = evStringValue2;
  s_mapStringValues["Third Value"] = evStringValue3;
  s_mapStringValues["end"] = evEnd;

  cout << "s_mapStringValues contains " 
       << s_mapStringValues.size() 
       << " entries." << endl;
}

int main(int argc, char* argv[])
{
  // Init the string map
  

  // Loop until the user stops the program
  while(1)
  {
    // Get the user's input
    cout << "Please enter a string (end to terminate): ";
    cout.flush();
	string Input;
	cin >> Input;
	Film Mine;
	Mine.Title = Input;
	Initialize(Input, "5");
    // Switch on the value
    switch(s_mapStringValues[Input])
    {
      case evStringValue1:
			cout << Mine.Title << endl;
        break;
      case evStringValue2:
        cout << "Detected the second valid string." << endl;
        break;
      case evStringValue3:
        cout << "Detected the third valid string." << endl;
        break;
      case evEnd:
        cout << "Detected program end command. "
             << "Programm will be stopped." << endl;
        return(0);
      default:
        cout << "'" << szInput << "' is an invalid string. s_mapStringValues now contains "<< s_mapStringValues.size() 
             << " entries." << endl;
        break;
    }
  }

  return 0;
}


This is my best attempt so far
Last edited on
Hmm... I would suggest using two separate maps for holding the command->enum association and the title->rating association... I hope the following helps:

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
//...
enum Command {cmdQuit=0,cmdAdd=1,cmdGet=2,cmdRemove=3};

int main()
{

    map<string,Command> commands;

    commands["quit"]=cmdQuit;
    commands["add"]=cmdAdd;
    commands["get"]=cmdGet;
    commands["remove"]=cmdRemove;

    map<string,int> films;

    string input;

    while (true)
    {

        cin >> input;

        //check if the command exists
        if (commands.find(input)==commands.end())
        {
            cout << "wrong command!" << endl;
            continue;
        }

        switch (commands[input])
        {
            case cmdQuit:
            //...
            
            break;
            case cmdAdd:
            //...
            
            break;
            case cmdGet:
            //...
            
            break;
            case cmdRemove:
            //...
        }
    }
    //...
}
//... 
Topic archived. No new replies allowed.