I'm learning Cpp programming and I'm using Dev-C++ as compiler. I made this example to see how class & objects works in this programming language but the problem is the compiler does not even running the code! Here's the code:
#include <iostream>
using namespace std;
class BuckysClass{
public:
void coolSaying(){
cout << "Some Sentences" << endl;
}
};
int main(){
BuckysClass buckysObj;
buckysObj.coolSaying();
return 0;
}
I have saved the file with .cpp extension and tried to run it by pressing Ctrl+F10.
Please if u know what I'm doing wrong ,let me know I really appreciate that. Thanks in advance...
First of all:
F9 - compile the source program
F10 - run the source program
In case if your terminal disappears, you can add getchar() before return 0. This will make the command prompt wait for your input, and thus you will be able to see the results.
PS: Don't use Dev-C++. It hasn't been updated for a long time.
You just need to add a getchar() or system("pause") before return 0
Nothing! The command prompt just appears in just 1 second and then
disappear...
Need to add a system("PAUSE"); (I'm supposing you are under Windows)
Try this:
class BuckysClass{
public:
void coolSaying(){
cout << "Some Sentences" << endl;
}
};
int main(){
BuckysClass buckysObj;
buckysObj.coolSaying();
system("PAUSE");
return 0;
}
Try to build it first then run it. also you need to add system("pause") before return 0 to pause the screen so you can see the result.
I highly recommand you use either CodeBlocks or Eclipce rather than Dev-C++.
Related
I'm getting started with C++ and am trying to work with vectors and printing stuff. I am using Windows 10, writing my code in Visual Studio Code, I just downloaded the MinGW-g++ compiler, and am trying to run my code in CMD.
This is my code, I've tried printing different things, and it's not even printing this "Hello". I've also tried using << endl; which also prints nothing. I also tried adding and remove a "return 0;" at the end but it changed nothing. The program compiles just fine. Any advice?
#include <iostream>
using namespace std;
int main(void)
{
std::cout << "Hello";
std::cout.flush();
}
I've been a C++ developer since it arrived. All was on windows, and I haven't touched it in about 6 years.
Now I'm trying to get an old code-base working using VS Code on my Mac. I'm using clang++ with c++17.
This problem is vexing; I've seen many other posts with the same issue, but the problem always seemed to be something in the code.
Note: this code worked fine with C++11 on Windows.
To simplify, I copied the code to execute right at the top of main. Here is is:
ifstream file("assets/textures/blocks.txt", ios::in);
if( file.is_open() ) {
string s;
getline(file, s); // <-- This line causes the error.
cout << s << endl;
}
As this code worked elsewhere, I assume I've got a setup or environment problem and am looking for hints towards what to check on.
Thank you for any help!
An update:
Thank you. I paired the program down and tried a few things. Here's the deal:
If I leave all my files to be compiled, but replace main.cpp with the below code, the cout line generates the same exception.
If I cull all the unused files, the code works.
Something in some other file is somehow breaking the stream code. I'm clueless.
#include <iostream>
#include <istream>
using namespace std;
int main() // int argc, char** argv)
{
cout << "Hello World" << endl;
}
I should add: this is a GLFW 3D game engine app. It does not subclass or interact with any stream in any way other than the most basic file read/write operations.
I'm using the following application
#include <iostream>
using namespace std;
void print(char* s)
{
cout << s << endl;
}
int main(int argc, char* argv[])
{
print("a");
print("b");
print("***");
print("c");
print("d");
}
When I run it with the debugger under VSCode (using "Native Debug" extension), it never pass the second print and this is my output:
a
b
It never pass print("***), even if I put a breakpoint after that line it doesn't reach it.
if I comment out print("***"), the application finish with the correct output:
a
b
c
d
The only thing I managed to figure out is that there is and issue printing "*" character and if I replace it with any other character all works fine. Why do I see this behavior and how can I fix this without having to change the code?
#molbdnilo, seem to be correct. It looks like a bug in "Native Debug" extension. The problem is not reproduced when another extension is used (I tried with Microsoft's "C/C++" extension)
when i run this code mentioned below, The output console appears for fraction of second and then disappear, but every thing is fine in this code.there is no compile error or warning. i also use get character function but same issue.
i am using dev-c++ 4.9.9.2 version on win 7, 32 bits. what to do?
#include <iostream>
using namespace std;
int main ()
{
cout<<"welcm";
return 0;
}
You can use cin.get() before your return statement to avoid the console window from closing.
You need to tell the program to wait before closing.
cout<<"welcm";
system("pause"); // add this line
return 0;
I have a Win32 program that runs on a loop. I would like to be able to pause that program while awaiting a keypress. It doesn't matter whether I use 'any key' or a specific key, but I need to have the program freeze until I press something.
I am wondering which command I should use. I am working with Visual C++ and the compiler doesn't recognise any of the following commands:
cin.get()
std::cin.get()
getch()
I am relatively new to C++. I understand that in a console app this is a fairly simple action to take (cin.get), but that it can be more difficult in Win32. Any simple solution or workaround would be appreciated. The program is bespoke to be used in a single scientific experiment, so for now I'm not fussed if the solution is a little botchy(!)
Apologies if I've missed any important info from my question.
You should use neither.
You should use
#include <iostream>
...
int main()
{
...
std::cin.ignore(); //why read something if you need to ignore it? :)
}'
Here's the documentation
Example:
#include <iostream>
#include <conio.h>
int main()
{
std::cout << "Press any key to continue . . ." << std::endl;
_getch(); // wait for keypress
}
_getch() is C++ equivalent to C getch()
Try
#include <iostream>
using namespace std;
char temp;
cin >> temp;
Assuming that you are looking for an alternative for getch ( which does not echo to screen).
If you are using windows and visual studio to be precise try using _getch.
Here is a link to it
http://msdn.microsoft.com/en-us/library/078sfkak(v=VS.100).aspx
You should #include <iostream> and use std::cin.get();
I think the getch() is a C function, but since you are using C++, then the cin would be more appropriate.
HWND hwnd = ::GetConsoleWindow();
while (!((::GetForegroundWindow() == hwnd) &&
((::GetKeyState(VK_SPACE) & 0x8000) != 0)))
::Sleep(0);
Suppose it is not the best way but it solved my problem.
Replace VK_SPACE with any other value you like.
And it is not portable.