Is there a way to find if a number is in an array more than once? How would I go about doing this? Thanks for the help.
Is there a way to find if a number is in an array more than once? How would I go about doing this? Thanks for the help.
Iterate through the whole array and have a variable that increments for each time it finds that an element equals a certain number.
Okay, I figured that out. Thanks for the help. I have a new problem though, which should be easy for anyone who isn't me to figure out. I have the user enter his first and last name and favorite number, and then have the program print it out. However, It will not say "Hello, First name Last name!" but rather it will display the first name, then the second name on a new line.
Code:printf( "Please enter your first name:\t " ); fgets(str, 20, stdin); printf("\nPlease enter your last name:\t"); fgets(lastname, 20, stdin); printf("\nWhat is your favorite number?\t"); scanf("%d", &number); printf("Hello, %s %s! I see that your favorite number is %d!\n", str, lastname, number);
Also, I need to use fgets because I am using pointers for another part.
If it fits, fgets will also read the \n into the buffer, that's why the last name is being printed on the other line. There are many ways to get rid of the \n. I personally do it like this:
Code:#include <string.h> char *p; if ((p = strchr(str, '\n'))) *p = '\0';
Ah! Thanks a lot!
As well as getting into the habbit of checking that the pointer doesn't equal NULL before trying to utilize it.Originally Posted by tcpl
Granted from getting input from the user there should be a new line character but getting into the habbit is good because if you're reading from a user supplied file killing new line characters in that method without checking first can bite you.
That's what the if statement does, makes sure p isn't NULL.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Oops should of looked at the code closer before posting -.- sorryOriginally Posted by dwks