Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

make for-loop stop when it reaches the end of a string

Status
Not open for further replies.

brunofeu

Programmer
Nov 24, 2001
3
BR
Question 1)

#include <iostream.h>

void main (void){
char word [50];
cin >> word;
for (int i=0;word!='ENTERLETTERHERE';i++){
cout << word << &quot; &quot;;
}
}

there should be some letter or maybe a \letter that, when replacing the ENTERLETTERHERE part of this code, works. What I want to do is this: If I type in Bruno, the output should be &quot;B r u n o&quot;. It stops the loop right after the last letter.

-----------------------------
Question 2)

struct stcPalavra{
char item [50];
struct stcPalavra *esq, *dir, *linhas;
};

// cutted code

void classIndice :: insere (char elemento [], stcPalavra * pont){
if (pont == NULL){
pont->item = elemento;
pont->esq = pont->dir = NULL;
}
else if (strcmp(elemento, pont->item) < 0) insere (elemento, pont->esq);
else if (strcmp(elemento, pont->item) > 0) insere (elemento, pont->dir);
else cout << &quot;Elemento ja existente!\n&quot;;
};


The problem in this code is in the line: &quot;pont->item = elemento&quot;... the compiler says that it cannot convert a char[] to a char[50] (that's why I made the first question). Is there a way of doing this without having to make a loop that copies letter to letter until all the word is copied?


Thanks for your time,
Bruno Feu

PS: The code is in Portuguese, I hope that is not a problem to you guys. :)
 
Question #1:

#include <iostream>

int main()
{
char sName[] = &quot;BRUNO&quot;;
char cTemp = 0;
int iIndex = 0;

do {

cTemp = sName[iIndex++];
std::cout << cTemp << &quot; &quot;;

} while ( cTemp != '\0' );

return 0;

}

Question #2:

#include <iostream>

int main()
{
char sName[] = &quot;BRUNO&quot;;
char sString[20];

strcpy( sString,sName );
std::cout << sString;

return 0;

}

Or if you want to make life easier:

#include <iostream>
#include <string>

int main()
{
std::string sName = &quot;BRUNO&quot;;
std::string sString;

sString = sName;

std::cout << sString;

return 0;

}



Mike L.G.
mlg400@blazemail.com
 
The problem is: I cannot (or don't know how to) use string inside this, the compiler does not recognize it. It says &quot;std is not a class member&quot;. If I just use &quot;string item;&quot;, it says thay there's a semicolon missing between string and item. That makes me use char[50].

Also, it is not recognizing &quot;strcmp(string1,string2) <0&quot;, although it recognizes &quot;strcmp(string1,string2)&quot;. When I compile it asks me where is the include to that file.


class classIndice {
struct stcPalavra{
char item [50];
struct stcPalavra *esq, *dir, *linhas;
};
(...)
 
I just found out why is this error happening:

it cannot find this files:

-strcat.asm
-vsprintf.c


does anyone knows how to solve this problem?

Thank you for your time,
Bruno Feu
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top