String Problem error

Hey guys, I was wondering if you could help me out with this problem on my program. I ask the user to guess a letter. If the letter is not found in the string , i tell them that it's wrong. But that does not work the program stops and gives me a "debug assertion failed " error. Can somebody tell me how to fix this.
Thanks.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


string name="hello";
char letter;
int i;
string store[100];
int main ()
{
	cout<<"enter a letter : ";
	cin>>letter;

	for ( i = 0; i  < name.length(); i++ )
	{


		store[i] = name[i];

	}

	if ( letter != name[i] )
	{
		cout<<"wrong"<<endl;
	}


system("PAUSE");
return 0;



}
Last edited on
Think about this:

1
2
3
4
5
6
7
8
9
10
11
12
for ( i = 0; i  < name.length(); i++ )
	{


		store[i] = name[i];

	}

	if ( letter != name[i] )
	{
		cout<<"wrong"<<endl;
	}


Why would this not work? Try printing the number 'i' in your if statement and see what value you get. Remember that string is just an array of characters and the first character starts at index 0. So the first character in name is name[0] = 'h'
it does not work, it debugs fine but when you enter a word , the program just stops.

How about this.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


string name="hello";
char letter;
int i;
int main ()
{
	cout<<"enter a letter : ";
	cin>>letter;

	for ( i = 0; i  < name.length(); i++ )
	{


		if(letter==name[i])
		{
		    return 0;
		}

	}

    cout<<"wrong"<<endl;


return 0;



}
@hoity
it will not work in my case since im using the code i posted in a while(true) loop.
How about just setting a boolean if the letter is found?

Your problem in the original code is what Smac89 pointed out, you're trying to index past the region of code your string is allowed to access. Remember that the for loop will increment i once "after" it aborts. (It actually increments before and then aborts immediately because the condition is false)

Also, why are you using an array of strings to store single characters from the "secret word"?
Then again, why do you need to store the characters separately in the first case? You never use them afterwards.
And the test to see if the letter exists in the word isn't correct. You're only checking against the last character in the word.
BlackSheep how would you write this program I kind of get you but not quite.

Yes I know about the problem that was pointed out by Smack89.
The main struggle I have is if I put the "if" statement in the loop it will print "wrong" many times because the" I"keeps updating.

This is part of my code for a hangman game. And the part where the user inputs a wrong guess is whats giving me trouble.
I'd write a function that returns true or false based on if a letter is in a word you pass it. It'll modularize it nicely, you can just return a boolean to let you know if the letter is in the word or not.

To tackle the loop, if you create a function you can actually use hoity's code and modify it slightly to return true or false instead of 0 and printing "wrong".
Gonna break this down for you and show you exactly what is going on in your code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>//input/output
#include <string>//string class
#include <fstream>//file I/O

using namespace std;


string name="hello";//You have a string
char letter;
int i;
string store[100];//You allocated storage space for a 2-Dimensional array of strings 
//i.e. string within a string
int main ()
{
  cout<<"enter a letter : ";//Prompt for a letter
  cin>>letter;//User enters letter

  for ( i = 0; i  < name.length(); i++ )//For loop
  {


    store[i] = name[i];//The top part of the 2d array store the character in name
    //So store[0] = 'h', store[1] = 'e', store[2] = 'l'...etc

  }

  //At this point, i is 5
  if ( letter != name[i] )//check that letter is equal to name[5]
    //name[5] = null terminating character. It is the character at the end of every string
    //that signifies to the computer that that is the end of the string. It looks like this '\0'
  {
    cout<<"wrong"<<endl;//After checking that letter is not equal to null terminating character,
    //you print out the message "wrong"
  }


system("PAUSE");
return 0;//return success



}


Notice that no where in your code did you compare the letter you received to the letters in the string 'name'. You only checked it against a null terminating character and that is why you will always get the same message unless you do something different
Topic archived. No new replies allowed.