-
April 9th, 2013, 03:01 PM
#31
Re: Help with C simple loop.
Originally Posted by Paul McKenzie
As to your code:
Code:
const char * getServer(char *svrList)
{
char temp[512];
strcpy (temp,svrList);
What if svrList points to a string that is more than 512 bytes? What if I pass a NULL to getServer()? Your code is now broken.
What is returned if the code falls through to this line that you commented out? The return value is now undefined.
Regards,
Paul McKenzie
As far as the more than 512 bytes, for what I am doing in this program and knowing that arrays cannot be dynamic in nature for 'C', I think my best bet is to just control the data that is in that array to make sure that a NULL or more than 512 bytes are not fed into the function. I know this is not the best way, but for this assignment, it will get me by.
Sorry about that, I changed it to return NULL; and changed my if statement below that.
As of right now, everything is working and the debugger helped a great deal! The only issue I am having now is:
I can't get this loop to break when I enter "N" at the prompt. It seems to completely skip the if true and false values and just continue running the while(1) loop. Am I missing something simple? I am just trying to continue the loop until the user enters anything other than Y or y. In the debugger, the value is getting put in "cont", but it just skips the continue or break on that if statement.
Code:
while(1)
{
printf("Checking which server to use.\n");
if (getServer(svrList) != "0")
{
server_name= getServer(svrList);
printf("The server is %s\n",server_name);
printf("Do you want to continue? [Y/N]");
fscanf(stdin,"%s", &cont);
printf("%s\n", &cont);
if (cont = "Y" || "y")
continue;
else
break;
}
else
{
printf("There are no available servers!\n");
break;
}
return 0;
}
-
April 9th, 2013, 03:04 PM
#32
Re: Help with C simple loop.
if (cont = "Y" || "y")
That's not how if statement works. Each side of the || is evaluated as an independent expression. "y" is a standalone expressions, and since it is non-zero, the if statement will always evaluate to true.
Why hot modify your while statement to look at cont, rather than have it continue indefinitely? while(1) is usually bad form.
-
April 9th, 2013, 03:24 PM
#33
Re: Help with C simple loop.
Originally Posted by GCDEF
if (cont = "Y" || "y")
That's not how if statement works. Each side of the || is evaluated as an independent expression. "y" is a standalone expressions, and since it is non-zero, the if statement will always evaluate to true.
Why hot modify your while statement to look at cont, rather than have it continue indefinitely? while(1) is usually bad form.
GCDEF, you are da man! I guess I just don't have the critical thinking to be able to handle some of this. That solution worked. I think that will do it. I know if I want to know best practices or any more help, I will definitely be back and will highly recommend this site to anyone! Can't thank you guys enough!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|