Join 137,176 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,373 people online right now. Registration is fast and FREE... Join Now!
if my program reads the following strings(few sentences) as command line arguments into the program: "I have just seen Sirs John and Nick!" "I have just met Sirs Mike, Paul and chris."
from the program i want to only take and output the names come after Sirs or Sir. in this case it would be John, Nick, Mike, Paul and chris. below are some codes that i am trying to code but still stuck. and also the sentences are stored using array of pointers, some word like Mike and Nick contains a full stop and exclaimation character. how do i get rid of those characters when i store the name? is it a good idea to store those name into a char buffer[80] as i want to do sorting later? if Mike has been store from the first sentence, the rest of the sentence comes after also has a Sir Mike, how do i counter check with the existing array, if Mike already existed then don't assign into it?
char sirs[80]; int i, j, k = 0; for (i = 1; i < argc; i++) { for (j = 1; j < argc; j++) { if (argv[i] == 'Sir') sirs[k++] = argv[i + 1]; else if (argv[i] == 'Sirs' && j > i && argv[j] != 'and') sirs[k++] = argv[j]; else if (argv[i] == 'Sirs' && j > i && argv[j] == 'and') sirs[k++] = argv[j + 1]; break; } }
for (int x = 0; x < k; x++) printf("%s ", sirs[x]);
system("PAUSE"); return EXIT_SUCCESS; }
but i get the below error msg and warnings upon compiling. pls advise. \solve.c:26:28: warning: multi-character character constant In function `main': warning: comparison between pointer and integer warning: assignment makes integer from pointer without a cast warning: multi-character character constant warning: comparison between pointer and integer warning: multi-character character constant warning: comparison between pointer and integer
warning: assignment makes integer from pointer without a cast warning: multi-character character constant warning: comparison between pointer and integer warning: multi-character character constant warning: comparison between pointer and integer warning: assignment makes integer from pointer without a cast
int get_sirs(char *sirs[]); void sort(char *p[], int n); void print_sirs(char *p[], int n);
char all_sirs[25]; char *sirs[MAXSIRS];
int main(int argc, char *argv[]) {
int number_of_sirs; int i, j, k = 0, m;
/* To find out the number of Sirs involved in the puzzle */ for (i = 1; i < argc; i++) { for (j = 1; j < argc; j++) { if (argv[i] == "Sirs" && j > i && argv[j] != "and") all_sirs[k++] = *argv[j]; else if (argv[i] == "Sirs" && j > i && argv[j] == "and") all_sirs[k++] = *argv[j + 1]; break; } }
/* To find out the number of Sir involved in the puzzle */ for (m = 1; m < argc; m++) if (argv[m] == "Sir") all_sirs[k++] = *argv[m + 1];
number_of_sirs = get_sirs(sirs);
if (number_of_sirs < 0) { printf("Memory allocation error.\n"); exit(-1); }
A char can only store one character. You use a pointer to a list of chars. You should first allocate memory for it (using the function new) before copying the data into it, terefore you get (null) as output now in your code.
I think it is a much easier solution to use a vector of strings:
as i have changed this part of code to allocate memory storage for the input sentences. but the output is "The Sirs are: " as no name appear as output.
FYI, the input sentences that i use for command line argument is "I have just seen Sirs Mike and Chris! \"I am a Knave,\" whispered Sir Eleonore. Who is a Knight and who is a Knave?"
/* To find out the number of Sirs involved in the puzzle */ for (i = 1; i < argc; i++) { for (j = 1; j < argc; j++) { if (puzz[i] == "Sirs" && j > i && puzz[j] != "and") all_sirs[k++] = *puzz[j]; else if (puzz[i] == "Sirs" && j > i && puzz[j] == "and") all_sirs[k++] = *puzz[j + 1]; break; } }
/* To find out the number of Sir involved in the puzzle */ for (m = 1; m < argc; m++) if (puzz[m] == "Sir") all_sirs[k++] = *puzz[m + 1];
yes i have adjusted that by including the opening and closing curly bracket in the for loop. before that the output was "The Sirs are: (null)". now the output is "The Sirs are: " as still no name appear.
i think the problem is here but i don't know how to fix it. here is the explanation.
the problem is in array of pointers, in a for loop when i reach a particular word let say "Sirs", then i want to store the word next to it into an array. so i have to use for loop to read the whole sentence word by word. the sentence is the command line argument, when the program runs it will all be stored into char *argv[]. so i need to look for the "Sirs" or "Sir" word along the sentences using for loop to loop through char *argv[]. but if i write like the code as mentioned above, it compares with the pointer address, not with the string pointed by the pointer address. is there anything wrong in the code? pls advise. thanks in advance.
FYI, if i write the below code, it can print out all the strings word by word in each element of the array pointed to by the pointer address, but how come when i do like above code, it will only compare with the pointer address?
CODE
for (int x = 1; x < argc; x++) printf("%s", argv[x]);