Thread: C string problem

  1. #1
    Registered User
    Join Date
    Oct 2008

    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

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

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

    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
    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 ?


  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    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, 03: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, 02:11 AM