Thread: string input issue

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    16

    string input issue

    Hey all,

    I am having an issue with my final programming assignment. Basically we have to create a program that collects a course code, subject name and 2 marks for the subject and write them to a file. I can do this without to much difficulty except for the subject name, if I try to input spaces the program skipps over the last two scanf statements and seem to loop at my menu screen (in a function not shown here) if I don't input spaces it works fine. Depending on how i modify the highlighted line below the program will sometimes skip right over that input screen and move on to the next.

    Code:
    int addSubject()
    {
    	//declare local variables
    	int i = 0;
    	int j = 0;
    	char courseCODE[8];
    	char subjectNAME[31];
    	float firstMARK;
    	float secondMARK;
    	FILE *fp;
    
    		// Get course code from user
    		system ("cls");
    		printf("\n\n	Class Mark Subject Management \n\n");
    		printf("		** Add Subject ** \n\n");
    		printf("		Please enter the 7 digit course code \n");
    		printf("\n	Please press <Enter> to continue ");
    		scanf("%7s", courseCODE);
    
    		// Check course code for alpha chars and convert them to upper case
    		for(j=0; j<=7; j++)
    			{
    			if(isalpha(courseCODE[j]))
    				{
    				courseCODE[j]=toupper(courseCODE[j]);
    				}
    			}
    
    		// Get input for subjectNAME from user
    		system ("cls");
    		printf("\n\n	Class Mark Subject Management \n\n");
    		printf("		** Add Subject ** \n\n");
    		printf("		Please enter the subject name \n");
    		printf("\n	Please press <Enter> to continue ");
    		scanf("%[^\n]", subjectNAME);
    
    		
    		// Get input for the firstMARK from user
    		system ("cls");
    		printf("\n\n	Class Mark Subject Management \n\n");
    		printf("		** Add Subject ** \n\n");
    		printf("		Please enter the first mark \n");
    		printf("\n	Please press <Enter> to continue ");
    		scanf("%f", &firstMARK);
    
    		// Get input for the secondMARK from user
    		system ("cls");
    		printf("\n\n	Class Mark Subject Management \n\n");
    		printf("		** Add Subject ** \n\n");
    		printf("		Please enter the second mark \n");
    		printf("\n	Please press <Enter> to continue ");
    		scanf("%f", &secondMARK);
    
    		// Open the file in append mode and write the data to it and close the file
    		fp = fopen(MarkFile, "a");
    		fprintf(fp, "%s\n%s\n%.2f\n%.2f\n" ,courseCODE, subjectNAME, firstMARK, secondMARK);
    		fclose(fp);
    
    return 0;
    I have tried the highlighted peice of code like this as well

    Code:
    scanf("%30s", subjectNAME);
    Any help would be appreciated.

    Oh one other question relating to posting, I am sure I will have a few more questions on this assignment but not realted to this particulare problem, should I creat new threads for those for the sake of searchability or continue to post in this thread.

    Thanks

    clearrtc

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    scanf() sucks for anything even remotely complicated like what you're doing.
    The reason being scanf() only processes the MINIMUM number of characters for a given conversion. This typically means it leaves lots of \n on the input stream to confuse later input calls (like your ^\n attempt for example).

    Code:
    char buff[BUFSIZ];
    fgets( buff, sizeof buff, stdin );
    sscanf( buff, "%7s", courseCODE);
    Each input is first read with fgets(), which reads a whole line (including a \n).
    Then you can process that line using whatever you like, for example with sscanf.

    > should I creat new threads for those for the sake of searchability or continue to post in this thread.
    If it's continuing the same question, to clarify points raised, or very similar then stick to one thread.
    If it's different question about a different part of the code, start a new thread.
    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.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    16
    Hey Salem

    Thanks for the quck relpy.

    I have implemented the code you suggested but i have one of my issue in a different location now. It complete skips the course code entry and displays the add subject portion.

    I am not sure why


    Code:
    //declare local variables
    	int i = 0;
    	int j = 0;
    	char courseCODE[8];
    	char subjectNAME[31];
    	char buff[BUFSIZ];
    	float firstMARK;
    	float secondMARK;
    	FILE *fp;
    
    		// Get course code from user
    		system ("cls");
    		printf("\n\n	Class Mark Subject Management \n\n");
    		printf("		** Add Subject ** \n\n");
    		printf("		Please enter the 7 digit course code \n");
    		printf("\n	Please press <Enter> to continue ");
    		fgets( buff, sizeof buff, stdin );
    		sscanf( buff, "%7s", courseCODE);
    
    /*		// Check course code for alpha chars and convert them to upper case
    		for(j=0; j<=7; j++)
    			{
    			if(isalpha(courseCODE[j]))
    				{
    				courseCODE[j]=toupper(courseCODE[j]);
    				}
    			}
    */
    		// Get input for subjectNAME from user
    		system ("cls");
    		printf("\n\n	Class Mark Subject Management \n\n");
    		printf("		** Add Subject ** \n\n");
    		printf("		Please enter the subject name \n");
    		printf("\n	Please press <Enter> to continue ");
    		fgets( buff, sizeof buff, stdin );
    		sscanf( buff, "%30s", subjectNAME);

    I am also not getting the correct output in my txt file this was what was in the file

    Code:
    ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ
    This
    89.00
    67.00
    I was not able to type anything in for the first field which is the strange character above, in the next field that says "This" I had typed in: This is a test

    Thanks in advance

    clearrct

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    16
    Ok I made a quick change I put a getchar(); above the fget and it seems to have fixed the first problem but it still is only capturing the first word before the space.


    Code:
    comp678
    programing
    65.00
    78.00
    where it says programing aboce I typed in programing in c

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but it still is only capturing the first word before the space.
    Try the "%[^\n]" again.

    > Ok I made a quick change I put a getchar(); above the fget
    You've still got a scanf() call elsewhere then.
    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.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    16
    I assume you mean in the fget now?

    tia

    clearrtc


    Excellent That worked great. Thanks for all your assistance.


    Carl
    Last edited by clearrtc; 08-20-2006 at 02:55 PM.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    speaking of fgets()...I used it in a program to enter a string after which I sent it of to some function to get rid of the '\n' and substitute it with a '\0'.

    Code:
        printf("\nInsert first name:");
        fgets(name,sizeof(name),stdin);
        clear_string(name,sizeof(name));
        
        printf("\nInsert surname:");
        fgets(surname,sizeof(surname),stdin);
        clear_string(surname,sizeof(surname));
    Well it kind of doesn't work cause as soon as it comes to the first fget() it doesn't wait for me to imput anything and it goes on and prints out the second print. I tried putting the two 'clear_string' functions in comments but it still doesn't wait for me to type anything in. Then I tried using fgets() alone in a smaller program:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char name[50];
        char surname[50];      
           
        printf("\nInsert first name:");
        fgets(name,sizeof(name),stdin);
        
        printf("\nInsert surname:");
        fgets(surname,sizeof(surname),stdin);
        
        printf(">%s< & >%s<",name,surname);
        
        char pause;
        pause=getchar();
        return 0;
    }
    and it works fine. Any ideas?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Well it kind of doesn't work cause as soon as it comes to the first fget() it doesn't wait for me to imput anything
    Look for residual scanf() calls earlier on in the code.

    If you're using fgets() for any input, you should really use it for ALL input.
    Trying to mix and match fgets() with scanf() is just too weird.
    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.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    1
    I've had a similar problem with it stoping after any spaces in input, so I tried the sscanf(yadda, yadda, yaada);...but it need a library, which one do i use for this?

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    ? ?

    <stdio.h>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Help - String input header
    By Nextstopearth in forum C Programming
    Replies: 10
    Last Post: 06-10-2009, 11:25 AM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM