end in a loop

N

nick048

Hi to all,
In a little program, I have this code:

i=0;
receved = recv(new_sd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;
receved = recv(new_sd, &c, 1, 0);
}
msg='\0';

My program work fine; but i need change the end from '\n' to '**'[/
b]. How can modify my code in order to exit from while when the
terminator '**' is received ?

I hope in your help.
Thank You and best regards

Nick
 
K

Klarth

Hi to all,
In a little program, I have this code:

i=0;
receved = recv(new_sd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;
receved = recv(new_sd, &c, 1, 0);
}
msg='\0';

My program work fine; but i need change the end from '\n' to '**'[/
b]. How can modify my code in order to exit from while when the
terminator '**' is received ?

I hope in your help.
Thank You and best regards

Nick


You would also check have to check the character of msg[i-1] to see if
it is '*' on each loop. However, simply adding && (msg[i-1] != '*') to
the while condition can cause problems on the first iteration,
especially if c gets initialised to '*'. Getting around this other
problem should be easy though.
 
S

santosh

nick048 said:
Hi to all,
In a little program, I have this code:

i=0;
receved = recv(new_sd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;
receved = recv(new_sd, &c, 1, 0);
}
msg='\0';

My program work fine; but i need change the end from '\n' to '**'[/
b]. How can modify my code in order to exit from while when the
terminator '**' is received ?


char prev = '\0';

/* ... */

i = 0;
while (((receved = recv(new_sd, &c, 1, 0)), c != '*') || prev != '*')
{
msg[i++] = prev = c;
}
 
N

nick048

nick048 said:
Hi to all,
In a little program, I have this code:
i=0;
receved = recv(new_sd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;
receved = recv(new_sd, &c, 1, 0);
}
msg='\0';

My program work fine; but i need change the end from '\n' to '**'[/
b]. How can modify my code in order to exit from while when the
terminator '**' is received ?


char prev = '\0';

/* ... */

i = 0;
while (((receved = recv(new_sd, &c, 1, 0)), c != '*') || prev != '*')
{
msg[i++] = prev = c;



}- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -- Nascondi testo tra virgolette -

- Mostra testo tra virgolette -


Tank You for help.
 
S

santosh

nick048 said:
nick048 said:
Hi to all,
In a little program, I have this code:
i=0;
receved = recv(new_sd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;
receved = recv(new_sd, &c, 1, 0);
}
msg='\0';

My program work fine; but i need change the end from '\n' to '**'[/
b]. How can modify my code in order to exit from while when the
terminator '**' is received ?


char prev = '\0';

/* ... */

i = 0;
while (((receved = recv(new_sd, &c, 1, 0)), c != '*') || prev != '*')
{
msg[i++] = prev = c;


Tank You for help.


Sorry but that won't work. Try this:

char prev = '\0'

/* ... */

i = 0;
receved = recv(new_sd, &c, 1, 0);
while (c != '*' || prev != '*')
{
msg[i++] = c;
}
 
S

santosh

santosh said:
nick048 said:
nick048 wrote:
Hi to all,
In a little program, I have this code:

i=0;
receved = recv(new_sd, &c, 1, 0);
while (c !='\n')
{
msg[i++]=c;
receved = recv(new_sd, &c, 1, 0);
}
msg='\0';

My program work fine; but i need change the end from '\n' to '**'[/
b]. How can modify my code in order to exit from while when the
terminator '**' is received ?

char prev = '\0';

/* ... */

i = 0;
while (((receved = recv(new_sd, &c, 1, 0)), c != '*') || prev != '*')
{
msg[i++] = prev = c;


Tank You for help.


Sorry but that won't work. Try this:

char prev = '\0'

/* ... */

i = 0;
receved = recv(new_sd, &c, 1, 0);
while (c != '*' || prev != '*')
{
msg[i++] = c;


Add here:
receved = recv(new_sd, &c, 1, 0);

That's it, I'm taking a break.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top