Visual Studio CPP No Entry - c++

I'm writing a cpp app for some while now. A few hours into the day made a small change did a build to test it and to my surprise , the build executed and nothing happened , it didn't stop executing it had a process running.
so I stopped it. and this is what I have tried.
Restrating my PC- Same Result
Making a breakpoint after the entry point. - Same Result
breakpoint didn't even hit which makes me think that the entry point just does not work.
Making a syntax error - it didn't compile and didn't run
which means my program did compile and did run before.
Completly undoing everything I did after the last running build - Same Result. it worked before but i guess not anymore
Changing my entry point from WinMain to int main() -
cmd window was created but no signs of code executing.
Doing std::cout on the first line(with cmd window) - Same Result
The only thing that worked was commenting the whole file with the entry point and just writing :
#include <iostream>
int main() {
int i;
std::cout << "hello";
std::cin >> i;
}
Anyone knows what can make such a weird behavier ?

You probably have an infinite loop in some static initialisation code.
If you hit pause in the debugger it will show you where the problem lies.

Related

GDB Skips While Loop Condition When Used With File Input

Alright Stack Overflow, I am running into a decently persistent problem in my C++ code. I'm sure this is one of those dumb mistake moments, but I have tried everything and cannot seem to squish this bug.
I have a bit of code here, and it's behavior is very odd. I have a main function that opens a file containing text I want to read in. I was taught in programming fundamentals class at my university that I could use getline() as a condition for the while loop, which is nice since it automatically terminates when it reaches the end of the file.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream input_mem_traces("gcc.txt");
string trace_to_parse = "";
while(getline(input_mem_traces, trace_to_parse))
{
cout << trace_to_parse << endl;
}
}
When I compile and run it, it works just fine. It reads out every single line of the file I pass it, and returns with no problems.
However, when I try to use gdb, and set a breakpoint at the line
cout << trace_to_parse << endl;
it didn't hit the breakpoint. Curious as to why that was, I broke above the loop, and tried single stepping through the code. When I got to the while loop, and tried to step, it simply skipped to the line after it, which happened to be the end of the program.
This behavior occurs both using the VSCode GUI front end for gdb, as well as straight gdb from the command line. I am running this on Windows using Ubuntu under WSL2, and VSCode as my IDE with the Remote - WSL extension enabled.
Turns out there was some weirdness going on with the working directory with GDB. For some reason, my working directory was changing under the VSCode GUI, thus the file was not able to be opened, and the while loop condition performed as expected for that circumstance by not entering the loop. Upon the recommendation by Retired Ninja in the comments, I used an absolute path in the fstream object constructor, and that solved the issue.

c++ (on Clion) for loop stops in the middle with no errors (exit code 0) [duplicate]

