Extremely beginner c++ issue

Hey guys, so I recently (like about 2 hours before making this post) started with c++. I am either blind, or just really stupid; because I have a probably very easily fixable problem. When I run the built in "Hello World" program, it works fine.. 50% of the time. The other 50% it doesn't show anything in the CMD window. Also, if I type something else into the quotations, nothing shows up when I build and run. If someone could help, I would very much appreciate it. Thanks!

1
2
3
4
5
6
7
8
9
10
11
  #include <iostream>

using namespace std;

int main()
{
cout << "Hello World!" << endl;

    return 0;
}
You should not have any problems unless you are talking about it maybe closing right as it opens? http://www.cplusplus.com/forum/beginner/1988/

I don't understand what you mean about it not dispalying anything else besides hello world.
Try this
1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << "!dlroW olleH" << std::endl;
}
This is what I mean:

http://imgur.com/Hd1U6qS
When you click build you are hitting build and run and not just run correct?
What happens if you do this:

1
2
3
4
5
6
7
8
#include <stdio.h> // I know guys, cstdio, but maybe his compiler is old.

int main()
{
    printf("Derp");
    getchar();
    return 0;
}
no he has the newest code::blocks.
Yes I am hitting build and run.
Then I am going to have to say you are trolling.
You have another exe open that you coded wrong and is displaying an error.
Dude, why would I spend all this time just to troll? I'm not trolling. Idk what the problem is. When I hit build and run, sometimes the text shows; and sometimes not. I didn't even mention the fact that when I do hit the build and run sometimes the cmd window doesn't even come up.
@giblit what if he didn't install the compiler that came with it or used another one?

@TheGaminLlama please try adding an std::getchar() call before return 0; Normally C::B will include code that halts the cmd window from exiting prematurely, but maybe you need to do this manually.
Well I have no idea then maybe someone else can help. Only thing I can suggest is to maybe try and create an empty project then add an empty cpp file to the active project.
I'm pretty new but I had to install mingw when I installed the newest version of Codeblocks because the compiler didn't get installed with it. You might want to check to make sure that it was installed.
giblit, I'm also "kinda new to this but let me tell you what i think.

Note that I use visual studio and im not all that familiar with your IDE.

With the code you have written, it shouldn't even give you enough time to read what you wrote. That is to say, your console window is meant to close immediately because you didnt provide a way to pause it.

So its possible your output already showed but was in a flash (as it is meant to be)

Im actually marveled you have a run-time of 19 seconds.

I think this might be a system error.
If i may suggest, try ising "\n" instead of -endl- so it doesnt have to flush your string.

if that doesnt work, try this code instead:
___________________________________________________________

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>

using namespace std;
inline void keep_window_open(){char ch; cin>>ch;} /*This line will help let you read what you have written*/

int main()
{
cout<<"Hello world \n";

keep_window_open();
return 0;
}
___________________________________________________________

Just copy and past the code
___________________________________________________________
I know a lot of it might seem abstract, especially the header files. but just ignore that for now. From time to time they will come in handy (even when you dont know) and with time you will understand.

if i may advice you also, forget about youtube learning. it only gives you a very basic (and usually wrong) understanding of the language. I tried youtube to learn C, C++ and visual basic, I ended up never really grabbing the concept behind programming.

But some weeks ago, i got a book by the creator of C++, and believe me, in less than a month I learned a lot more than I did from youtube in over a year.

Im glad to see someone who also is a newbie. I was advised to share my programming experience with people to improve but i dont know much people around here who are interested. So i hope maybe we could share ideas and help each other sometime.

If you are interested in the book, message me and i send you a link to download it. You dont have to pay a dime.

Hope some years from now, we can both say proudly "WE ARE PROFESSIONALS"

Good luck in your endeavors!!
AFAIK '\n' does flush the stream as well.
system ("pause");


try that?
closed account (EwCjE3v7)
It's something worn with your compiler ...uninstall your compiler and IDE (code::block). Than go to the website again and download the code blocks with ming g++.. That should fix it
@megiflipz code::blocks keeps the window open automatically.
@kefin \n is just a new line it doesn't flush like std::flush or std::endl , std::endl is a \n and std::flush combined.
@thefail93 please don't suggest system to people
@captainblast I had no problems with the GNU gcc ( mingw ) compiler that came with code::blocks and I have the same version of code::blocks as him
@giblit my recent test seems to claim otherwise. By using a frame limiter, I made a simple loop:

1
2
3
4
5
for (int i(0); i < 1000; ++i)
{
    std::cout << i << "\n";
    ips.limit(); // Just a simple frame limiting class object, set at 30fps
}


Repeating said code without the "\n" part

1
2
3
4
5
for (int i(0); i < 1000; ++i)
{
    std::cout << i << "";
    ips.limit(); // Just a simple frame limiting class object, set at 30fps
}


Suddenly outputs an entire array (0-377... pause ...719... pause ...999 (program exit)), and then waits a bit before it flushes again. Now, what is the definition of "flushing" anyways. I could be wrong here.

I'm using g++ with C::B on Linux Mint 15 with the MATE desktop.

btw the frame limiter is implemented by calculating the time used in the iteration, and subtracted from a required time per frame, which is then used as an argument to std::this_thread::sleep_for(std::chrono::microseconds);
@kefin:

giblit is correct. endl outputs a newline and flushes the stream. "\n" is just a newline without necessarily flushing.

What's probably happening in your case is that your implementation of cout is flushing the stream at every newline, so they happen to be the same for you. Most implementations will automatically flush periodically.


To word that another way:
- "\n" might flush on some implementations, but doesn't have to
- endl is guaranteed to flush on all implementations.
Disch wrote:
- "\n" might flush on some implementations, but doesn't have to


I see, thanks.
Topic archived. No new replies allowed.