C string problem

This is a discussion on C string problem within the C Programming forums, part of the General Programming Boards category; Hello guys i just started learning C 1 week ago i m trying to do a program for guessing i ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    12

    C string problem

    Hello guys i just started learning C 1 week ago i m trying to do a program for guessing i done it but i wanna try make it like if user put 3 times wrong program will close i did but i have 2 errors in them >>thanks in advanced for your help

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main(void){
    char szKey[100]="mangos";
    char inPut[100];
    
    do{
    	printf("Please Enter your favorite fruit:   ");
    	fgets (inPut, 100, stdin);
    }
    while(strcmp(szKey,inPut));
    puts("Correct answer");
    if(inPut==3)
    exit(0);}return 0;}

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You will need to remove the newline that comes along in fgets().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    12
    yes thanks didnt notice that i fixed that also exit function didnt need a brace after it but problem is it keeps exuting even after 3rd line how can i make at at 3rd question it will exit ?


    genie

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    You'll need a counter variable to keep track of how many times you've gone through the loop. Initialize it to 0 outside the loop and then increment it each time you go through the loop. You can alter the conditional part of the loop to include a check of this value (such that it will exit said loop once the count is 3) or you can put a check inside the loop after the increment step which calls break to exit the loop.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 01:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21