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;
}
tux4life commented: Well done on the code tags, for your first post. +8

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.

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.