When using CLion I have found the output sometimes cuts off.
For example when running the code:
main.cpp
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 1000; i++) {
printf("%d\n", i);
}
fflush(stdout); // Shouldn't be needed as each line ends with "\n"
return 0;
}
Expected Output
The expected output is obviously the numbers 0-999 on each on a new line
Actual Output
After executing the code multiple times within CLion, the output often changes:
Sometimes it executes perfectly and shows all the numbers 0-999
Sometimes it cuts off at different points (e.g. 0-840)
Sometimes it doesn't output anything
The return code is always 0!
Screenshot
Running the code in a terminal (i.e. not in CLion itself)
However, the code outputs the numbers 0-999 perfectly when compiling and running the code using the terminal.
I have spent so much time on this thinking it was a problem with my code and a memory issue until I finally realised that this was just an issue with CLion.
OS: Ubuntu 14.04 LTS
Version: 2016.1
Build: #CL-145.258
Update
A suitable workaround is to run the code in debug mode (thanks to #olaf).
The consensus is that this is an IDE issue. Therefore, I have reported the bug.
A suitable workaround is to execute the code in debug mode (no breakpoint required).
I will update this question, as soon as this bug is fixed.
Update 1
WARNING: You should not change information in registry unless you have been asked specifically by JetBrains. Registry is not in the main menu for a reason! Use the following solution at your own risk!!!
JetBrains have contacted me and provided a suitable solution:
Go to the Find Action Dialog box (CTRL+SHIFT+A)
Search for "Registry..."
Untick run.processes.with.pty
Should then work fine!
Update 2
The bug has been added here:
https://youtrack.jetbrains.com/issue/CPP-6254
Feel free to upvote it!

Get output to the Console window in C++

so i am new to C++ and I was hoping the community could help me with my homework. Now im not asking for someone to do it for me because i am very capable of doing it on my own, i am just asking for help on a particular part. So, my assignment involved making a program that could be able to find and print all of the prime numbers between 2 and 100. I have to use a double loop (its what my professor said), so i have set up an if statement to run through all of the numbers from 2 to 100 and have a second loop inside the first one to determine if the current number is a prime number and then print it. Here is where my issue comes into play, when i run it, it opens the console window and closes it so fast i cannot see if anything did print to it. So i added a break point to see if it did. When i press F5 to step into each next step, it runs through the loop once and then it starts jumping over to different windows reading the lines of different source files (i think they are source files). Eventually the console window closes with nothing printed to it, and it doesn't start the loop over again like it should. My question is this, like in Visual Basic where you can put console.readline() so a button has to be pressed from the keyboard in order to continue, how can you do the same in C++ so that after the loop to see if the number is prime ran and printed the number, the program will wait for a key to be pressed right after it was printed?
Here is my current code as follows, Thanks again for any help, I really appreciate it.
#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int counter=2;
int primeCounter=counter;
//two variables, one to keep track of the number we are one, the other to check to see if its prime.
if(counter<101)
{//I want the counter to increment by 1 everytime the loops runs until it gets to 100.
if(!(counter%(primeCounter-1)==0))//if the counter has a remainer, then it is prime
{//each time the main loop run i want this loop to run too so it can check if it is a prime number and then print it.
cout<<counter<<endl;
//if this was VB, here is where i would want to have my console.Readline()
}
else
{
}
counter+=1;
}
else
{
}
Since you're using Visual Studio you can just use Ctrl+F5 to run your program without debugging. That way the console window stays after the program's finished.
Alternatively you can run the program from the command line.
Or you can put a breakpoint on the last } of main and run it in a debugger.
It's not a good idea to add a “stop here” at the end.
If you want see each line of output as it's produced, just place a breakpoint after the output statement and run the program in the debugger, in Visual Studio keypress F5.
In passing, <stdafx.h> is not a standard header. It's in support of Visual C++ precompiled headers, which is a feature that yields quite non-standard preprocessor behavior. Better turn that off in the project settings, and remove the >stdafx.h> include.
Also
int _tmain(int argc, _TCHAR* argv[])
is a silly non-standard Microsoft-ism, at one time in support of Windows 9x, but no longer with any purpose other than vendor lock-in.
Just write standard
int main()
or with trailing return type syntax,
auto main() -> int
Finally, instead of
!(counter%(primeCounter-1)==0)
just write
counter%(primeCounter-1) != 0
cin.get() will do what you want.
In Visual Studio you can actually call system("pause"); in a place you need to suspend your app.
so i figured out my issue. First off the loop wasnt running because the first if statement didnt loop. I changes this to a while and now the output works like a charm. take a look
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
int counter=2;
int primeCounter=counter;
//two variables, one to keep track of the number we are one, the other to check to see if its prime.
while(counter<101)
{//I want the counter to increment by 1 everytime the loops runs until it gets to 100.
if((counter%(primeCounter-1)==0))//if the counter has a remainer, then it is prime
{//each time the main loop run i want this loop to run too so it can check if it is a prime number and then print it.
}
else
{
cout<<counter<<endl;
}
counter+=1;
primeCounter=counter;
}
}
now i just need to polish the condition to actually figure out the prime numbers. thanks again for your help.!!!!

C++ , won't display last line of code

