Thread: Beginner Help

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Beginner Help

    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.

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    To print a backslash you must put a doublequote before it:
    Code:
    printf(""\");
    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:
    #include <string.h>
    
    int compareFirstLetter() {
        char *yourWord = "hello";
        return strncmp(yourWord, "h", 1);
    }
    would return 0. However that example doesn't compare the whole word, just the first letter.


    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Thumbs up

    Quote Originally Posted by space-oddity View Post
    To print a backslash you must put a doublequote before it:
    Code:
    printf(""\");
    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:
    int compareFirstLetter() {
        char *yourWord = "hello";
        return strncmp(yourWord, "h", 1);
    }
    would return 0. However that example doesn't compare the whole word, just the first letter.
    I think you are missing a end quote and I need the whole word to be compared.Would it work in a loop?
    int compareFirstLetter() {
    char *yourWord = "hello";
    int counter;
    for(counter=0;counter<strlen(yourWord);counter++)
    {
    strncmp(yourWord, "h", counter);
    }
    Last edited by maniac20; 04-30-2008 at 05:27 PM.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    missing an endquote? if you want to print a backslash you just put one doublequote before it:
    Code:
    printf("Printing a backslash: "\ Done.");
    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.)

    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.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    For me this is not working im getting "undefined reference to `WinMain@16' " error.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    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:

    Code:
    int i = 0;
    char *yourWord = "Hello";
    while (yourWord[i] != '\0'){
        if (yourWord[i] == "a") {
            /*...do something...*/
        }
        i++;
    }
    that goes through all the letters of the word checking if it is the letter a, and if so does something..

    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.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    Quote Originally Posted by maniac20 View Post
    For me this is not working im getting "undefined reference to `WinMain@16' " error.
    im not sure why that's happening sorry, you'll have to wait for someone else to reply on that

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need a main() function, as that function is the one that automatically starts when your program is run.

  9. #9
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Quote Originally Posted by maniac20 View Post
    For me this is not working im getting "undefined reference to `WinMain@16' " error.
    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.

    Quote Originally Posted by space-oddity
    To print a backslash you must put a doublequote before it
    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.

    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 "

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think space-oddity has things backwards.
    To print a backslash you must put a doublequote before it:
    Code:
    printf(""\");
    Actually, to print a backslash, one would use:
    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 \\.

    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.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    The escape character for strings isn't a double quote, it's a backslash.

    edit: soooo slow....

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    23
    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.

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    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;
    }

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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:
    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
    }
    The game loop may be in a called sub function, from menu(). Effect is just the same.

    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.

  15. #15
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    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:

    Code:
    int main()
    {
      char yn = 'y';
    
      while (yn == 'y')
      {
        play_game();
        printf("Play again? (y/n)");
        scanf("&#37;c", &yn);
      }
    
      return 0;
    }
    [edit]too slow. [/edit]
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  2. What are some good beginner programs I shouold make?
    By oobootsy1 in forum C# Programming
    Replies: 6
    Last Post: 08-09-2005, 02:02 PM
  3. Books, Beginner, MustHave
    By Zeusbwr in forum C++ Programming
    Replies: 9
    Last Post: 10-25-2004, 05:14 PM
  4. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM