Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:39:37+00:00 2026-06-05T12:39:37+00:00

For a homework assignment: I’m supposed to create randomized alphabetial keys, print them to

  • 0

For a homework assignment: I’m supposed to create randomized alphabetial keys, print them to a file, and then hash each of them into a hash table using the function “goodHash”, found in my below code.

When I try to run the below code, it says my “goodHash” “identifier isn’t found”. What’s wrong with my code?

#include <iostream>
#include <vector>
#include <cstdlib>
#include "math.h"
#include <fstream>
#include <time.h>
using namespace std;

// "makeKey" function to create an alphabetical key 
// based on 8 randomized numbers 0 - 25.
string makeKey() {
    int k;
    string key = "";
    for (k = 0; k < 8; k++) {
        int keyNumber = (rand() % 25);
        if (keyNumber == 0)
            key.append("A");
        if (keyNumber == 1)
            key.append("B");
        if (keyNumber == 2)
            key.append("C");
        if (keyNumber == 3)
            key.append("D");
        if (keyNumber == 4)
            key.append("E");
        if (keyNumber == 5)
            key.append("F");
        if (keyNumber == 6)
            key.append("G");
        if (keyNumber == 7)
            key.append("H");
        if (keyNumber == 8)
            key.append("I");
        if (keyNumber == 9)
            key.append("J");
        if (keyNumber == 10)
            key.append("K");
        if (keyNumber == 11)
            key.append("L");
        if (keyNumber == 12)
            key.append("M");
        if (keyNumber == 13)
            key.append("N");
        if (keyNumber == 14)
            key.append("O");
        if (keyNumber == 15)
            key.append("P");
        if (keyNumber == 16)
            key.append("Q");
        if (keyNumber == 17)
            key.append("R");
        if (keyNumber == 18)
            key.append("S");
        if (keyNumber == 19)
            key.append("T");
        if (keyNumber == 20)
            key.append("U");
        if (keyNumber == 21)
            key.append("V");
        if (keyNumber == 22)
            key.append("W");
        if (keyNumber == 23)
            key.append("X");
        if (keyNumber == 24)
            key.append("Y");
        if (keyNumber == 25)
            key.append("Z");
    }
    return key;
}

// "makeFile" function to produce the desired text file.
// Note this only works as intended if you include the ".txt" extension,
// and that a file of the same name doesn't already exist.
void makeFile(string fileName, int n) {
    ofstream ourFile;
    ourFile.open(fileName);
    int k; // For use in below loop to compare with n.
    int l; // For use in the loop inside the below loop.
    string keyToPassTogoodHash = "";
    for (k = 1; k <= n; k++) {
        for (l = 0; l < 8; l++) {    // For-loop to write to the file ONE key
        ourFile << makeKey()[l];
        keyToPassTogoodHash += (makeKey()[l]);
        }
        ourFile << "  " << k << "\n";// Writes two spaces and the data value
        goodHash(keyToPassTogoodHash); // I think this has to do with the problem
        makeKey(); // Call again to make a new key.
    }
}

// Primary function to create our desired file!
void mainFunction(string fileName, int n) {
    makeKey();
    makeFile(fileName, n);
}

// Hash Table for Part 2
struct Node {
    int key;
    string value;
    Node* next;
}; 
const int hashTableSize = 10;
Node* hashTable[hashTableSize];

// "goodHash" function for Part 2
void goodHash(string key) {
    int x = 0;
    int y;
    int keyConvertedToNumber = 0;
    // For-loop to produce a numeric value based on the alphabetic key,
    // which is then hashed into hashTable using the hash function
    // declared below the loop (hashFunction).
    for (y = 0; y < 8; y++) {
        if (key[y] == 'A' || 'B' || 'C')
            x = 0;
        if (key[y] == 'D' || 'E' || 'F')
            x = 1;
        if (key[y] == 'G' || 'H' || 'I')
            x = 2;
        if (key[y] == 'J' || 'K' || 'L')
            x = 3;
        if (key[y] == 'M' || 'N' || 'O')
            x = 4;
        if (key[y] == 'P' || 'Q' || 'R')
            x = 5;
        if (key[y] == 'S' || 'T')
            x = 6;
        if (key[y] == 'U' || 'V')
            x = 7;
        if (key[y] == 'W' || 'X')
            x = 8;
        if (key[y] == 'Y' || 'Z')
            x = 9;
        keyConvertedToNumber = x + keyConvertedToNumber; 
    }
    int hashFunction = keyConvertedToNumber % hashTableSize;
    Node *temp;
    temp = new Node;
    temp->value = key;
    temp->next = hashTable[hashFunction];
    hashTable[hashFunction] = temp;
}

// First two lines are for Part 1, to call the functions key to Part 1.
int main() {
    srand ( time(NULL) );            // To make sure our randomization works.
    mainFunction("sandwich.txt", 5); // To test program
    cin.get();
    return 0;
}

I realize my code is cumbersome in some sections, but I’m a noob at C++ and don’t know much to do it better.

I’m guessing another way I could do it is to AFTER writing the alphabetical keys to the file, read them from the file and hash each key as I do that, but I wouldn’t know how to go about coding that.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:39:39+00:00Added an answer on June 5, 2026 at 12:39 pm

    C++ expected everything to be declared in order, so that nothing’s used before it’s declared.
    If you need to refer to a function higher in the file than where it’s defined, you need to have a function prototype near the top of the file that declares the function. (Writing prototypes for all functions is a standard practice as a result of this.)

    Near the top of the file (after the #includes) simply add

    void goodHash(string key);
    

    Definitions

    Function declaration: something that declares the name of the function and the types the function takes.

    Function definition: something that specifies the actual code of the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a homework assignment in which I'm supposed to create a vector of pointers
As part of a homework assignment, we are supposed to create an array that
For my homework assignment, I'm supposed to create an ATM/Teller program which stores users
I have a homework assignment where I need to take input from a file
For a homework assignment, we're to be turning the basicCompare method into something that
I'm doing a homework assignment in which I need to print out to the
I'm working on a homework assignment and I ran into a little snag. I'm
So for a homework assignment I am supposed to play with several threading mechanisms
I have a homework assignment that I am supposed to write a http server
My homework assignment is to create a shell (done) with a history function (done)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.