C++ where to begin?

I have a fairly involved question about how to start writing a c++ program, I know how to use different things in c++ but I don't know how to put it all together. here is the question. Where should I start the programming for this?

For this problem, you are given an abbreviated “dictionary” (see below). Your program should read a string of digits (from 2 to 9, not using 1 as space) from the console and find the words in the dictionary whose spellings contain that series of consecutive digits anywhere within the word. If there are no matches, print the string 'No matches'. If there is one match, print the matching word. If there are n > 1 matches, print the string 'n matches:' followed by the matching words, one per line. Here is the dictionary to be used for this problem with the numeric spellings of each word:
cappuccino: 2277822466
chocolate: 246265283
cinnamon: 24662666
coffee: 263333
latte: 52883
vanilla: 8264554
So do you read in
cappuccino: 2277822466

or do you just get
cappuccino:
and you're expected to derive 2277822466 yourself.

Looks like a job for std::map TBH.
The "codes" are telephone number-to-letter mappings.

cappuccino
2277822466

2 ABC
3 DEF
4 GHI
5 JKL
6 MNO
7 PQRS
8 TUV
9 WXYZ

ah so they type in 246 and get chocolate and cinnimon and cappuchino etc?

and I assume this is a challenge / timed problem too, right?
you need a way to rapidly eliminate data to avoid searching things that are no use.
eg for the 246 example, how do you avoid looking thru latte for what cannot be there?

or, if you can find one, a way to directly get a yes/no from a word without searching its pattern.
the point is to find either no matches, 1 matches, 2 matches, etc.

yes, that is what I am looking to do, find a way to search the dictionary for anything that matches and print out all the matches but I don't know how to define them so it will be easy to code what I will need.

I have never learned about std::map, if that would work in this case.
If I understand your problem, I think a vector of a struct with two strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct NameCode
{
    string name;
    string code;
};


int main()
{
    vector<NameCode> v;
    
    // read the pairs into v
    
    // for each input
    //    loop through v checking if the input string is a substring
    //                 of the NameCode's code.
}

I am have not heard of struct before, only a little of vectors with arrays. So how would i define the dictionary and how does this even work?
> I know how to use different things in c++ but I don't know how to put it all together.
...
> I am have not heard of struct before
You need to pick up the pace on learning new things.

You don't need to memorise the manual pages, but knowing in high level terms what the features are and what they're typically used for helps you get started on new problems. Like for example knowing what all the containers are.
https://en.cppreference.com/w/cpp/container

> I have never learned about std::map, if that would work in this case.
Then allow me to demonstrate.
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
#include <string>
#include <iostream>
#include <string>
#include <map>
#include <list>
using namespace std;

int main()
{
  map<string,list<string>> dictionary;

  dictionary["2277822466"].push_back("cappuccino");
  dictionary["246265283"].push_back("chocolate");
  dictionary["24662666"].push_back("cinnamon");
  dictionary["263333"].push_back("coffee");
  dictionary["52883"].push_back("latte");
  dictionary["8264554"].push_back("vanilla");

  // for fun, the number doesn't match the name
  dictionary["52883"].push_back("mocca");

  cout << "What?";
  string what;
  cin >> what;

  map<string,list<string>>::iterator it;
  it = dictionary.find(what);
  if( it != dictionary.end() ) {
    list<string>::iterator i = it->second.begin();
    while( i != it->second.end() ) {
      cout << *i << endl;
      ++i;
    }
  }

  return 0;
}


$ g++ -std=c++11 bar.cpp
$ ./a.out 
What?52883
latte
mocca


And if as jonnin suggests there are substrings involved, then you iterate over the entire dictionary and use string::find() to see if what you type in is contained within a dictionary key string.

ok, I see how that works, thank you so much. this will be very helpful.
well, what I was trying to say was, for substring searching you want to minimize the # of calls to find() if you can, by storing the data in a way that you can ignore parts of your dictionary, because you know the substring can't be in there. If you are doing one of those challenges with a huge dictionary and a time limit, we can explore that.
Topic archived. No new replies allowed.