Problem with array in C++ Program miss 1

Problem with array in C++ Program miss 1

Post by lycosidae024 on Thu Jun 19, 2014 2:31 am
([msg=81540]see Problem with array in C++ Program miss 1[/msg])

Hello, have spent most of the day looking this over and cant quite spot where im going wrong. I know im leaking a lot of memory, maybe thats my problem? The first code is my working code that im trying to modify to get it to actually match the correct word. The second code is as close as i have come so far.. any help appreciated

Code: Select all
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
int counter = 0;
int number = 0;
string asciiSum;
int sumUpdate = 0;
string getwordsArray[9];

// small helper method to display the before and after arrays
void displayArray (string array[], int size)
{
cout << "{";
for ( int i = 0; i < size; i++ )
{
  // you'll see this pattern a lot for nicely formatting
  // lists--check if we're past the first element, and
  // if so, append a comma
  if ( i != 0 )
  {
   cout << ", ";
  }
  cout << array[ i ];
}
cout << "}";
}

void convertToASCII(string letter)
{
    int sum = 0;
    for (int i = 0; i < letter.length(); i++)
    {
        char x = letter.at(i);
        //cout << int(x) << endl;
        sum += int(x);
    }
    sumUpdate = sum;
}


string temp;
void getwords()
{

    for (int g = 0; g < 10; g++)
    {
        cout << "Please enter you word here " << g << ":\n";
        cin >> temp;
        getwordsArray[g] = temp;
    }
}

int main ()
    {
    getwords();

    int asciiArray[1275];
    string wordlistArray[1275]; // creates array to hold names
    short loop=0; //short for loop for input
    string line; //this will contain the data read from the file
    ifstream myfile ("C:\\Users\\Zack\\Desktop\\wordlist.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
    while (! myfile.eof() ) //while the end of file is NOT reached
    {
    getline (myfile,line); //get one line from the file
    wordlistArray[loop] = line;
    cout << wordlistArray[loop] << endl; //and output it
    asciiSum = line;
    convertToASCII(asciiSum);
    asciiArray[loop] = sumUpdate;
    cout << asciiArray[loop] << "\n" << "\n";
    loop++;
    }
    //cout << "the loop counted too: " << loop;


    string temp1;
    int getwordAsciiArray[19];
    for (int i = 0; i < 10; i++)
    {
        temp1 = getwordsArray[i];
        convertToASCII(temp1);
        getwordAsciiArray[i] = sumUpdate;
    }
//    cout << "\nThis is the words array: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << getwordsArray[i] << "\n";
//    }
//    cout << "\nThis is the value of each word in the array: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << getwordAsciiArray[i] << "\n";
//    }
    int a = 0;
    int b = 0;
    string finalAnswer;
    string finalAnswerArray[10];
    while(b < 1275)
    {
        if (getwordAsciiArray[a] == asciiArray[b])
        {
            finalAnswer = wordlistArray[b];
            finalAnswerArray[a] = finalAnswer;
            b = 0;
            a++;
        }
        b++;
    }
    cout << "\nThe words you entered are: \n";
    displayArray(getwordsArray, 9);
    cout << "\nThe Final Answers are: \n";
    displayArray(finalAnswerArray, 9);  /// Final Results
    myfile.close(); //closing the file
//    cout << "\nThis is the words array: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << getwordsArray[i] << "\n";
//    }
    //cout << "\nThis is the value of each word in the array: \n";
//    int b = 0;
//    int a = 0;
//    string finalAnswer;
//    string finalAnswerArray[9];
//   while (b < 1275)
//    {
//            if (getwordAsciiArray[a] == asciiArray[b])
//            {
//                finalAnswer = array[b];
//                finalAnswerArray[a] = finalAnswer;
//                a++;
//                b = 0;
//            }
//            b++;
//    }  // while statement
//    cout << "The Correct words are: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << finalAnswerArray[i] << "\n";
//    }
    }  // for if statement
    else cout << "Unable to open file"; //if the file is not open output
    system("PAUSE");

}


Second set of code.. need help with this one
Code: Select all
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
int counter = 0;
int number = 0;
string asciiSum;
int sumUpdate = 0;
string getwordsArray[9];
string primaryAnswerArray[9];
string primaryAnswer;
// small helper method to display the before and after arrays
void displayArray (string array[], int size)
{
cout << "{";
for ( int i = 0; i < size; i++ )
{
  // you'll see this pattern a lot for nicely formatting
  // lists--check if we're past the first element, and
  // if so, append a comma
  if ( i != 0 )
  {
   cout << ", ";
  }
  cout << array[ i ];
}
cout << "}";
}

