Code Not Updating
-
7 februarie 2012 04:52
Ok first off I am brand new to Visual C++ 2010 Express however I am confused by something very basic. I have a program that I build and run and it works fine with the exception that it closes the console window before I can see the results. I have tried adding "getchar();" to the end of my code but that doesn't seem to help at all.
Secondly, if I change the source code and then rebuild and rerun the code the old program still remains. I have to close the source file and then reopen it and rebuild and rerun to see if the changes took effect. What the hell is wrong with this thing??? Shouldn't I be able to type a change in the code and then save, build and run and be confident that it is using the latest code?????? WTF am I missing??? Does it matter that my actual source code is in a different folder location that the rest of the project???
I am so freakin' frustrated it isn't even funny. Any advice would be appreciated. I need to go slit my wrists after this nonsense. =(
- Editat de Robert J. Wagstaff 7 februarie 2012 04:57
Toate mesajele
-
7 februarie 2012 05:16>I have tried adding "getchar();" to the end of my code
>but that doesn't seem to help at all.
Where did you add it?
Some possible causes:
(1) The getchar() is *after* a return statement.
Obviously if the program ends via a return statement
before the getchar() is reached it won't help much.
(2) A Release Build is being done and the compiler's
optimizations are throwing the getchar() away as its
result is never used. A Debug build should be used to
prevent optimizations altering expected code generation.
An alternative is to put a Breakpoint on the line you
want to stop at (before execution). Note that focus
will be on the IDE when the program stops on a
Breakpoint, so you have to manually switch to the
console window with the program output.
Using Breakpoints and Tracepoints
http://msdn.microsoft.com/en-us/library/5557y8b4.aspx
>if I change the source code and then rebuild and rerun
>the code the old program still remains.
May be caused by:
(1) An explicit Save isn't done before doing a Re/Build.
and
(2) The "Before building" Option isn't set to "Save all
changes".
Build and Run, Projects and Solutions, Options Dialog Box
http://msdn.microsoft.com/en-us/library/cyhcc7zc.aspx
- Wayne -
7 februarie 2012 07:12
Thank you Wayne for your response. This is what I have found from testing the following code.....
========================
#include <stdio.h>
#include <stdlib.h>int main ()
{
double vnumber;
printf("[Test Alpha] Please enter a number: ");
scanf_s("%lf", &vnumber);
printf("The number you entered rounded to the nearest tenth is: %.1f", vnumber);
getchar();getchar();
}=============================================
1) The console window only stays open if I repeat "getchar();" twice. Does this seem right??????
2) If I save the source code to another location (e.g. network drive, USB Flash drive, other folder) and build and run the code after a change it is only using the actual new code some of the time. All I have been doing is changing "Test Alpha" to "Test Bravo" and then hitting F5 and it builds and runs. It DOES NOT ALWAYS show the new code. The exception to this is if I put the source code into the root folder of the project. In that case it seems to always update. I am sure the file is saved regardless of the location. I can send the source code file to notepad and it shows the updated code. This is puzzling to me. How can it compile and run using the old code????
Any insight would be appreciated. =)
-
7 februarie 2012 08:08>then hitting F5 and it builds and runs
F5 is the Run command. Whether or not a Build takes place
before the program is run depends on your settings. Look
at the last link I gave you, and read the part about:
"On Run, when projects are out of date"
Note that if it's set to: "Never build" then guess what
happens when you run? I recommend that it be set to:
"Always build"
I also recommend always doing an explicit Build (F7) or
Rebuild (CTRL + ALT + F7) before trying to do a Run. You
should check compiler/linker warnings and errors and
resolve them all before trying to Run the program.
- Wayne -
7 februarie 2012 08:20>The console window only stays open if I repeat "getchar();"
>twice. Does this seem right?
Yes. Because the scanf() stops when it encounters a newline
in the stream, which will be at the end of the input from
pressing Enter. The first getchar() accepts that newline
and so doesn't have to wait for more keystrokes.
You should get by with a single getchar() if you change
the scanf() to this:
scanf_s("%lf%*c", &vnumber);
- Wayne -
7 februarie 2012 08:23
Hi
I can only give you clue. Put fflush(stdin); before getchar(). It looks to me that getchar() takes your [RETURN] key meant for scanf(). I hope that other guys can explain this.
Best regards
Chong
-
7 februarie 2012 08:58
A follow-up caveat:
Using %*c will discard the newline *if* that's the next
character in the input stream. However, if your input
is something like this: 123.45abc then the %*c will
just throw away the 'a' and the next character 'b' will
be grabbed by the first getchar() and the program won't
stop. While it's tempting to use fflush(stdin) since it
will discard everything left in the stdin stream, take
note that using fflush() with an *input* stream is *not*
supported by the C/C++ language Standards. While it
works with VC++ it may not work with other compilers.
So a portable solution that will discard all data left
in the buffer after the scanf may look like this:
scanf_s("%lf", &vnumber); // NB - *no* %*c
char c;
while((c = getchar()) != '\n' && c != EOF);
printf("The number you entered rounded to the nearest tenth is: %.1f", vnumber);
getchar();
}
- Wayne- Propus ca răspuns de chong kyong kim 7 februarie 2012 09:05
- Marcat ca răspuns de Rob PanModerator 13 februarie 2012 09:11
-
7 februarie 2012 18:23
Thank you again for all the helpful insight. My Visual C++ 2010 Express is setup with "Save all changes" before building and "Always build" on run when projects are out of date. Therefore I am not sure why it is using old code when I run (and build) the project if source code is in another folder. I guess I'll just keep the source code in the root folder of the project. Lame but whatever.
Seems like "getchar();getchar();" is simple enough to keep console open. I'll just stick with that unless it is a big no no.
Cheers!!
-
7 februarie 2012 18:38>Seems like "getchar();getchar();" is simple enough to
>keep console open. I'll just stick with that unless
>it is a big no no.
It's "dealer's choice". But be aware that for some
programs you write you will have to press Enter twice
at the end as *both* getchar() calls will cause a
pause. It all depends on whether or not something is
left in stdin.
Also, in cases such as I described where there are
*several* characters left in the stdin stream then
the program will end immediately and the console
window will close without waiting. Both getchar()
calls will find something waiting in stdin so they
won't have to wait for additional keyboard input.
- Wayne -
8 februarie 2012 03:17
you can use getch() or system("pause"); etc . Perform rebuild . Clean the solution and build your solution once again. you can use fflush(stdin) to flush everything from your console.
Thanks
Rupesh Shukla
- Editat de Pintu ShuklaMicrosoft Community Contributor 8 februarie 2012 03:19
- Marcat ca răspuns de Rob PanModerator 13 februarie 2012 09:11
-
29 martie 2012 16:08
Does anyone know why I cannot create a new thread (post) on any of the MSDN forums??? I used to be able to but now there just isnt a link for "Ask a Question". There is just no option to create a new post.
Any help would be appreciated.
Thank you
-
29 martie 2012 23:07
>I used to be able to but now there just isnt a
>link for "Ask a Question".
You mean you don't see a big, brown colored link in
the upper right corner which says:
"Ask a question in the forums" ?
- Wayne- Editat de WayneAKingMicrosoft Community Contributor 30 martie 2012 03:12