I cannot assign my rawWord value to the string array. The program reads a value from testText.txt and then edits it. Then this manipulated char rawWord[20]'s value must be assigned to the string array word. When i debug i see that rawWord is edited correctly, but when it comes to addWord(), the program does not assign the value to the string word[] array. Can you help me with this problem please.

This is my first post, so if i have mistakes sorry.


#ifndef WORDS_H
#define WORDS_H

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class words
{
	
	int arraySize;
	string word[1000];
	char rawWord[20];
	
	public:
	words(int arraySizeI=1 ): arraySize(arraySizeI){}

	void readWord(fstream & txtPointer)// member function that just reads the data and hold it in the rawWord[]
	{
		txtPointer>>rawWord;
	}
	
	bool checkWord()
	{
		if( !((rawWord[0]>64 && rawWord[0]<91) || (rawWord[0]>96 && rawWord[0]<123)) )// check if it is a word starting with
			return 0;																  // an alphabet
		else // if it is a acceptable formed word then enter this
		{
			return 1;
		}
	}

	void editWord()
	{
		for(int i=0; i<20; i++)
		{
			if( !((rawWord[i]>64 && rawWord[i]<91) || (rawWord[i]>96 && rawWord[i]<123)))
			{
				rawWord[i]=0;
				break;
			}
			else if( (rawWord[i]>64 && rawWord[i]<91) )
				rawWord[i]+=32;
		}
	}
	bool checkSame()
	{
		bool same=0;
		for(int i=0; i<arraySize; i++)
		{
			if( word[i]== rawWord)
			{
				same=1;
				return same;
			}
		}
		return same;
	}

	void addWord()
	{
		word[arraySize-1]=rawWord;
		arraySize++;
	}
};

#endif
#include "200611004_lab2.h"

int main()
{
	bool status;// holds 1 if word is acceptable else holds 0
	words test;
	fstream file;
	file.open("C:\\a\\testText.txt");
	for(int i=0; i<1000; i++)
	{
		test.readWord(file);
		status=test.checkWord();
		
		if(status==1)
		{
			test.editWord();
			if(test.checkSame())
				test.addWord();
		}
	}


	return 0;
}

Edited 6 Years Ago by Tellalca: n/a

Comments
Well done on the code tags, for your first post.
Attachments
#include "200611004_lab2.h"

int main()
{
	bool status;// holds 1 if word is acceptable else holds 0
	words test;
	fstream file;
	file.open("C:\\a\\testText.txt");
	for(int i=0; i<1000; i++)
	{
		test.readWord(file);
		status=test.checkWord();
		
		if(status==1)
		{
			test.editWord();
			if(test.checkSame())
				test.addWord();
		}
	}


	return 0;
}
#ifndef WORDS_H
#define WORDS_H

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class words
{
	
	int arraySize;
	string word[1000][];
	char rawWord[20];
	
	public:
	words(int arraySizeI=1 ): arraySize(arraySizeI){}

	void readWord(fstream & txtPointer)// member function that just reads the data and hold it in the rawWord[]
	{
		txtPointer>>rawWord;
	}
	
	bool checkWord()
	{
		if( !((rawWord[0]>64 && rawWord[0]<91) || (rawWord[0]>96 && rawWord[0]<123)) )// check if it is a word starting with
			return 0;																  // an alphabet
		else // if it is a acceptable formed word then enter this
		{
			return 1;
		}
	}

	void editWord()
	{
		for(int i=0; i<20; i++)
		{
			if( !((rawWord[i]>64 && rawWord[i]<91) || (rawWord[i]>96 && rawWord[i]<123)))
			{
				rawWord[i]=0;
				break;
			}
			else if( (rawWord[i]>64 && rawWord[i]<91) )
				rawWord[i]+=32;
		}
	}
	bool checkSame()
	{
		bool same=0;
		for(int i=0; i<arraySize; i++)
		{
			if( word[i]== rawWord)
			{
				same=1;
				return same;
			}
		}
		return same;
	}

	void addWord()
	{
		word[arraySize-1]=rawWord;
		arraySize++;
	}
};

