Skip to main content
gamedev.net
🔒 Locked

clearing strings question

Started by omegasyphon Nov 12, 2000 at 5:47 PM 2 replies 500+ views
Original Post
omegasyphon
omegasyphon
ok my question is i have a loop like so i have a string declared and i input a 10 letter wo.rd now, when i go to run it over and over how do i clear the string so that it doesnt skip the function call to input the string?
omegasyphon
omegasyphon
ok heres my code:
when i run it allows you to enter a word the first time and then a char but the second time it skips the option of entering a word because i think the string still contains the word from the first time and i dont know why



void main()
{
char word[11] ="";
char wordl = ''Y'';
char letter = '' '';

while (wordl == ''Y'')
{

enterword(word);
while (letter != '';'')
{
enterletter(letter);
check(letter,word);
}
contword(wordl);
letter = '' '';
}
}

void enterword(char word[])
{
cout<<"Enter a 10 letter word"< cin.getline(word,11);
}

void enterletter(char &letter)
{
cout<<"Enter a letter or a semicolon to quit"< cin >>letter;
}

void check(char letter,char word[])
{
int cntr=0;
int x;
for (x=0;x<11;x++)
{
if (letter==word[x])
cntr+=1;
else
if (word[x]==''\n'')
cntr=15;
}
if (letter !='';'')
{
cout<<"The word you entered is: ";
for(x=0;x<11;x++)
{
cout< }
cout<<""< cout<<"the letter you entered to count is: "< if (cntr==1)
cout<<"there is one letter "< else
if (cntr>1)
cout<<"there is "<
}
}

void contword(char &wordl)
{
cout<<"enter a Y or N to continue if you want to enter a new word"< cin>>wordl;
wordl = toupper(wordl);
}




Ampere
Ampere
According to the loops in your main() function, you ask for a word, then keep asking for letters until a '';'' is entered. Then if you enter a ''Y'', it''ll ask you for a word again. Is that the behavior your getting?

Btw, it''d be a little cleaner if you made the loops do-while and eliminated the two letter = '' '' lines and the word1 = ''Y'' line. In case you care.

Oh, and does that first line in main do what you think it does? It seems to me that it would allocate an 11 char buffer, and then assign word to the address of a null character, losing the first buffer. I doubt that''s the behavior you''re looking for.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.