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;
}
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
Re: Is there such a thing as code withdrawal in a compiler?
Originally Posted by SeventhStar
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;
}
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
Re: Is there such a thing as code withdrawal in a compiler?
Originally Posted by 2kaud
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.
Re: Is there such a thing as code withdrawal in a compiler?
Originally Posted by riechan
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)
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
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
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()?
Re: Is there such a thing as code withdrawal in a compiler?
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.
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.
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
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
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?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.