Beginner's Mistake - c++

I know this is probably a dumb question but I am a beginner and I just started learning today. I am using Dev C++ and I wrote my first code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World" ;
return 0;
}
I click to compile and run. Nothing comes up. Then I click just "run" and it said it is not compiled yet.
I think there might be errors and I would have gladly fixed them myself but I don't know where I can see the errors in Dev C++.
Could this be a compiler error or did I mess up something in my code?
Thanks!

Most likely your program exits before it manages to write everything out to the console. Try adding new line to the output, like this:
cout << "Hello World" << endl;
When you write to cout, the data is not transfered to the screen right away, out of efficiency considerations. Writing to the screen is relatively slow, so the program prefers to do it in "bursts". The text is accumulated in a buffer until a special command is given to flush the buffer, or the buffer fills up. Writing out endl forces a flush, so the output will appear on the screen before your program exits.

Related

Why is my C++ code outputting NSString literals rather than plaintext?

I know this question seems odd, as NSString doesn't exist in C++. Yet, for some reason, when I run the following code:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
I see the following output:
#"Hello, World!\r\n"
The C functions printf and puts show similar behavior. I also see the same output if I store the text as a std::string and then display that.
I have no idea how to even start troubleshooting this. What is going on?
I figured it out a while ago but I forgot about my own question.
In the debug window, it just displays output like that. It only gets formatted like that in debug mode; if I compile it and run the executable from the command line, then it works as expected.

Why is the console asking for input even though there is no input in code?

I'm taking an online computer science class for grade 12 and we're using c++. I've never touched c++ and I'm starting to wish I never had. The teacher is comparing c++ to java (a language I can use perfectly fine) and we're currently learning how to input and output strings and chars. The simple practice problem was
Use one of fputc(), putc(), or putchar() to print your name one char at a time.
Since I have no clue how to use fputc() or putc() I decided to go with putchar()
#include <iostream>
using namespace std;
#include <stdio.h>
int main() {
cout << "My name is :" << endl;
putchar('J');
putchar('a');
putchar('c');
putchar('o');
putchar('b');
return 0;
}
I tried just using putchar(), and then added the cout, and have tried restarting eclipse, etc. but every time I run the program, the console asks for an input. There should not be an input for this program at all.
Try running your program from outside of the IDE and see what happens. When you launch a console program from inside of an IDE, a new console window is created to run the program in. When the program ends, the console window will close. Many IDEs setup the console to wait for you to press a key, giving you a chance to see the program's output, before the window closes.

How does a program interact with another program?

Alright, I'm a little bit (honestly way too) confused about how the heck I can make a program interact with another program.
For example, let's say a game, a shooter, when you run an external program and you make your character not able to die, or immediately shoot when detects an enemy, etc...
I was reading a little bit about it, and they say you have to know how the "target" is composed. But I still don't get it.
For example, let's say we've got a simple code like this:
#include <iostream>
#include <windows.h>
int main() {
for(int h = 0; ; h++) {
std::cout << "The H's value is: " << h << std::endl;
Sleep(1000);
}
return 0;
}
Then, how do I create another program where I can change the H's value to zero everytime I press any key?
Don't get me wrong, I ain't trying to hack anyone or anything, I'm just curious about how those programs work.
(Sorry if I've got some grammar issues, English isn't my native language).
Specific to your program in the exapmle if we take that the program is already compiled and you are not allowed to make any changes to the source code the solution would be to build a program which will run with high enough privileges to examine this process's memory and directly change the in-memory value of h, which should be on the top of the stack(or almost).
Speaking of some more "legal" ways to do so you should check you should read about inter process communication which can be done with multiple methods. Read this.
However most "Bots" and programs which help cheaters in games are in many cases graphics based and are able to analyse the image and thus help aim. On the other hand some "recoil reducers" simply move your mouse in the opposite direction of the recoil of the gun in game. So there is a ton of approaches to your question and for every particular case the answer might be different.

Coloured output in Turbo C++

My compiler is Turbo C++ v3.0 with DOS v5.0 emulated in DOSBox v0.74
I use this because Turbo C++ is the compiler with which
my highschool has chosen to teach the C++ programming language.
It has been stressed that I use this compiler while coding my final term project.
I'm running Windows 8.1 (64 bit) with Intel Core i5-3317U CPU # 1.70GHz
For the sake of liveliness and in tribute to popular culture,
I want my output screens to have green text.
The following is what seemed to work :
#include<iostream.h>
#include<conio.h>
void main(){
clrscr();
textcolor(2); // text set to green colour (conio.h function)
cprintf("\n\t Hello World"); // cprintf from conio.h
cout << "\n\t Hello World"; // cout from iostream.h
getch();
}
The output of which is as follows (screen has been trimmed to save space on this post):
According to the Help Section in Turbo C++,
cprintf() sends formatted output to the text window on the screen.
As you can see, the text printed onto the screen by cout is not green and my project is composed of a lot of cin and cout and some writing and reading files.
The result I desire can (although I have not yet tried) most likely be obtained by replacing all my cout << "..."; with cprintf("...");
but I've written so many cout statements that it's going to be hard to edit the code that much.
cprintf is new territory for me and I'm slightly set aback by how
cprintf("\t"); is outputed as o
So, I'm not to keen using this. I don't wish to use this until and unless it is my only option.
The libraries cstdlib.h and windows.h are unavailable in Turbo C++ and hence I can't use their utilities to get what I want either.
In the end, all I want is that output prompt to display the text I've couted in bright green. Minimal change to my code would be nice.
I wouldn't even mind having to change some settings of my emulator or compiler or shell to do it.
All help is much appreciated. Thank you in advance =)
Ah, the 1990s called, they want their QEMM back :)
The one solution I can think of is to put this in your CONFIG.SYS:
DEVICE=C:\DOS\ANSI.SYS
and then output ANSI escape sequences.
You can use the constream library for colored text output:
#include <constrea.h>
int main()
{
constream cout;
cout << setclr(2);
cout << "\n\t Hello, World!" << endl;
getch();
return 0;
}
I don't know what to do about the tab character.
you just need to add the clrscr(); function after the textcolor(); and it works with the couts

How do I input data using the eclipse console? (c++)

I'm trying my hand at c++ and am using fedora eclipse (3.4.2) as my IDE.
At the moment I'm trying to enter a string of numbers into the console, get the program to sort them and spit them back out. The program is straight out of a book and works with xcode and through a normal terminal - so I know it's correct.
Basically I run the program and enter a few numbers into the eclipse console, the numbers are coloured green so I know its accepting the input correctly.
When I hit enter, the console jumps to a new line and nothing happens. When I press control+shift+D, nothing happens. When I press control+d, nothing happens.
I use eclipse for python too, and the console works properly. Just hitting enter inputs data to the program.
Am I missing something here? I've spent the last half hour or so trying to figure this out. Can anyone help me? Thanks.
What version of ecplise and what complier are you using? The following worked for me on Eclipse Ganymede with GCC version 3.4.5:
#include <iostream>
using namespace std;
int main() {
int x = 0;
cout << "Type your input here:";
cin >> x ;
cout << "You entered " << x << endl;
return 0;
}
How does your program know that input has ended? It sounds like it accepts multiple lines of input in the console window. Isn't there some magic case that pops you out of that loop so you can process the input that's been collected?
As other said, without the code there's no answer.