Desperately Need Help

This is a discussion on Desperately Need Help within the C Programming forums, part of the General Programming Boards category; MY PROBLEM FIXED TY...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    55

    Desperately Need Help

    MY PROBLEM FIXED TY
    Last edited by Astra; 10-21-2006 at 10:32 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Double quotes are for strings. Single quotes are for single characters. You can't compare strings with the equality operator. You can't store a string in a single character*. You don't flush input streams either.


    Quzah.

    *Actually you can, but the "string" will consist of only a single nul character.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    Thanks for the help (edited code in first post now) i don't get any warnings now! However, with the flushing, i had to use it for the first loop because the keyboard was thinking the input was the same as the last time i ran it. Do i not need to use it? Or do i only need to use it on the loops with integers?

    My program still isnt looping the entire events, maybe i am having problems with xAgain?
    Not quite sure why it doesnt repeat it. :S

    Thanks in advance if you can help me out here

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > fflush(stdin);
    Read the FAQ.

    Use fgets() to read ALL input.
    Messing about with scanf() is just more trouble than it's worth in the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    okay thanks for the advice, i am learning about fgets() now. If i use fgets() will i still have to test it like i did with scanf? Can i still use printf with the fgets or should i use puts? OR should i use printf for the numbers and puts for the chars?
    Also, fgets(char *, int , FILE *); << apparently this is the syntax, does the FILE bit mean the memory address of the pointer? does the int mean the buffer size of the char array? (in this case 2?) does the char * mean the char pointer? (why does it even need a pointer?)

    The following code is WRONG, but why?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char *cYesNo[2];
    
    	printf("Enter Y or N");
    	fgets(*cYesNo,2, stdin);
    	printf("You typed: %s\n", cYesNo);
    
    	return 0;
    }
    And btw, i can't find the relevant information on fgets on the internet or in the FAQ. Which is why im asking.
    Last edited by Astra; 10-21-2006 at 05:46 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have an array of pointers to characters. Remove the *.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    Sorry for constantly asking questions, but as soon as i've got my head around how to make this work in the same way as if it was scanfs everything will be good

    okay, below i've included a small portion of the program because i don't need to paste it all:

    the problems i am getting are all 'Warnings':
    -iAge being used without having been initialized (in the fgets line)
    (i wasn't aware i had to initialize it as it is just a null variable until
    the user inputs)
    -char differs in levels of indirection from const int (talking about iTestScan)
    (i know that i should not compare these two but, how else can i test to
    see if the input was the correct type?)

    Thanks in advance if you take the time to help me

    Code:
    int main(void)
    {
    	int iAge, iClientPre25, iClientPost25, iClaimNum, iClaimRecent01, iTestScan, xAgain;
    	char cClaimYN[2], cContinueStop[2];
    	
    	iClientPre25 = 0;
    	iClientPost25 = 0;
    	iClaimNum = 0;
    	
    	do
    	{
    		do
    		{
    			printf("What is the age of the client?:");
    			iTestScan = fgets(iAge,sizeof iAge,stdin);
    			fflush(stdin);
    		}while (iTestScan == 0);

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    i wasn't aware i had to initialize it as it is just a null variable until
    the user inputs
    It's not null. It's uninitialized. That mean it has some seemingly random value in it.
    Code:
    iTestScan = fgets(iAge,sizeof iAge,stdin);
    The first argument to fgets is supposed to be a buffer of characters you read your input into. They you pull what you need out of it. You should read the FAQ on it again.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Code:
    char buff[BUFSIZ];
    while ( iTestScan == 0 &&
            fgets( buff, sizeof buff, stdin ) != NULL ) {
      int temp;
      if ( sscanf( buff, "%d", &temp ) == 1 && temp != 0 ) {
        iTestScan = temp;
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    55
    Should i use strtol() in my program to turn the input from fgets to integers on the questions where the user is asked to input age?

    is this correct usage of what Salem coded:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    	char buff[4];
    	int iTestInput;
    
    	do
    	{
    	printf("Please enter your age: ");
    	iTestInput = fgets(buff, sizeof buff, stdin);
    	}while (iTestInput == 0 && fgets(buff, sizeof buff, stdin) != NULL) ;
    
    	if (buff < 18)
    	{
    		printf("young");
    	}
    	else if (buff >= 18)
    	{
    		printf("not young");
    	}
    	
    	return 0;
    }
    Last edited by Astra; 10-21-2006 at 08:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C-- Help Desperately Needed
    By toadkiwi in forum C Programming
    Replies: 10
    Last Post: 02-14-2008, 10:08 PM
  2. Replies: 2
    Last Post: 05-27-2005, 05:05 PM
  3. Color ListBox (Need help desperately pls....)
    By wayne in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2005, 04:02 AM
  4. Student desperately needing help!
    By pinkpanther1586 in forum C Programming
    Replies: 4
    Last Post: 10-26-2003, 07:46 AM
  5. I desperately need a project..........
    By frenchfry164 in forum Game Programming
    Replies: 13
    Last Post: 07-03-2003, 11:45 AM

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