Shouldn't this work? I mean, the code is merely a test and is meant so that the dialogue goes this way : What is your name? name here, Hello name here, and yet it does not show the last line of Hello after I type in my name and click enter it just dissapears. Here is the code.
#include <iostream>
#include <string>
int main (void)
{
using std::cin;
using std::cout;
using std::string;
string name = "";
cout << "What is your name, pls?\n";
cin >> name;
cout << "\nHello " << name.c_str() << "\n";
return 0;
}
My guess is that you are running from the debugger, or double clicking the executable. In either of those cases, when the program ends, the console will close. So, the program produced output, but you just could not see it before the console closed.
Run the program from a pre-existing console so that the console remains after your program ends. Or, just whilst debugging, arrange that your program does not terminate immediately after emitting its final output. A simple way to do that is to place a break point at the end of the program.
It probably showed it right before it disappeared. If you're going to write console programs, and if you're going to send output to a console, you should run them from a console so the output has some place to go.
After you are done with your program, press Ctrl + F5 ( Run without debugging). This will prompt before closing the window and this is what you want.
Make sure you put a breakpoint before main goes out of scope. I guess your console disappears under VS?
Also, you don't need to extract the char* in the last cout statement:
cout << "\nHello " << name << endl;
Open a terminal (or a command prompt window).
Navigate to the folder that contains the executable.
Run it.
It's not disappearing. It is just running really fast.
Every IDE has a keyboard shortcut that allows you to run code and pause after the execution has finished.
This keyboard shortcut is Ctrl-F5 in Visual Studio.
I have no idea what IDE you're running, but that is your basic problem.
The other thing you can do is to test your code in ideone : ideone.com/hb4Cel (it's the same code. There is no point pasting it here)
A dirty workaround is to add something like this
cin >> name;
at the end, just before return 0;. It forces the window to wait for input (i.e. hitting return) before returning (which closes the program).
This isn't necessarily good design, but if all you want to do is run some tests then it'll do the trick.
Basically when you enter your name it displays your last line and exits after return 0.
Here are the following things to avoid that
1- use command line to run the application
Start->accessories->command prompt
Go to folder in which your application is using cd command
c:>cd c:\path\foldername
Now run the application by typing the program name e.g
c:\path\foldername>my_application.exe
It will display your last line.
2- Now if your are using microsoft visual c++ press ctrl+F5 to run your program
3- This is not recommended but you an use it as long as your are debugging then remove it from the code afterwards. Include conio.h header file and add getch(); line before return statement. It would hold the screen for you till you press a key.

Why would calling MessageBox[etc]() without a return variable cause the program to crash?

So if I write the following code:
MessageBoxA(0, "Yo, wazzup!", "A Greeting From Earth", 0);
the program crashes with an access violation when it exits. When I write the code like this:
int a;
a = MessageBoxA(0, "Yo, wazzup!", "A Greeting From Earth", 0);
it doesn't crash. Now I know why it crashes when it crashes thanks to another question I asked, also regarding argument-mismatching, but I don't know why it crashes.
So why does this cause an APPCRASH? I was always under the impression that calling a function that had a return-type, without actually giving one was safe, example:
int SomeFunction (void) {
std::cout << "Hello ya'll!\n";
return 42;
}
int main (void) {
int a;
// "Correct" ?
a = SomeFunction();
a = MessageBoxA(0, "Yo, wazzup!", "A Greeting From Earth", 0);
// "Incorrect" ?
SomeFunction();
MessageBoxA(0, "Yo, wazzup!", "A Greeting From Earth", 0);
}
When I run this kind of test "clean" (in a new file) I don't get any errors. It only seems to give an error with MessageBox/MessageBoxA when run in my program. Knowing the possible causes would help me pinpoint the error as the project code is too big to post (and I would need my friend's permission to post his code anyway).
Additional Info:Compiler = GCCPlatform = Windows
EDIT:
UpdateThanks everyone for your feedback so far. So I decided to run it through a debugger... Now Code::Blocks doesn't debug a project unless it is loaded from a project file (*.cbp) - AFAIK. So I created an actual project and copy-pasted our project's main file into the projects. Then I ran in debug mode and didn't get so much as a warning. I then compiled in build mode and it ran fine.Next, I decided to open a new file in Dev-C++ and run it through the debugging and later the final build process and again I got no errors for either build or debug. I cannot reproduce this error in Dev-C++, even with our main file (as in the one that causes the error in Code::Blocks).
ConclusionThe fault must lie in Code::Blocks. AFAIK, they both use GCC so I am pretty confused. The only thing I can think of is a version difference or perhaps my compiler settings or something equally obscure. Could optimizer settings or any other compiler settings somehow cause this kind of error?
The version with the return value does not crash because it had one int more on the stack. Your erroneous code reads over the bounds of the stack and then runs into an access violation. But if you have more on the stack you will not hit the guard page, because that is just enough extra stack. If the the erroneous code only reads it is sort of OK, but still broken.
We had one bit of WTF inducing code that was like so:
char dummy[52];
some_function();
There was thankfully a longish comment explaining that removing dummy, makes some_function crash. It was in a very old application so nobody dared touch it and the some_function was totally different module we had no control over. Oh yea and that application was running smoothly in the field for over 20 years in industrial installations, like refineries or nuclear power plants... ^_^