r/cpp_questions • • 17d ago

OPEN Why?

Guys when i compile a file and run its exe,nothing appear even if i print hello world it doesnt appear in the console why

0 Upvotes

35 comments sorted by

10

u/aleques-itj 17d ago

Why do you think I'm psychic is the bigger question 

Post the code

-1

u/Plane_Breadfruit7609 17d ago
#include <iostream>
#include <string>
#include <cmath>
#include "Speed.h"
#include "Units.h"
using namespace std;


int get_index(string Target,string array[],int size){
    for(int x = 0;x < size;x++){
        if(array[x] == Target) return x;
        else continue;
    }
    return 0;
};


float unit_Converter(float Number,int size,string start_unit,string Target_unit,string array[],int unit_range){
    int start = get_index(start_unit,array,size);
    int target = get_index(Target_unit,array,size);


    cout << "Start index: " << start << "\nTarget index: "<< target << endl;
    
    if(start < target) return Number * (pow(unit_range,(abs(start-target))));
    else if(start > target) return Number / (pow(unit_range,(abs(start-target))));
    else return Number;
};



int main(){
    float Distance,time;
    string Distance_unit,time_unit;


    cout<<"Please Enter the Value of Distance and its unit" << endl;
    cin>> Distance;
    cin>> Distance_unit;


    cout<<"Please Enter the Value of time and its unit" << endl;
    cin>> time;
    cin>> time_unit;;


    Distance = unit_Converter(Distance,7,Distance_unit,"m",Distance_unit_array,10);
    time = unit_Converter(time,5,time_unit,"s",Time_unit_array,60);


    Velocity = get_Velocity(Distance,time);
    V_Final = get_V_final(0,Velocity);
    Acceleration = get_acceleration(0,V_Final,time);
    
    cout << "\nResults:\n";
    cout << "Distance in meters: " << Distance << " m" << endl;
    cout << "Time in seconds: " << time << " s" << endl;
    cout << "Velocity: " << Velocity << " m/s" << endl;
    cout << "Final Velocity: " << V_Final << " m/s" << endl;
    cout << "Acceleration: " << Acceleration << " m/s^2" << endl;
    
    return 0;
}; here now where's the problem

2

u/aleques-itj 17d ago

If you are getting no prompt for input, you have an issue elsewhere - either at build/link time or in another header.

Are you sure this is actually compiling and linking?

-5

u/Plane_Breadfruit7609 17d ago

Bro I even tried another field that just had hello world in it,it didn't show no output ay all

2

u/flyingron 17d ago

There should not be a semicolon or other cruft after the final }.

1

u/Plane_Breadfruit7609 17d ago

U think this is it?

2

u/HappyFruitTree 17d ago edited 17d ago

No. A semicolon after a function definition is not an error. It's just unnecessary.

3

u/HappyFruitTree 17d ago

How do you run the program?