#endif
Our late edict shall strongly stand in force:
Navarre shall be the wonder of the world;
Our court shall be a little Academe,
Still and contemplative in living art.
You three, Biron, Dumain, and Longaville,
Have sworn for three years term to live with me
My fellow-scholars, and to keep those statutes
That are recorded in this schedule here:
Your oaths are passd; and now subscribe your names,
That his own hand may strike his honour down
That violates the smallest branch herein:
If you are armd to do as sworn to do,
Subscribe to your deep oaths, and keep it too.
LONGAVILLE I am resolved; tis but a three years fast:
The mind shall banquet, though the body pine:
Fat paunches have lean pates, and dainty bits
Make rich the ribs, but bankrupt quite the wits.
DUMAIN My loving lord, Dumain is mortified:
The grosser manner of these worlds delights
He throws upon the gross worlds baser slaves:
To love, to wealth, to pomp, I pine and die;
With all these living in philosophy.
BIRON I can but say their protestation over;
So much, dear liege, I have already sworn,
That is, to live and study here three years.
But there are other strict observances;
As, not to see a woman in that term,
Which I hope well is not enrolled there;
And one day in a week to touch no food
And but one meal on every day beside,
The which I hope is not enrolled there;
And then, to sleep but three hours in the night,
And not be seen to wink of all the day--
When I was wont to think no harm all night
And make a dark night too of half the day--
Which I hope well is not enrolled there:
O, these are barren tasks, too hard to keep,
Not to see ladies, study, fast, not sleep!
FERDINAND Your oath is passd to pass away from these.
BIRON Let me say no, my liege, an if you please:
I only swore to study with your grace
And stay here in your court for three years space.
LONGAVILLE You swore to that, Biron, and to the rest.
BIRON By yea and nay, sir, then I swore in jest.
What is the end of study? let me know.
FERDINAND Why, that to know, which else we should not know.
BIRON Things hid and barrd, you mean, from common sense?
FERDINAND Ay, that is studys godlike recompense.
BIRON Come on, then; I will swear to study so,
To know the thing I am forbid to know:
As thus,--to study where I well may dine,
When I to feast expressly am forbid;
Or study where to meet some mistress fine,
When mistresses from common sense are hid;
Or, having sworn too hard a keeping oath,
Study to break it and not break my troth.
If studys gain be thus and this be so,
Study knows that which yet it doth not know:
Swear me to this, and I will neer say no.
FERDINAND These be the stops that hinder study quite
And train our intellects to vain delight.
BIRON Why, all delights are vain; but that most vain,
Which with pain purchased doth inherit pain:
As, painfully to pore upon a book
To seek the light of truth; while truth the while
Doth falsely blind the eyesight of his look:
Light seeking light doth light of light beguile:
So, ere you find where light in darkness lies,
Your light grows dark by losing of your eyes.
Study me how to please the eye indeed
By fixing it upon a fairer eye,
Who dazzling so, that eye shall be his heed
And give him light that it was blinded by.
Study is like the heavens glorious sun
That will not be deep-searchd with saucy looks:
Small have continual plodders ever won
Save base authority from others books
These earthly godfathers of heavens lights
That give a name to every fixed star
Have no more profit of their shining nights
Than those that walk and wot not what they are.
Too much to know is to know nought but fame;
And every godfather can give a name.
FERDINAND How well hes read, to reason against reading!
DUMAIN Proceeded well, to stop all good proceeding!
LONGAVILLE He weeds the corn and still lets grow the weeding.
BIRON The spring is near when green geese are a-breeding.
DUMAIN How follows that?
BIRON Fit in his place and time.
DUMAIN In reason nothing.
BIRON Something then in rhyme.
FERDINAND Biron is like an envious sneaping frost,
That bites the first-born infants of the spring.
BIRON Well, say I am; why should proud summer boast
Before the birds have any cause to sing?
Why should I joy in any abortive birth?
At Christmas I no more desire a rose
Than wish a snow in Mays new-fangled mirth;
But like of each thing that in season grows.
So you, to study now it is too late,
Climb oer the house to unlock the little gate.
FERDINAND Well, sit you out: go home, Biron: adieu.
BIRON No, my good lord; I have sworn to stay with you:
And though I have for barbarism spoke more
Than for that angel knowledge you can say,
Yet confident Ill keep what I have swore
And bide the penance of each three years day.
Give me the paper; let me read the same;
And to the strictst decrees Ill write my name.
FERDINAND How well this yielding rescues thee from shame!
BIRON [Reads] Item, That no woman shall come within a
mile of my court: Hath this been proclaimed?
LONGAVILLE Four days ago.
BIRON Lets see the penalty.

a char array needs to be assigned to a string.

//assuming null termination
word[arraySize-1].assign(rawWord);

That is true too but i found my mistake. checkSame() member function returns 0 if it is a different from the words in word[] array. So i changed the if(test.checkSame()) to

if(!test.checkSame())

Thanks anyway.

This article has been dead for over six months. Start a new discussion instead.