I cannot find my mistake :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

SECOND FUNCTION
int k=o;
while(k<(set.length()-1) && x==false && z==false)  // this function checks if there is any duplication in set
	  {
		int y=set.find(set.substr(k), (k+1));     k++; 
        if(y!=string::npos)
		{
			cout<<"You cannot repeat a character in your set!"<<endl;
			cout<<name<<", please enter another character set: "; cin>>set;  IfExit(set, x); z=true;
		}
		else
			x=false;
	  }

NOTES:
-Conditions of functions are true. In big function, which these little ones are involved, i check a given string if it has any punctuations, numbers, duplications etc. if there is, i ask user to re-enter another set.

-i used a given header file and a cpp file, they are both called strutils.


-IfExit() is a function which i wrote in my own cpp file. way it works is whenever user enters something like eXit, EXIT, ExiT etc, program shuts off, it changes x's value to true or false in terms of keeping program run or shut it down. I also checked it, it works correctly.


I figured out what was wrong in first func. :)

In sec. func. i check every single letter, except last one, if there is any other same letter. ps: i only check right side of the chosen letter. i.e set is "asdfgh", lets say we are checking d: function starts checking from f, at least what i tried to do was this.


i tried to change their inner condition(yet i still believe i wrote them down true:p). sometimes i did make them work but they worked in a wrong way. i.e i entered "ead" and program said u cannot repeat a character in your set!

Also i am having problems with storing words which i ask user to enter in a while loop. i defined 2 string: wordlist and word. i ask user to enter a word in a while function, if it is entered correctly, i ask for another till user enters "end". i defined wordlist+=word. yet i think it stores words as one. i couldn't figure out how to store them as word 1, word 2 -or in any other way- to use them separately after on the program.

any help will be appreciated:)
Last edited on
This statement

int y=set.find(set.substr(k), (k+1));

will return always std::string::npos because set.substr( k ) is always greater than the substring in which the search is being done.
You could always store each word as an element of a string vector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <vector>

string resp = "y";
vector<string> word;
string temp;
while (resp=="y") {
	cout << "Enter a word";
	getline(cin, temp);
	word.push_back(temp);
	cout << "Would you like to enter another word?";
	getline(cin, resp);
}
for (int i=0; i<word.size(); i++) {
	cout << word[i];
}
Last edited on
@vlad from moscow, how can i fix it? i mean i can't think of any other function to check duplication. also i thought that if there is no equivalent for string.find() function, it returns string::npos yet you are saying that if i try to find k th letter after (k+1) th letter, it will always return string::npos. now i'm confused about what string::npos is:)



@rcast, we haven't learn to use vector library and i don't think professor will accept it in my work. could u gimme any other idea within the string library?
I think you should check in a loop that a next letter of the string is not found any more.

1
2
3
4
5
6
bool IsDuplicate = false;

for ( std::string::size_type i = 0; !IsDuplicate && set.size() - i > 1; i++ )
{
   IsDuplicate = set.find( set[i], i+1 ) != std::string::npos;
}




Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
int count = 0;
cout << "Enter the number of words you would like to enter: ";
cin >> count;
cin.ignore();
string* word = new string[count];
for (int i=0; i < count; i++) {
   cout << "Enter word " << i+1 << " now: ";
   getline(cin, word[i]);
}

for (int i=0; i < count; i++) {
	cout << word[i] << " ";
}
Topic archived. No new replies allowed.