for the love of all that is good and holy, please remove those foul GOTO statements.
the answer is easy enough, but i cant even bear to look at this code as long as there's a GOTO in there.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
any sort of conditional statement is fine ... "while" is a common one to use
so instead of, say, doing this:
A: printf("enter name : ");
scanf("%s", gotName);
... stuff ...
if (strcmp(gotName,"quit") == 0)
goto A;
do something like this:
int isDone = 0;
char gotName[32];
while ( ! isDone)
{
printf("enter name ");
scanf("%s", gotName);
if (strcmp(gotName,"quit") == 0)
isDone = 1;
else
{
... stuff ...
}
}
seriously though. im not being facetious. gotos make me ill. the use of gotos should absolutely never be done, unless there is a really, really compelling reason to do so. And I can't think of any.
they should never have been allowed to be included into the language.
other options are using "for" loops and "do/while" loops ... related useful commands include "continue" and "break" which cause the loop to be immediately run again, or immediately broken out from, respectively.
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
now here's something that might be of interest to you.
char names[3][32]; // array of 3 names, each being a string of up to 32 chars
int index =0;
...
while (index<3) {
printf("enter name #%d : ",index);
scanf("%s",names[index]);
index++;
}
...
for (index=0; index<3; index++) {
printf("name #%d : ",name[index]);
}
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
Maybe the following clarifies it
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char** p = NULL;
const int numStrings = 2;
char string[21];
p = (char**)malloc(sizeof(char **) * numStrings);
for(int nn = 0; nn < numStrings; ++ nn)
{
sprintf(string, "string %d\0", nn);
p[nn] = (char*) malloc(strlen(string) + 1);
strcpy(p[nn], string);
}
for(nn = 0; nn < numStrings; ++ nn)
{
printf("string %d = [%s]\n", nn, p[nn]);
// free the memory as we go
free(p[nn]);
}
// finally free memory pointed to by p
free(p);
return 0;
}
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
I kno how to print a static array, but I dont know why I cannot wrap my head around this dynamic array with pointers concept.
oh, whoops. sorry. i missed where you said that this was a dynamic array.
i didnt actually read your code. i still can't see past those big ugly GOTO's
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
>the use of gotos should absolutely never be done
A more reasonable guideline is simply to avoid goto until you're confident that you completely understand it and its alternatives. Sadly, too many people take the "never use goto!" as some sort of silly religious mantra and don't bother to actually learn about the feature or consider it as an option.
>unless there is a really, really compelling reason to do so. And I can't think of any.
Don't take this the wrong way (or do, I don't much care), but if you can't think of any legitimate reasons to use goto, you're not qualified to tell people never to use it.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Sadly, too many people take the "never use goto!" as some sort of silly religious mantra
YOU SHALL PAY FOR YOUR INSOLENCE, BLASPHEMOUR!
but if you can't think of any legitimate reasons to use goto, you're not qualified to tell people never to use it.
actually, i have a confession. ive used GOTOs recently in the context of error handling for hardware drivers, following a convention that was established by National Instrument's source code. i swallowed my distaste because it was rather simple and concise in the way it was structured.... but i didnt want to get into that here.
to see a simple input routine run by GOTOs is just painful. Truly, theres nothing that GOTOs do that cant be done another, equally good and probably better, way. using GOTOs in professional work isusually a sign of either incompetence or laziness, so the last thing people should be teaching students is to use GOTO.
Now when you have the misfortune of inheriting some 5 year old code of 50,000 lines and hundreds of functions, and some fargin bastage has gone and put GOTOs and Globally scoped variables all over the eff-ing place --- it makes you want to hunt down Kernigan and Ritchie and ask them WTF's idea was it to allow GOTO in the first place, even against the advice of many industry professionals.
.
jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179