Im barely learning C at my high school and I was wondering is there a way to print "\" and is there a way to compare a char string with a character.
What I mean is having a word and compare by a letter.
Im barely learning C at my high school and I was wondering is there a way to print "\" and is there a way to compare a char string with a character.
What I mean is having a word and compare by a letter.
Last edited by maniac20; 04-30-2008 at 05:17 PM.
To print a backslash you must put a doublequote before it:
The strncmp function can be used to compare the first n characters of two strings and returns 0 if same, <0 if less than (alphabetically), and >0 if more than, so for example:Code:printf(""\");
would return 0. However that example doesn't compare the whole word, just the first letter.Code:#include <string.h> int compareFirstLetter() { char *yourWord = "hello"; return strncmp(yourWord, "h", 1); }
are you using a book for learning C? there should be some kind of reference/appendix that gives an overview of the string.h library functions (where strncmp comes from)
Last edited by space-oddity; 04-30-2008 at 05:23 PM.
Last edited by maniac20; 04-30-2008 at 05:27 PM.
missing an endquote? if you want to print a backslash you just put one doublequote before it:
you want to compare the whole word with just one letter? what are you specifically wanting to check for? but yes, a loop will be required for something like that. (Unless you're dealing with just one word and you know how long that word will be, allowing you to use a multiple of that one letter ("hello" and "aaaaa") but that's unlikely.)Code:printf("Printing a backslash: "\ Done.");
do you know about the manual? type man printf or man strncmp or man "name of function" into your shell to find some useful information
Last edited by space-oddity; 04-30-2008 at 05:48 PM.
For me this is not working im getting "undefined reference to `WinMain@16' " error.
actually you don't have to use strncmp if you're just comparing for equality to one letter (but you don't know whether it's less than or greater than if it isn't equal). you can do something like:
that goes through all the letters of the word checking if it is the letter a, and if so does something..Code:int i = 0; char *yourWord = "Hello"; while (yourWord[i] != '\0'){ if (yourWord[i] == "a") { /*...do something...*/ } i++; }
there's some useful tutorials on this site if you want to look, http://www.cprogramming.com/tutorial.html
Last edited by space-oddity; 04-30-2008 at 06:04 PM.
You need a main() function, as that function is the one that automatically starts when your program is run.
You have your compiler set up to compile the program as a windows program. You need to change your compiler's settings so it compiles as a console program. int main() is the first function a console program starts with, WinMain() is the first function a windows program starts with.
Actually, you would use a backslash before it, not a double quote. The double quote will make the compiler think you've ended your text string and that is not what you want.Originally Posted by space-oddity
The backslash in a string makes the character immediately following it behave differently:
Code:printf("\\"); /* prints \ */ printf("\"); /* this is an error, there is no ending quote for the string */ printf("\""); /* prints " */ printf("""); /* (note: 3 double quotes in a row) is an error. */ printf("\t"); /* prints the tab character */ printf("t"); /* prints t */ printf("\0"); /* prints the NULL character */ printf("0"); /* prints 0 */ etc.
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "
I think space-oddity has things backwards.
Actually, to print a backslash, one would use:To print a backslash you must put a doublequote before it:
Code:printf(""\");
You see, the compiler starts processing a string when it finds a double quote. It stops processing the string when it finds another double quote. If you want to put a double quote inside a string, therefore, you have to use \". Since the backslash is used to "escape" characters like this, to include a literal backslash you must escape the backslash itself with \\.Code:printf("\\");
I'm probably just being confusing. http://www.cppreference.com/escape_sequences.html
[edit] Beaten. [/edit]
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.
The escape character for strings isn't a double quote, it's a backslash.
edit: soooo slow....
ahh yes that's right, \\ instead of "\, i knew that but for some reason muddled up. i think i was thinking of the case when you want to output doubequotes and you write \" my mistake.
sorry for the confusion maniac.
thank you guess alot,im currently creating a hangman that requires that.Instead of creating another post can you guys help me on my game.Do any one see anything wrong with it?Is there a way to go back to the top after you win without using goto?
Code:#include<stdlib.h> #include<stdio.h> #include<string.h> int main(void) { int select,x,y,w,z=0; char ans[30],see[30],guess[30],layer1[5]="|",layer2[5]="|",layer3[5]="|"; printf("Please enter the answer\n"); gets(ans); system("cls"); for(x=0;x<strlen(ans);x++) { if(ans[x]==' ') see[x]=' '; else see[x]='-'; } printf("1)Guess\n2)Solve\n"); scanf("%d",&select); system("cls"); if(select==1) { for(x=0;x<6;) { getchar(); printf("____\n| |\n%s\n%s\n%s\n====",layer1,layer2,layer3); printf("\n\n"); for(w=0;w<strlen(ans);w++) { printf("%c",see[w]); } printf("\n"); printf("Please select a letter\n"); scanf("%c",&select); system("cls"); for(y=0;y<strlen(ans);y++) { if (ans[y]!=select) z++; else see[y]=select; if(strcmp(ans,see)==0) { printf("You won"); break; } } switch(z){ case 1:layer1[2]='O'; break; case 2:layer2[2]='|'; break; case 3:layer2[1]='/'; break; case 4:layer2[3]='\\'; break; case 5:layer3[1]='/'; break; case 6:layer1[3]='\\'; } } } if(select==2){ printf("Guess\n"); gets(guess); getchar(); if(strcmp(ans,guess)==0) printf("\nYou won"); } system("pause"); return 0; }
Game code usually has a lay out like this:
main()
calls menu()
user makes his/her selections
calls initialize() based on those selections
big game loop
game loop is basically:
The game loop may be in a called sub function, from menu(). Effect is just the same.Code:while(1) //setting up an infinite loop game code play in here, or called from inside this loop if(game_over || quit button clicked || quit key pressed) break }
So everything flows in a loop, with no goto's. Goto's are ok when you need to escape from deep inside nested loops, but not to go from one function into another. They make your code very difficult to de-bug/improve, and are a real invitation for a memory leak that will totally wreck your game, if you're not very careful.
Get the flow control right, and dump the goto's.
The easiest thing would probably be to encapsulate the game itself into a separate function, then in main() you would run a loop until the user entered no (response to "Play again?"). Each time through the loop you would call the function that runs the game.
Something like:
[edit]too slow. [/edit]Code:int main() { char yn = 'y'; while (yn == 'y') { play_game(); printf("Play again? (y/n)"); scanf("%c", &yn); } return 0; }
abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "