Is there such a thing as code withdrawal in a compiler?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Is there such a thing as code withdrawal in a compiler?

  1. #1
    Join Date
    Jul 2013
    Posts
    30

    Is there such a thing as code withdrawal in a compiler?

    By code withdrawal, I mean, like certain functions not working at all (or being shunned/passed by the compiler). In the code below, I've been getting to make the fgets() work in Turbo C (running in DOSBox, Win7 x64) but it always passes/jumps that code, resulting in a null value, and then proceeds to the next input prompt (scanf() -- or possibly another fgets(), but since it keeps jumping the fgets(), I had to go the scanf() route). Is there even such a thing, or did I just use fgets() incorrectly?

    Code:
    	do {
    		printf("Please enter your name (50 characters max): ");
    		fgets(nme, sizeof(nme), stdin);
    		printf("\nName: %s", nme);
    
    		len = strlen(nme);
    		printf("\nLength: %d", len);
    		if(nme[len-1] == '\n') {
    			nme[len-1] = '\0';
    		}
    
    	} while(nme[0] == '\n');
    I also tried compiling the rest of the program with Code::Blocks/MingGW and this time around, it can't seem to find the filepath of the file stream (for the fopen()).

    Code:
    fp = fopen("D:/acctinfo.txt", "r+");
    Here is the complete code for reference:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int addAcct(){
    	FILE *fp;
    	char nme[60];
    	char acn[10];
    	char pina[10];
    	char pinb[10];
    	int len;
    	long int bal = 0;
    	int pinstat = 0;
    
    	clrscr();
    	printHdr(1);
    
    	printf("before: %c\n", nme[0]);
    	nme[0] = '\n';
    	printf("after :%c", nme[0]);
    	do {
    		printf("Please enter your name (50 characters max): ");
    		fgets(nme, sizeof(nme), stdin);
    		printf("\nName: %s", nme);
    
    		len = strlen(nme);
    		printf("\nLength: %d", len);
    		if(nme[len-1] == '\n') {
    			nme[len-1] = '\0';
    		}
    
    	} while(nme[0] == '\n');
    
    	while (pinstat != 1){
    		printf("\nPlease key in your 6-digit PIN code: ");
    		//fgets(pina, sizeof(pina), stdin);
    		//printf("%s", pina);
    		//printf("%d", strlen(pina));
    		scanf("%s", &pina);
    
    		if ((strlen(pina) > 6) || (strlen(pina) < 6)) {
    			printf("\nPlease enter a 6-digit PIN code.");
    		} else {
    			printf("Please re-enter your new PIN code: ");
    			//fgets(pinb, sizeof(pinb), stdin);
    			scanf("%s", &pinb);
    
    			printf("Name: %s\nPIN1: %s\nPIN2: %s", nme, pina, pinb);
    			getch();
    
    			if (strcmp(pina, pinb) != 0) {
    				printf("\nEntered PIN codes do not match.\n");
    			} else {
    				pinstat = 1;
    			}
    		}
    	}
    
    	itoa(rand()%1000, acn, 10);
    	
    	fp = fopen("/acctinfo.txt", "w+");
    	if (NULL != fp) {
    		printf("\nYour account information is as follows:\n");
    		printf("Account Name:    %s\n", nme);
    		printf("Account Number:  %s\n", acn);
    		printf("Current Balance: $ %ld.00", bal);
    
    		fprintf(fp, "%s\n%s\n%s\n%ld", nme, acn, pina, bal);
    	} else {
    		printf("\nERROR!");
    		printf("\nAccount information not found.");
    		exit(EXIT_FAILURE);
    	}
    
    	printf("\n\nEnrollment complete!");
    	fclose(fp);
    	getch();
    
    	return 1;
    }

  2. #2
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661

    Re: Is there such a thing as code withdrawal in a compiler?

    Well... this could be a crazy TurboC/DosBox bug, but before jumping to this conclusion, are you sure the fgets instructions get skipped, instead of returning immediately?

    What does strlen(nme) give you after the first fgets call?

    Could it be that stdin has some unflushed bytes from before? Try using the (non-standard but probably working under Windows) call fflush(stdin) before your first use of fgets.
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  3. #3
    Join Date
    Jul 2013
    Posts
    30

    Re: Is there such a thing as code withdrawal in a compiler?

    Quote Originally Posted by SeventhStar View Post
    Well... this could be a crazy TurboC/DosBox bug, but before jumping to this conclusion, are you sure the fgets instructions get skipped, instead of returning immediately?

    What does strlen(nme) give you after the first fgets call?

    Could it be that stdin has some unflushed bytes from before? Try using the (non-standard but probably working under Windows) call fflush(stdin) before your first use of fgets.
    @SeventhStar: Before the fflush(), if the input is "Rie Ishida", it only got the "Rie" part and saves the "Ishida" part to the following scanf().

    Cool! Thanks a lot! That fixed my problem with the fgets(). However, I'd like to ask why the system() is not working (pretty sure that the system() is a Windows-only function).

    Note that I'm using Turbo C in DOSBox. I tried this in Code::Blocks/MingGW and it didn't work (even if I changed the address of the acctinfo.txt from relative to its complete file location.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(){
    	int opt, stat;
    	FILE *fp;
    	stat = 0;
    
    	clrscr();
    	insertCard();
    
    	if (checkAcct() != 1) {
    		...
    	} else {
    		clrscr();
    		printHdr();
    		printf("Loading account information...");
    		system("/PROJ.EXE");
    		getch();
    	}
    
    	return 0;
    }

  4. #4
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661

    Re: Is there such a thing as code withdrawal in a compiler?

    system is not a windows only function. If you remove the forward slash, it should work fine.
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  5. #5
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,558

    Re: Is there such a thing as code withdrawal in a compiler?

    As written, your program does what it should for me under MSVS c.

    You have tried to use fgets to obtain the pin number. Don't forget that fgets also stores the terminating '\n' so if 123456 is entered as pin, you get 123456\n with a length of 7 not 6.

    but since it keeps jumping the fgets()
    This is very unlikely. Much more probable is that fgets is returning either with eof/error or because there is already data in the input buffer. After using fgets, you should test for eof/error. I concur with SeventhStar about unflushed buffers. I would recommend using fflush(stdin) before using fgets for stdin.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  6. #6
    Join Date
    Jul 2013
    Posts
    30

    Re: Is there such a thing as code withdrawal in a compiler?

    Quote Originally Posted by 2kaud View Post
    As written, your program does what it should for me under MSVS c.

    You have tried to use fgets to obtain the pin number. Don't forget that fgets also stores the terminating '\n' so if 123456 is entered as pin, you get 123456\n with a length of 7 not 6.



    This is very unlikely. Much more probable is that fgets is returning either with eof/error or because there is already data in the input buffer. After using fgets, you should test for eof/error. I concur with SeventhStar about unflushed buffers. I would recommend using fflush(stdin) before using fgets for stdin.
    I know, that's why:

    Code:
    	do {
    		printf("Please enter your name (50 characters max): ");
    		fflush(stdin);
    		fgets(nme, sizeof(nme), stdin);
    		printf("\nName: %s", nme);
    
    		len = strlen(nme);
    		printf("\nLength: %d", len);
    		if(nme[len-1] == '\n') {
    			nme[len-1] = '\0';
    		}
    
    	} while(nme[0] == '\n');
    So, from the C++ Reference, it said there that:

    If the given stream was open for writing (or if it was open for updating and the last i/o operation was an output operation) any unwritten data in its output buffer is written to the file.

    If stream is a null pointer, all such streams are flushed.

    In all other cases, the behavior depends on the specific library implementation. In some implementations, flushing a stream open for reading causes its input buffer to be cleared (but this is not portable expected behavior).

    The stream remains open after this call.

    When a file is closed, either because of a call to fclose or because the program terminates, all the buffers associated with it are automatically flushed.
    Does that mean that I have to "flush" after every time I open a stream (or possibly before opening a new one)?

    Also, is file linking/handling the same as when you're working with HTML? Like,

    system("PROJ.EXE");

    Then that would mean that the file should be in the same folder as the current program running? Because I tried running that code (without the forward slash) and it said: "Illegal command: PROJ.EXE". I'm assuming that it didn't recognize the "PROJ.EXE" because it is not a native program (most of the examples I'm seeing is running notepad or another native Windows application.

  7. #7
    Join Date
    Nov 2002
    Location
    Sofia, Bulgaria
    Posts
    661

    Re: Is there such a thing as code withdrawal in a compiler?

    Quote Originally Posted by riechan View Post

    Also, is file linking/handling the same as when you're working with HTML? Like,

    system("PROJ.EXE");

    Then that would mean that the file should be in the same folder as the current program running? Because I tried running that code (without the forward slash) and it said: "Illegal command: PROJ.EXE". I'm assuming that it didn't recognize the "PROJ.EXE" because it is not a native program (most of the examples I'm seeing is running notepad or another native Windows application.
    If the executable is not in the same folder as the other, you should specify it's path with DOS paths (backslashes)

    like

    Code:
    system("\\proj.exe"); // root path
    system("c:\\somepath\\proj.exe); // absolute path
    // or
    system("..\\proj.exe"); // relative path
    Mind the double backslash
    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames

  8. #8
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,558

    Re: Is there such a thing as code withdrawal in a compiler?

    Does that mean that I have to "flush" after every time I open a stream (or possibly before opening a new one)?
    No. I would suggest only flushing before using fgetc or fgets for the stdin stream. Flushing an input stream means removing any data from the stream buffers so that the subsequent request for data has to be got from the device rather than from the buffer. Flushing an output stream means writing any data still in the output buffer to the device. Different OSs might behave differently with respect to auto flushing of buffers. Your original code in post #1 worked as expected on my XP system without requiring fflush() but on your DOSBox system you seem to need fflush().
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,316

    Re: Is there such a thing as code withdrawal in a compiler?

    Using fflush on an input stream, or an update stream in which the most recent operation was not input, actually results in undefined behaviour. So, you should not use fflush(stdin), though you might get away with it if you are sure that your code will only be used with a standard library implementation that implements and documents fflush as causing "its input buffer to be cleared" for "a stream open for reading".
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Jul 2013
    Posts
    30

    Re: Is there such a thing as code withdrawal in a compiler?

    I was looking at this:

    http://www.cplusplus.com/forum/general/39982/

    And it said that system() is a non-standard function, and is OS-dependent (Windows). Is there any other way for us to run an external program, aside from system()? Also, is system("cls") a C function alternative for clrscr()?

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,316

    Re: Is there such a thing as code withdrawal in a compiler?

    Quote Originally Posted by riechan
    And it said that system() is a non-standard function, and is OS-dependent (Windows).
    No, system (at least the one you're probably thinking of) is in the standard library. The thing is, the commands that might be run via system are not standard with respect to C, and could be OS-dependent.

    Quote Originally Posted by riechan
    Is there any other way for us to run an external program, aside from system()?
    Probably, but it depends on say, the OS.

    Quote Originally Posted by riechan
    Also, is system("cls") a C function alternative for clrscr()?
    That depends on what does the cls command actually do, if it even exists, and what does clrscr actually do (since clrscr is non-standard).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,558

    Re: Is there such a thing as code withdrawal in a compiler?

    And it said that system() is a non-standard function, and is OS-dependent (Windows). Is there any other way for us to run an external program, aside from system()
    system() is present in Turbo c (stdlib.h).

    Under Turbo c there are also the exec... and the spawn... group of functions defined in process.h (these are _exec... , _spawn... in MSVS). I would strongly suggest reading the documentation for these groups of functions before trying to use them.

    Also, is system("cls") a C function alternative for clrscr()?
    In turbo c, clrscr() clears the entire active text window (defined in conio.h). system ("cls") will execute the cls command (if present) which under windows clears the console window.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,558

    Re: Is there such a thing as code withdrawal in a compiler?

    I've been getting to make the fgets() work in Turbo C (running in DOSBox, Win7 x64)
    Why are you using Turbo c running in DOSBox under Windows 7? Is there any special reason why you need to use Turbo c? Why not use the free Microsoft Visual Studio Express?

    http://www.microsoft.com/visualstudi...indows-desktop
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Jul 2013
    Posts
    30

    Re: Is there such a thing as code withdrawal in a compiler?

    scratch this post. i saw what the problem is. hahaha
    Attached Images Attached Images  
    Last edited by riechan; August 9th, 2013 at 05:48 AM.

  15. #15
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,558

    Re: Is there such a thing as code withdrawal in a compiler?

    Code:
    } else if (x < acct.bal) {
    		printf("Insufficient funds.");
    x is 100.0, acct.bal is 10000.0. What is the relationship between x and acct.bal - less than, equal or greater than?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center