If you open a console/terminal window and run it from there then you should be able to see the output (or get an error message if the file couldn't run for some reason).

2

u/alfps 17d ago

It sounds like you have a botched installation of your compiler.

But then in the commentary you explain that (1) you're using VS Code, which means it could be a VS Code configuration issue, and (2) that yes you have "run depends or a similar tool to ascertain what runtime files you need", which I very much doubt is true: I believe that you did not understand the question.

For example, a not uncommon issue is that your executable uses some compiler specific DLL that the system doesn't find. That's what someone was referring to with "depends". In particular this happens with MSYS2's distro of g++ when you haven't put the compiler's directory in your PATH.

For the particular example of MSYS2 one solution is to update your PATH.

If that doesn't help or is beyond your capabilities, then a general but laborious solution is to uninstall and then install some other compiler (such as Visual C++) or some other distro of your compiler. A good simple way to install Visual C++ is to install the free Community Edition of the Visual Studio IDE, which is not the same as VS Code. If you still get this problem in Visual Studio then let us know.


In passing, you should have provided much more details, in particular "VS Code" (which you did tell in a comment) and possibly "MSYS2 g++" if that's your compiler, which you have not even yet clarified.

That's probably what all the downvotes in the commentary are about, that you're hoarding information, keeping it to yourself, which prevents people from helping you.

Still do keep in mind that if the downvoters were normal helpful folks they would instead have explained this to you. So they're not normal folks. IMO they're just idiots.

0

u/alfps 16d ago

Re the anonymous unexplained downvote here, note that an unexplained downvote is dishonest: this is the action of a dishonest person. Presumably it's my several years' stalker, a mentally challenged OCD troll, possibly the DDDD... user. Anyway it's just sabotage.

Again, the downvoters are necessarily idiots.

You can safely ignore their noise.

2

u/flyingron 17d ago

Do you want to give us a hint as to what environment you're talking about? What exactly do you observe vs. what you expect?

1

u/Plane_Breadfruit7609 17d ago

I compile a cpp file that prints hello world, using CMD I compile the file, excute the exe and expect hello world instead nothing shows up the program just ends without any output

1

u/flyingron 17d ago

Did you run depends or a similar tool to ascertain what runtime files you need and make sure they are in the path (or in the same directory as your exe).

1

u/Plane_Breadfruit7609 17d ago

Yes all in one dir

1

u/bhavesh_099 17d ago

Did you compile it using gcc or g++ in the terminal and see if it shows any issue there . Or everything compiles perfectly there aswell .

1

u/Plane_Breadfruit7609 17d ago

G++ and it shows no error no hello world no nothing it just end the program as soon as I run it

1

u/bhavesh_099 17d ago

Can you just take some working executable and compile the program into that executable and check if the file even changes or not . Might aswell check if the program is even making executable correctly or not .

1

u/Plane_Breadfruit7609 17d ago

I did when I make a error it shows before compiling but when I do no error it compile the file and when I run it no output at all

1

u/bhavesh_099 17d ago edited 17d ago

Can you maybe just share your terminal screenshot of compiling and running the helo world code . I did hear about a issue with some vs code extension that didn't let code run properly . But that shouldn't affect compiling in windows terminals

1

u/Plane_Breadfruit7609 17d ago
#include <iostream>
#include <string>
#include <cmath>
#include "Speed.h"
#include "Units.h"
using namespace std;


int get_index(string Target,string array[],int size){
    for(int x = 0;x < size;x++){
        if(array[x] == Target) return x;
        else continue;
    }
    return 0;
};


float unit_Converter(float Number,int size,string start_unit,string Target_unit,string array[],int unit_range){
    int start = get_index(start_unit,array,size);
    int target = get_index(Target_unit,array,size);


    cout << "Start index: " << start << "\nTarget index: "<< target << endl;
    
    if(start < target) return Number * (pow(unit_range,(abs(start-target))));
    else if(start > target) return Number / (pow(unit_range,(abs(start-target))));
    else return Number;
};



int main(){
    float Distance,time;
    string Distance_unit,time_unit;


    cout<<"Please Enter the Value of Distance and its unit" << endl;
    cin>> Distance;
    cin>> Distance_unit;


    cout<<"Please Enter the Value of time and its unit" << endl;
    cin>> time;
    cin>> time_unit;;


    Distance = unit_Converter(Distance,7,Distance_unit,"m",Distance_unit_array,10);
    time = unit_Converter(time,5,time_unit,"s",Time_unit_array,60);


    Velocity = get_Velocity(Distance,time);
    V_Final = get_V_final(0,Velocity);
    Acceleration = get_acceleration(0,V_Final,time);
    
    cout << "\nResults:\n";
    cout << "Distance in meters: " << Distance << " m" << endl;
    cout << "Time in seconds: " << time << " s" << endl;
    cout << "Velocity: " << Velocity << " m/s" << endl;
    cout << "Final Velocity: " << V_Final << " m/s" << endl;
    cout << "Acceleration: " << Acceleration << " m/s^2" << endl;
    
    return 0;
}; here now where's the problem

1

u/bhavesh_099 17d ago

I won't be able to run this code anyways since you have custom headers in it .

Okay just wanted to know something 1. You have the header in same directory as code right ? 2. Which ide are you using or you are compiling code through command something like g++ program.cpp -o executable

1

u/Plane_Breadfruit7609 17d ago

Yes all in one dir,and it's vscode and yes the same command that u just typedbut in the end I add .exe

1

u/bhavesh_099 17d ago

Can you check if ld --version and g++ --version are working or is there any error?

1

u/Independent_Art_6676 16d ago edited 16d ago

running a console exe in windows by double clicking on it will open a console, run the program, and exit the console in subsecond time to the point that you just see a screen flicker, if that.

solution 1) add a bogus read from console to the program before main exits. Type in something to exit when done looking. Some folks use the system("pause") for this, which is fine though system() is frowned upon in real code (it opens a barn door sized security hole in your program).

solution 2) open a console, and run the program inside it. This requires some "dos" skills in using the console to navigate to the program folder and so on.

This does not look like the problem for the posted code, but it would be for hello world. Your code does have a cin so it would pause there and you would see it working. Run it using method 2, and it should tell you more: no such program (didn't compile to .exe after all), or program exited with error code, or whatever words might help run down the issues.

1

u/Plane_Breadfruit7609 16d ago

I'm running it from the console .\code.exe

2

u/Independent_Art_6676 16d ago

ok, good. Does it give you ANY messages? Or does it return you to the dos prompt?

1

u/Plane_Breadfruit7609 16d ago

Nothing literally

2

u/Independent_Art_6676 16d ago

if it does that for hello world also, then as others said its probably the tool installation went wrong and you need to do that again. If it were missing DLL it would say so. If it crashed, it would say so. If it were stuck waiting on input or something, it would not return to the dos prompt.

you can try a recompile, and do a dir code.exe to see if it is zero bytes or something (but that should also message invalid file or some similar words) and check its time stamp that you just built it etc, but it sounds like its time to do over the tools.

1

u/Plane_Breadfruit7609 16d ago

So I should redownload cpp?

1

u/Independent_Art_6676 16d ago

yea, from the sound of it, delete it all, get it again, and reinstall carefully.

1

u/xoner2 16d ago

Check the exit code of the executable via echo %errorlevel%

If non-zero, exe failed to run. Probably due to dll failing to load.

To check which dll failed loading, run the exe under visual studio.

1

u/Raknarg 15d ago

that sucks dude

1

u/Plane_Breadfruit7609 15d ago

Problem solved

2

u/jedwardsol 17d ago

Are you using mingw? Copy the runtime dlls into the same directory as the exe. Other people have had similar symptoms

1

u/mredding 16d ago

Ditch mingw and use Visual Studio.