void convertToASCII(string letter)
{
    int sum = 0;
    for (int i = 0; i < letter.length(); i++)
    {
        char x = letter.at(i);
        //cout << int(x) << endl;
        sum += int(x);
    }
    sumUpdate = sum;
}



void getwords()
{
    string temp;
    for (int g = 0; g < 10; g++)
    {
        cout << "Please enter you word here " << g << ":\n";
        cin >> temp;
        getwordsArray[g] = temp;
    }
}

int main ()
    {
    getwords();
    int asciiArray[1275];
    string wordlistArray[1275]; // creates array to hold names
    short loop=0; //short for loop for input
    string line; //this will contain the data read from the file
    ifstream myfile ("C:\\Users\\Zack\\Desktop\\wordlist.txt"); //opening the file.
    if (myfile.is_open()) //if the file is open
    {
    while (! myfile.eof() ) //while the end of file is NOT reached
    {
    getline (myfile,line); //get one line from the file
    wordlistArray[loop] = line;
    //cout << wordlistArray[loop] << endl; //and output it
    asciiSum = line;
    convertToASCII(asciiSum);
    asciiArray[loop] = sumUpdate;
    //cout << asciiArray[loop] << "\n" << "\n";
    loop++;
    }
    }
    myfile.close(); //closing the file
    //cout << "the loop counted too: " << loop;
    getwords();

    string temp1;
    int getwordAsciiArray[19];
    for (int i = 0; i < 10; i++)
    {
        temp1 = getwordsArray[i];
        convertToASCII(temp1);
        getwordAsciiArray[i] = sumUpdate;
    }
//    cout << "\nThis is the words array: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << getwordsArray[i] << "\n";
//    }
//    cout << "\nThis is the value of each word in the array: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << getwordAsciiArray[i] << "\n";
//    }
    int a = 0;
    int b = 0;
    int d = 0;
    int e = 0;
    string secondaryAnswer;
    string secondaryAnswerArray[30];
    while(b < 1276)
    {
        while (b < 1275) /// While statement that gets all the words that match the entered word  ///
        {
        if (getwordAsciiArray[d] == asciiArray[b])
           {
            secondaryAnswer = wordlistArray[b];
            secondaryAnswerArray[a] = secondaryAnswer;
            a++;
           }
        b++;
        }
        b = 0;
      //  string primaryAnswer;
     //   string primaryAnswerArray[10];
        while (d < 10)  /// While statement that checks to see if the entered word matches wordlist.txt
        {
            for (int i = 0; i < a; i++)
            {
                if (secondaryAnswerArray[i] == getwordsArray[d])
                {
                    primaryAnswer = secondaryAnswerArray[i];
                    primaryAnswerArray[e] = primaryAnswer;
                    e++;
                    return 0;
                }
            }
        }
        d++;
        a = 0;
    }
    cout << "The words you entered are: \n";
    displayArray(getwordsArray, 10);
    cout << "The Final Answers are: \n";
    displayArray(primaryAnswerArray, 9);  /// Final Results
//    cout << "\nThis is the words array: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << getwordsArray[i] << "\n";
//    }
    //cout << "\nThis is the value of each word in the array: \n";
//    int b = 0;
//    int a = 0;
//    string finalAnswer;
//    string finalAnswerArray[9];
//   while (b < 1275)
//    {
//            if (getwordAsciiArray[a] == asciiArray[b])
//            {
//                finalAnswer = array[b];
//                finalAnswerArray[a] = finalAnswer;
//                a++;
//                b = 0;
//            }
//            b++;
//    }  // while statement
//    cout << "The Correct words are: \n";
//    for (int i = 0; i < 10; i++)
//    {
//        cout << finalAnswerArray[i] << "\n";
//    }
  //  }  // for if statement
}
lycosidae024
New User
New User
 
Posts: 3
Joined: Thu Jun 19, 2014 2:23 am
Blog: View Blog (0)


Re: Problem with array in C++ Program miss 1

Post by -Ninjex- on Thu Jun 19, 2014 9:28 am
([msg=81548]see Re: Problem with array in C++ Program miss 1[/msg])

I would help you, but this is a programming challenge and all so...
Keep at it and good luck.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Problem with array in C++ Program miss 1

Post by cyberdrain on Thu Jun 19, 2014 11:56 am
([msg=81550]see Re: Problem with array in C++ Program miss 1[/msg])

Ehm, Ninjex, the idea is to learn the language while using it. Fixing the problem can be done with giving hints. You could've just said you don't do C++ and move on, you know. :P

lycosidae024: sorry, I have little knowledge of C++. The only thing I noticed is you use a very large non-dynamic array. Maybe that slows the program down?
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Problem with array in C++ Program miss 1

Post by lycosidae024 on Thu Jun 19, 2014 3:22 pm
([msg=81552]see Re: Problem with array in C++ Program miss 1[/msg])

