C++ Why am I not seeing output from cout? - c++

Given this little piece of code
//============================================================================
// Name : prwe.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
int main() {
cout << "Hello World" << endl; // prints Hello World
return 0;
}
This code compiles under Eclipse ,but nothing is presented in the Console window .
Any idea what did I do wrong ?
Thanks

The program runs, prints Hello World, and closes before you can see it.
add
cin.get();
before the return 0; and it should be working fine. Then, you will have to hit enter to close the console.
What cin.get(); is doing is requiring user input to continue, and in this case continuing closes the program.

Related

How to not get a black screen when processing c++

Whenever I run my code a command prompt window appears and says "process returned 32762 (0x7FFA) execution time .009 seconds press any key to continue." I looked on google and couldnt find a solution.
I expect it to print "hello world"
here is my code
// my first C++ program
#include <iostream>
int main() {
std::cout << "Hello, World";
return 0;
}

How to clear a specific line in the console window

I'm making a text based top down game in c++,every time I move my player I need to clear the whole console window with system("CLS") and after that print the whole world again. That process is really slow and inefficient. My question is whether there is any function to clear a certain line in the console window that will not affect the rest of the text? For example, look at the code.
Thanks :)
#include <iostream>
#include<string>
#include "windows.h"
using namespace std;
int main()
{
cout << "hello\n";
cout << "world\n";
//Output:
// hello
// world
//Wanted Output:
//
// world
system("pause");
return 0;
}
I expect text to be printed on the screen and then one line will be cleared without affecting the rest of the text
The win32-API includes a function called SetConsoleCurserPosition: https://learn.microsoft.com/en-us/windows/console/setconsolecursorposition.
I used this function a few years ago.

Get rid of space when printing a new line using standard output in c++ and ncurses

Hello I'd like to know how to get rid of the space created
when printing a new line in c++ using ncurses library.
#include <iostream>
#include <ncurses.h>
using namespace std;
int main(){
initscr();
noecho();
cout << "Hello" << endl;
cout << "World" << endl;
endwin();
return 0;
}
I have this output
Hello
----World
(The dashes are the space I mean)
Offhand, I'd expect this output:
Hello
World
since curses puts the screen into raw mode, suppressing the cooked-mode feature that converts output line-feeds into carriage-return/line-feed. If you really want to print
Hello
World
you should use curses calls (which also happen to work with curses output buffering):
addstr("Hello\n");
addstr("World\n");
Besides the misformatted output, mixing cout with curses can cause your output to be sent in a different order than the curses calls. That's what I meant by buffering.

Error in opening .exe release file in eclipse

When i run the .exe release file it shows some kind of awkward error in my IDE.
And when I try to run from outside the IDE as an appilcation it terminates as soon as it opens without showing any output.
I am using Eclipse and MinGw
This is the error message my ide shows.
And i guess there is nothing wrong with my code.
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World" << endl;
cout << "hello Again" << endl;
return 0;
}
This is not an error, you are viewing the contents of the exe file which is not human-readable, but binary data. The output from your application is in the 'Console' window at the bottom of the screen.

writing a file in c++ with Xcode and running with terminal

I'm very new to programming and i bought myself a self help book but the book is designed for windows. I've mostly been able to translate so far but i'm stumped on writing/appending files and running them through terminal. I was wondering if someone could translate these lines for me. these lines are what I'm told to type in command prompt/terminal.
C:\MyPrograms> c++ write.cpp -o write.exe
C:\MyPrograms> write
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string poem = "\n\tI never saw a man who looked" ;
poem.append("\n\tWith such a wistful eye") ;
poem.append("\n\tUpon that little tent of blue") ;
poem.append("\n\tWhich prisoners call the sky") ;
ofstream writer("poem.txt") ;
if (! writer)
{
cout << "Error opening file for output" << endl ;
return -1 ; //signal an error then exit the program.
}
writer << poem << endl ; // write output
writer.close() ; // close filestream.
return 0 ;
}
This is the program i am trying to run named write.cpp please help thanks!
On OS X, the first line in Terminal would be:
g++ write.cpp -o write
The second line would be:
./write
The first line compiles your code and creates an executable called write. The second line runs the executable.