Thread: Problem storing a newline character

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    12

    Problem storing a newline character

    Hey guys,
    Still working on my project for my programming class and I have encountered yet another problem! I have a function that asks the user to enter a drill name. The function will then search to see if the file exists, and if not it will create it. If it does exist, it will prompt the user to enter a different name for the drill.

    So here's the code snippet for the creation part of my function:
    Code:
    	printf("To create a drill, please start by entering the following:\n");
    	do {
    		/* clears the arrays */
    		for (i=0; i<SIZE; i++)
    			drill[0][i]='\0';
    		for (i=0; i<2; i++)
    			garbage[i]='\0';
    		
    		printf("Drill Name:\n");
    		fgets(garbage, 2, stdin);
    		fgets(drill[0],50,stdin);
    		
    		/* removes the newline character */
    		if (drill[0][strlen(drill[0])-1]=='\n')
    			drill[0][strlen(drill[0])-1]='\0';
    		
    		/* adds the .txt to the array */
    		addtxt(drill, 0);
    		
    		/* attempts to open the file to see if the user exists */
    		new_drill = fopen(drill[0], "r");
    		
    		if (new_drill == 0) {
    			/* adds the txt file if the drill doesn't exist */
    			new_drill = fopen(drill[0], "w");
    			trigger = 1;
    		} else 
    			printf("Drill already exists! Please enter another name.\n\n");
    	} while (trigger == 0);
    Now my problem is storing the newline character. If the drill doesn't exist, it stores the newline character in garbage just fine, and stores the name in drill. However, if the drill already exists, when it repeats the do statement, it doesn't store a newline character in garbage. So when I enter in a drill (Lets say Push-up), it will store the P in garbage and create the .txt ush-up.txt. So my question is:
    Why does is store the newline only on the first time? I don't have this problem in any of my other functions. Also, how can I fix this? I'm a bit confused as to why this doesn't work like it should.

    Thanks
    -Matt

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Why do I get the feeling you're trying to do some hack around not understanding how to use fgets, or some other input function prior to this code, correctly? What is garbage exactly? Why is drill an array in which you only ever use the first element?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    12
    Garbage is just an array that I use to store the newline character (it's what my professor taught me).

    Code:
    char drill[1][SIZE];		
    char garbage[2];
    where SIZE is defined as 500, for storing long descriptions and other stuff.

    And drill stores the drill the user enters.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    An array to store a newline character??? A two-D array with one dimension being a single element??? Sorry, but you are being cheated. Your professor is totally incompetent.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    12
    garbage is not a 2-D array, drill is.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by mdekom12 View Post
    Garbage is just an array that I use to store the newline character (it's what my professor taught me).

    Code:
    char drill[1][SIZE];		
    char garbage[2];
    where SIZE is defined as 500, for storing long descriptions and other stuff.

    And drill stores the drill the user enters.
    I fully encourage you to ask for a tuition refund and make them fire that idiot.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by mdekom12 View Post
    garbage is not a 2-D array, drill is.
    I was speaking of the drill variable. It appears to be nothing more than a name. That means it should be a single array of SIZE characters.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    12
    It needs to be a 2-D array so I can add the .txt to the end of it.

    I think we're deviating from the point here, I still don't know how to fix this problem.

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by mdekom12 View Post
    It needs to be a 2-D array so I can add the .txt to the end of it.

    I think we're deviating from the point here, I still don't know how to fix this problem.
    You are joking right? Why can't you add that to the original name and have that in a SINGLE array of chars.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character problem
    By popescuradhoo in forum C Programming
    Replies: 3
    Last Post: 05-09-2008, 12:26 PM
  2. Problem storing in char pointer
    By Termina in forum C Programming
    Replies: 2
    Last Post: 05-02-2008, 07:22 PM
  3. Problem in reading Character
    By Bargi in forum C Programming
    Replies: 5
    Last Post: 04-10-2008, 11:40 PM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. Comparing Character Arrays problem...
    By newy100 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2003, 07:54 PM