0

I am trying to replicate the dictionary example found in Bjarne Stroustrup's "Programming: Principles and Practice Using C++" (2016 ed)

The goal of the example is, given an input of some words, they're written out in order without any repetition.

I am using CodeBlocks 20.03, and when I tried to run the example, nothing showed up on the terminal. When I typed some input, it went through without any error messages, but with no response.

This is the code:

//simple dictionary: list of sorted words
#include "std_lib_facilities.h"; 
int main()
{
    vector<string> words;
    for(string temp; cin>>temp; )        //read whitespace-separated words
        words.push_back(temp);     //put into vector
    cout << "Number of words: " << words.size() << '\n';
    sort(words);                                     //sort the words
    for (int i = 0; i<words.size(); ++i)
        if (i==0 || words[i-1]!=words[i])//is this a new word?
        cout << words[i] << "\n";
    }
  • 2
    How do you terminate the input? How do you tell the program that you have finished with the input? – Some programmer dude Jan 05 '21 at 07:02
  • Not really enough information to give you an answer. Can you make any program work? Can you simplify the above program and make it work? Which part of the above code do you have to remove to make it work? There could be many reasons you are having problems and really in the first place it's up to you to do some work trying to diagnose the problem. – john Jan 05 '21 at 07:04
  • If the code really is **exactly** as you have posted above then it will not compile (missing `}`). – john Jan 05 '21 at 07:05
  • On an unrelated note, even if the examples in the book are using the `"std_lib_facilities.h"` header file, please make it a habit to include the proper standard header files. Otherwise you'll soon include `` which [you should not do](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) (I'd argue that `"std_lib_facilities.h"` is just as bad). – Some programmer dude Jan 05 '21 at 07:05
  • 1
    Code::blocks has a front end for debuggers (probably GDB in your case). With a debugger you can step through the program and see where things went wrong. Not using a debugger is handicapping yourself, so learn to use debuggers as soon as possible.. – user4581301 Jan 05 '21 at 07:06
  • Thanks a lot everyone! To "Some programmer dude", your question was helpful in making me realize, the author wants us to terminate the program using Ctrl Z to get the output – Mohammad Rizvi Jan 05 '21 at 07:11

0 Answers0