6 Replies - 200 Views - Last Post: 07 February 2012 - 08:54 AM Rate Topic: -----

Topic Sponsor:

#1 znerihc  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 05-February 12

Looping and Increment Problem

Posted 06 February 2012 - 09:50 PM

so basically i am creating this game kinda like super text twist. I have this code that is supposed to get the number of possible words that can be made from the jumbled words. basically all words in a text file (which contains all valid words)is checked against the given word, if it is a subset. If a certain word is a subset, the counter increments by one.

#include<iostream>
#include<fstream>

using namespace std;

int main()
{
    ifstream input;
    input.open("corncob_caps.txt");
    string word;
    string getWord;
    char letters[]={ 'A', 'B', 'C','D', 'E', 'F', 'G', 'H', 'I', 'J', 'K' , 'L' ,'M' ,'N' ,'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
    int accum1[]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
    bool is_subset=1;
    int counter=0;
    cin >> word;
    
    int length1=word.size();
    for (int i=0; i< length1; i++)
    {
        char letter= word[i];
        switch(letter)
		{ 
		case 'A':
             accum1[0]+=1;
             break;
		case 'B':
             accum1[1]+=1;
             break;
		case 'C':
             accum1[2]+=1;
             break;
		case 'D':
             accum1[3]+=1;
             break;
		case 'E':
             accum1[4]+=1;
             break;
		case 'F':
             accum1[5]+=1;
             break;
		case 'G':
             accum1[6]+=1;
             break;
		case 'H':
             accum1[7]+=1;
             break;
		case 'I':
             accum1[8]+=1;
             break;
		case 'J':
			accum1[9]+=1;
             break;
		case 'K':
             accum1[10]+=1;
             break;
		case 'L':
             accum1[11]+=1;
             break;
		case 'M':
             accum1[12]+=1;
             break;
		case 'N':
             accum1[13]+=1;
             break;
		case 'O':
             accum1[14]+=1;
             break;
		case 'P':
             accum1[15]+=1;
             break;
		case 'Q':
             accum1[16]+=1;
             break;
		case 'R':
             accum1[17]+=1;
             break;
		case 'S':
             accum1[18]+=1;
             break;
		case 'T':
             accum1[19]+=1;
             break;
		case 'U':
			accum1[20]+=1;
             break;
		case 'V':
             accum1[21]+=1;
             break;
		case 'W':
             accum1[22]+=1;
             break;
        case 'X':
             accum1[23]+=1;
             break;
        case 'Y':
             accum1[24]+=1;
             break;
        default:
             accum1[25]+=1;
		}
    } 



do
{
    getline(input, getWord);
    int accum2[]={0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
    int length2=getWord.size();
    
    for (int i=0; i< length2; i++)
    {
        char letter= getWord[i];
        switch(letter)
		{ 
		case 'A':
             accum2[0]+=1;
             break;
		case 'B':
             accum2[1]+=1;
             break;
		case 'C':
             accum2[2]+=1;
             break;
		case 'D':
             accum2[3]+=1;
             break;
		case 'E':
             accum2[4]+=1;
             break;
		case 'F':
             accum2[5]+=1;
             break;
		case 'G':
             accum2[6]+=1;
             break;
		case 'H':
             accum2[7]+=1;
             break;
		case 'I':
             accum2[8]+=1;
             break;
		case 'J':
			accum2[9]+=1;
             break;
		case 'K':
             accum2[10]+=1;
             break;
		case 'L':
             accum2[11]+=1;
             break;
		case 'M':
             accum2[12]+=1;
             break;
		case 'N':
             accum2[13]+=1;
             break;
		case 'O':
             accum2[14]+=1;
             break;
		case 'P':
             accum2[15]+=1;
             break;
		case 'Q':
             accum2[16]+=1;
             break;
		case 'R':
             accum2[17]+=1;
             break;
		case 'S':
             accum2[18]+=1;
             break;
		case 'T':
             accum2[19]+=1;
             break;
		case 'U':
			accum2[20]+=1;
             break;
		case 'V':
             accum2[21]+=1;
             break;
		case 'W':
             accum2[22]+=1;
             break;
        case 'X':
             accum2[23]+=1;
             break;
        case 'Y':
             accum2[24]+=1;
             break;
        default:
             accum2[25]+=1;
		}
    } 

    
  for (int i=0; i<26; i++)
    {
        if (accum2[i]>accum1[i])
        {  
          is_subset=0;
        
        }        
    }
    
  if (is_subset)
     counter++;

}while(!input.eof());


cout << counter;



system("pause");
return 0;

}
  
  




but i cant seem to make it work. for any word, the answer is always zero, except for aardvark, in which case the answer is one. any help? the text file is included by the way.

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Looping and Increment Problem

#2 tlhIn`toq  Icon User is online

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: Looping and Increment Problem

Posted 06 February 2012 - 10:15 PM

YOu have one loop, that is to go through all the letters of the one word.

Wouldn't you also need a loop encasing that to go through each word in the list?

Also, that case construct just kills me.

YOu can just compare the nth letter of the word entered to the corresponding letter of the word you are checking against.




Oh my goodness... There's a second giant case construct.

Please spend a bit more time with self-teaching books and work on the language a while longer before trying to archetict programs from scratch.

The problem with only owning a hammer is that no matter what the job is, you have to find a way to make a hammer work... as a wrench... as a screw driver... because its the only tool you have.

Same with coding. When you have a very limited number of tools in your coding toolbox, you will try to make them work for things that other tools are better suited for.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1812
  • View blog
  • Posts: 4,891
  • Joined: 27-December 05

Re: Looping and Increment Problem

Posted 06 February 2012 - 10:46 PM

All that scolding notwithstanding, it would work on Windows systems if you would simply re-initialize is_subset = 1; at the beginning of the do-while loop. As it is now, as soon as you get the first "miss" you set it to 0 and there it remains.

For Linux systems your program has a more insidious problem: every line in your Windows-generated dictionary has char 13 ('CR') before the newline char, and the default case in your switch statement counts that in accum2[25] as if it were a 'Z'. But the initial word entered at the keyboard in Linux never has a 'CR' so none of the words in the dictionary will be a subset of it.


PS: both of your accum array initializers contain 27 0s and a trailing comma.

This post has been edited by r.stiltskin: 06 February 2012 - 10:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 znerihc  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 05-February 12

Re: Looping and Increment Problem

Posted 07 February 2012 - 04:40 AM

View PosttlhIn`toq, on 06 February 2012 - 10:15 PM, said:

YOu have one loop, that is to go through all the letters of the one word.

Wouldn't you also need a loop encasing that to go through each word in the list?

Also, that case construct just kills me.

YOu can just compare the nth letter of the word entered to the corresponding letter of the word you are checking against.




Oh my goodness... There's a second giant case construct.

Please spend a bit more time with self-teaching books and work on the language a while longer before trying to archetict programs from scratch.

The problem with only owning a hammer is that no matter what the job is, you have to find a way to make a hammer work... as a wrench... as a screw driver... because its the only tool you have.

Same with coding. When you have a very limited number of tools in your coding toolbox, you will try to make them work for things that other tools are better suited for.


Sorry for offending you with my post/question. I forgot to mention that I am just starting to learn the language, and what I have been doing is just to test some things out. Gee, sorry for exploring the possibilities with what I know now and not having the superior intellect in C++ as you do. Thanks for ruining my day.

++++++++++

That being said, thank you r.stiltskin, your reply was very helpful, unlike the reply before you.
Was This Post Helpful? 1
  • +
  • -

#5 kevindckr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 105
  • Joined: 09-January 12

Re: Looping and Increment Problem

Posted 07 February 2012 - 05:17 AM

That's how I was greeted on here too.
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • WillMyCodeWork = !FailedWhenYouTriedIt;
  • member icon

Reputation: 3290
  • View blog
  • Posts: 6,898
  • Joined: 02-June 10

Re: Looping and Increment Problem

Posted 07 February 2012 - 08:34 AM

I'm afraid you misinterpreted my post. There was no scolding involved. Nobody insulted you, called you names, said you couldn't do programming, said you were a bad person... or anything negative about who you are.

We see LOTS of people who think the way to learn is to start architecting their own programs from scratch. Its amazing how many people start trying to develop calculators or text adventures after their first week with a course or self-teaching book.

This approach never turns out well. It is the coding equivalent of saying "I'm going to start doing appendectomies and learn surgery has I go" after a week with an anatomy textbook. There are constant threads with the most basic of questions that all come from a misguided desire to do before learning.

The result of all that is extreme frustration for the student. They begin to feel they are incapable, not cut out of this career choice, doubting their intellect or at least their ability to learn, lowering morale etc. THAT is what we DON'T want. Those of us that do this for a living must love it and want to share that with others or we wouldn't be here mentoring people in the hopes they too would take it to heart as we have. Suggestions made to work an entire self-teaching book from cover-to-cover before trying to build your own application are out of desire to see you learn and enjoy the process so you stick with it.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is online

  • No Sugar Coding Here!
  • member icon

Reputation: 4686
  • View blog
  • Posts: 20,364
  • Joined: 23-August 08

Re: Looping and Increment Problem

Posted 07 February 2012 - 08:54 AM

Any time you see repeating code, think "function". There are tutorials on those in my signature.

The problem with the case statement is that it's easily removed. The letters 'A' through 'Z' are represented numerically in order per the ASCII table. Also, because characters are actually representations of numbers, you can do arithmetic using them. For example, 'A' - 'A' == 0. Understanding that, think about how you can use that knowledge to remove the switch/case construct(s) altogether.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1