thanks cyberdrain.. yeah im pretty sure its an array issue.. amongst other things, was really tired writing this. bout to go back through it.

Just looking for a way to increase your post count? lol
I would help you, but this is a programming challenge and all so...
Keep at it and good luck.
lycosidae024
New User
New User
 
Posts: 3
Joined: Thu Jun 19, 2014 2:23 am
Blog: View Blog (0)


Re: Problem with array in C++ Program miss 1

Post by -Ninjex- on Thu Jun 19, 2014 4:57 pm
([msg=81553]see Re: Problem with array in C++ Program miss 1[/msg])

cyberdrain wrote:Ehm, Ninjex, the idea is to learn the language while using it. Fixing the problem can be done with giving hints.

And he could find another project to learn the language first. Instead of asking us to help him fix his code dealing with one of the challenges, or even hint at it.

cyberdrain wrote:You could've just said you don't do C++ and move on, you know. :P

Assuming what? That I don't know C++ lol... this is humorous
I guarantee you that I could create a program to solve this in C++ very quickly actually, and I'm sure others here can vouch for me. No one is going to help this kid, unless it's going to be you. The reason is because he needs to learn the language himself or work on another project we will be willing to help him with, not just hint to him how to fix his code for one of our challenges.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Problem with array in C++ Program miss 1

Post by cyberdrain on Thu Jun 19, 2014 5:20 pm
([msg=81555]see Re: Problem with array in C++ Program miss 1[/msg])

-Ninjex- wrote:And he could find another project to learn the language first. Instead of asking us to help him fix his code dealing with one of the challenges, or even hint at it.

If that is so, why do you think there's a part of the forum dedicated to asking questions about these missions? Note that it's you who assumed he was asking to get his code fixed. He didn't mention that anywhere. If he did ask us to write the code for him to solve the challenge, then yes, I would have agreed with you.

-Ninjex- wrote:
cyberdrain wrote:You could've just said you don't do C++ and move on, you know. :P

Assuming what? That I don't know C++ lol... this is humorous
I guarantee you that I could create a program to solve this in C++ very quickly actually, and I'm sure others here can vouch for me. No one is going to help this kid, unless it's going to be you. The reason is because he needs to learn the language himself or work on another project we will be willing to help him with, not just hint to him how to fix his code for one of our challenges.

Seriously, Ninjex, you assume too much. I haven't implied you don't know C++, where did you read that? All I meant to say was if a similar question (maybe phrased differently) about arrays was asked and it was about Ruby, I think you'd be all over that.
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Problem with array in C++ Program miss 1

Post by -Ninjex- on Thu Jun 19, 2014 5:45 pm
([msg=81557]see Re: Problem with array in C++ Program miss 1[/msg])

He posts his source code and asked for help getting it to work, so yes I assume he is asking us to help him fix it.
Ruby or not, I would have said the same thing.

I also highly doubt the programming challenge threads are intended for people to post their source code and ask for people to help fix it.

Anyways, I'm done on this thread as it's becoming more of a pissing match than something constructive.
image
For those that know
K: 0x2CD8D4F9
User avatar
-Ninjex-
Moderator
Moderator
 
Posts: 1691
Joined: Sun Sep 02, 2012 8:02 pm
Blog: View Blog (0)


Re: Problem with array in C++ Program miss 1

Post by cyberdrain on Thu Jun 19, 2014 6:44 pm
([msg=81560]see Re: Problem with array in C++ Program miss 1[/msg])

-Ninjex- wrote:I also highly doubt the programming challenge threads are intended for people to post their source code and ask for people to help fix it.

Anyways, I'm done on this thread as it's becoming more of a pissing match than something constructive.

I agree on both these accounts.

lycosidae024: had any progress yet fixing your code/finding the problem?
Free your mind / Think clearly
User avatar
cyberdrain
Expert
Expert
 
Posts: 2160
Joined: Sun Nov 27, 2011 1:58 pm
Blog: View Blog (0)


Re: Problem with array in C++ Program miss 1

Post by lycosidae024 on Thu Jun 19, 2014 7:45 pm
([msg=81562]see Re: Problem with array in C++ Program miss 1[/msg])

Im a lot closer, got the program far more stable now, just looking for how i want to go about sorting the strings to compare to , but as far as storing the arrays it works well now. thanks

-- Fri Jun 20, 2014 3:44 am --

you assume way to much ninja.. if you would have read the block of text before my code, i never asked for a fix, was curious where i was going wrong.

but, i have it fixed now.. Thanks cyberdrain
lycosidae024
New User
New User
 
Posts: 3
Joined: Thu Jun 19, 2014 2:23 am
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests