Hey everyone. I really need some help with my code for class. Here is the prompt :

Write a program to manage a dictionary. The dictionary should be stored on a text file named diction.txt and consist of an alphabatized list of words, one per line. When a user enters a word, scan the dictionary looking for the word. If the word is in the dictionary, say so. If not, display the dictionary word immediately preceding and immediately following so the user can see words that are close in spelling. Then ask if the user wants to answer another word, if the answer is yes, go back to the above steps. If no, terminate the program.

I would really appreciate any help that anyone can give me on this lab.

Dani AI

Generated

A few focused troubleshooting steps and a simple, reliable approach to the lookup.

A likely immediate cause: in Post #5 the filename is written as dictiona.txt while the assignment calls for diction.txt — check for that typo and for hidden extensions (Windows Explorer can show a file named diction.txt.txt). Also note that dropping a file into the IDE’s “Source Files” view does not always place it in the program’s runtime working directory; was right to ask for code, and was right that you need a real text dictionary file.

Quick checks to run inside the program: print the current working directory and attempt to open the exact filename. This shows where the program is looking.

#include <iostream>
#include <fstream>
#include <filesystem>

int main() {
    std::cout << "cwd: " << std::filesystem::current_path() << '\n';
    std::ifstream fin("diction.txt");
    if (!fin) { std::cerr << "Cannot open diction.txt\n"; return 1; }
    std::string line;
    if (std::getline(fin, line)) std::cout << "First line: " << line << '\n';
}

If the file still won’t open, either move the file into the printed cwd (or the executable folder) or use an absolute path. In Visual Studio, set the file’s property “Copy to Output Directory” to “Copy if newer” so it appears beside the .exe when debugging.

Recommended implementation (simple and robust): read the dictionary once into a std::vector<std::string>, normalize lines (trim and optionally lowercase), then use std::lower_bound to find the query’s insertion point and report the immediate predecessor and successor:

// after filling vector<string> dict and getting string query:
auto it = std::lower_bound(dict.begin(), dict.end(), query);
if (it != dict.end() && *it == query) std::cout << "Found\n";
else {
  std::string before = (it == dict.begin() ? "<start>" : *(it-1));
  std::string after  = (it == dict.end()   ? "<end>"   : *it);
  std::cout << "Not found. Before: " << before << " After: " << after << '\n';
}

Extra notes: ensure the dictionary truly is one word per line, test with a tiny sample first, and normalize case if the search should be case-insensitive.

Recommended Answers

All 4 Replies

Hi amber... No-one is going to do your homework for you... show us you tried and we will assist where you get stuck... post some code or give errors or problems dont ask us to do everything

The first thing you need to do is create that text file, unless your teacher gave you one. If you google around for dictionary text files you may be able to find one.

After that, write your program one small step at a time. you know that you have to ask the user for a word, so start out your program with just that informaton. Compile and get it working before moving on gto the next part of the assignment.

is one of several text dictionaries

sorry here is my attempt, the only problem is that for some reason I can never open the file dictiona.txt ( which is actually a txt item in my "source files" folder.So I can't even see if my program is working correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.