I'm making a word descramble program for http://www.hackthissite.org/missions/prog/1/ . I think I have a lot of this code correct but I dont know how to read each indivdual character in the scrambled string and compare it to the letters in the unscrabled string from the word list.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    int i, l;
string x, y;

    do
    {
    fstream file;
    file.open("text.txt",ios::in|ios::out|ios::nocreate);
    getline(file, x[i]);  //read a string
    i++;
    }while(file);
    file.close;

    do
    {
    fstream file;
    file.open("scramble.txt",iso::in|ios::out|ios::nocreate);
    getline(file, x[i]);  //read a string
    l++;
    }while(file);
    file.close;

    while(i >= 0)
    {
        if (x.length() == y.length())
        {
        //compare the number each char occurs and print word if they are equal
        }
    i--;
    }
    return 0;
}

Recommended Answers

All 8 Replies

Member Avatar for iamthwee

Set up a character frequency counter, for example if the character frequency is the same in each string you have a match.

I would create a nested for loop to do this.

And have an int array count for each letter, so there would be 26 in total.

int letter_count[26]

As you loop through each letter if a match is found increment the array.

It appears that you're using getline but you're reading one character at a time.

You're also loading each file line from each file into the same variable.

In the second loop you're incrementing l but using i as the index

Set up a character frequency counter, for example if the character frequency is the same in each string you have a match.

I have no idea how to do this, could you please explain more

It appears that you're using getline but you're reading one character at a time.

You're also loading each file line from each file into the same variable.

In the second loop you're incrementing l but using i as the index

It appears that you're using getline but you're reading one character at a time.

You're also loading each file line from each file into the same variable.

In the second loop you're incrementing l but using i as the index

Thanks, I fixed that

Member Avatar for iamthwee

Let's say you wanted to count all the letters in the string:

'foobar'

f = 1
o = 2
b = 1
a = 1
r = 1

Think about how you would go about doing that?

Think about how you would go about doing that?

I have no idea....

Member Avatar for iamthwee

Well, you had better take a step back and work that one out first, as it is imperative to understand how a letter frequency counter works.

If you can't work it out with code, start with a flow chart.

Member Avatar for iamthwee

Additionally, consider formatting (indenting) your code properly as it is a good idea to pick up good practices now rather than later.

For example,

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
    int i, l;
    string x, y;
    do
    {
        fstream file;
        file.open("text.txt", ios::in | ios::out | ios::nocreate);
        getline(file, x[i]);  //read a string
        i++;
    }
    while (file);
    file.close;
    do
    {
        fstream file;
        file.open("scramble.txt", iso::in | ios::out | ios::nocreate);
        getline(file, x[i]);  //read a string
        l++;
    }
    while (file);
    file.close;
    while (i >= 0)
    {
        if (x.length() == y.length())
        {
            //compare the number each char occurs and print word if they are equal
        }
        i--;
    }
    return 0;
}

Reads much better. Also your code fails to compile:

x@x-Calistoga-ICH7M-Chipset:~/Documents/cpp$ g++ -Wall -pedantic test.cpp

My output:

test.cpp: In function ‘int main()’:
test.cpp:12:52: error: ‘nocreate’ is not a member of ‘std::ios’
test.cpp:13:27: error: cannot convert ‘std::fstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’
test.cpp:16:12: error: ‘file’ was not declared in this scope
test.cpp:21:35: error: ‘iso’ has not been declared
test.cpp:21:56: error: ‘nocreate’ is not a member of ‘std::ios’
test.cpp:22:27: error: cannot convert ‘std::fstream’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’

Consider fixing those as well. Good luck

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.