Eclipse C++ Runs on Windows Command Prompt - c++

I wrote a simple C++ code. When I click "Run" on Eclipse, first a Windows command prompt appears with my program. After executing it, Eclipse console starts to work. Is there a way to deactivate command prompt and just use Eclipse console?
#include <iostream>
int main(){
int x = 0,y = 0;
std::cout <<"type2 numbers and I will make a magic trick."<< std::endl;
std::cin >> x >> y;
std::cout<<"Ta daa "<<x<<" and "<<y<<" equals to "<<x+y<<std::endl;
return 0;
}
When I erase std:cin part, command prompt appears and dissappears less than a second so I guess everytime I run my code, this command prompt will appear.

Related

Error in Visual Code while running a C++ Code

I was trying to run a C++ program in VS Code.
Program:
#include<iostream>
#include<math.h>
using namespace std;
int main(){
int n;
cout<<"enter the no "<<endl;
cin>>n;
int ans=0,i=0;
while(n!=0){
int bit=n&1;
ans= (bit * pow(10, i) ) + ans;
n=n>>1;
i++;
}
cout<<ans;
}
This program isn't working in my pc. But whenever I try to run same program in online compiler on another pc, It works fine.
Like If I enter the input as 6, then it prints 109 instead of 110.
Why is the program not working properly on my PC?
I also tried to debug to check the program but Debugger prints the output to Debug Console even though ExternalConsole is set to true. From Debug Console, It doesn't take the input. Giving the error, Unable to perform this action because process is running.
Why my vscode prints on Debug Console instead of ExternalConsole?

Using ctrl + z to end reading an array of characters in c++

I want a, to be read until user presses Ctrl+Z. But it does nothing. My guess is that it's visual studios fault, because when I press Ctrl+Z it shows ^Z in console. What should I do? here is my code :
#include <iostream>
using namespace std;
int main()
{
char a[50], b[20];
int n;
while (1)
{
if (NULL == fgets(a, 50, stdin))
break;
cin >> b;
cin >> n;
}
return 0;
}
End of file in an interactive terminal does not depend on the language nor on the OS but on... the terminal and the configuration of the command interpretor.
If you use a true console on Windows running the good old cmd.exe, then Ctrl Z will be seen as an end of file. If you use an IDE, anything is possible and you should try also Ctrl D because some IDE prefere to provide a Unix compatible environment. After all Ctrl Z was the end of file on CP/M (ancestor of MS/DOS) in the 70'...
I tried Enter then Ctrl Z then Enter then next ... and it worked

Why if I input EOF from keyboard Clion don't print on Run window the output of the program?

I just start to using Clion as my IDE and I'm struggling with EOF inputed from keyboard.
If I execute this simple c++ code
#include <iostream>
int main(){
int sum = 0, value = 0;
//read until EOF, calculating a running total of all values read
while(std::cin >> value){
sum += value;
}
std::cout << "Sum is " << sum << std::endl;
return 0;
}
In the Run window of Clion, after I input from keyboard
1 2 cmd+d I get this
1 2 ^D
Process finished with exit code 0
In MacOS the EOF(with keyboard) is Cmd+D.
In CLion is in settable in:
File -> Preferences -> KeyMap -> search EOF and set Cmd+D.
However, I suggest you to use in your code a Terminating Character such as zero that in this case doesn't change the result.
Example of execution in CLion
This is some known issue with CLion on Windows. Disabling the run.processes.with.pty in Registry (open via Find Action) usually helps.
In some environments the EOF emulation is performed using Ctrl+Z combination not Ctrl+D
To avoid such problems - create file with contents you want to enter "1 2"
and run your program from terminal redirecting the standard input to read from file. In this case the EOF event will always happen at the end of the input.
$./test < myinputfile.txt

C++ console program closing before completion.

My Program is a simple sum finder. the code of which I will post at the end.
But it asks for the first number. Upon entering it asks you for a second number.
After entering the second number, the console window closes before showing the results. When I first build and tested in Visual C++ 2010 it ran fine, but this problem only occurs when running the .exe from the build location. Any tips?
Here is the code If testing yourself please re-assemble:
#include "stdafx.h" // for Visual Studio users
#include <iostream>
int main()
{
using namespace std;
int no1, no2, sum ;
cout << "\nEnter the first number = " ;
cin >> no1 ;
cout << "\nEnter the Second number = " ;
cin >> no2 ;
sum = no1 + no2 ;
cout << "\nThe sum of "<< no1 <<" and "<< no2 <<" = "<< sum ;
return 0 ;
}
One way to keep the console open until you're satisfied with it is to add a cin at the end of it - Before closing it will then wait for the user to close it or to enter a line of input.
That's because the window closes when the program is finished running. Use std::cin.get() to keep the window open while it waits for input:
int main()
{
// ...
std::cin.get(); // keep the window open; wait for a character
return 0;
}
Add cin.get() before return 0;
Console applications are actually meant to be executed directly from the console. If you run them directly on it, after the program is done you'll be left with your console window, containing all of the output given by your program. Plus, you'll get a little more used to using the command prompt, which is pretty cool and useful sometimes. :-D
If you have any doubts about how to run a program from the console on a windows environment, take a look at this answer (Compiling C-code from the Command Prompt in Windows?) or maybe this one (How to run a c program using command prompt).

Running a C++ program with cmd

When I run a program through command line, once the program ends, cmd instantly closes, so I can't see the output easily. Is there anyway to stop this from happening so I can actually verify the output?
#include<iostream>
using namespace std;
class Exercises {
public:
void sayHello(int x) {
for (int i = 0; i < x; i++)
cout << "Hello!!" << endl;
}
}exercise;
int main() {
exercise.sayHello(4);
return 0;
}
You can also use cin.get();
It will wait for you to press enter or until you close the program.
Following methods can help in keeping the command window till another input is provided.
#include <conio.h>
void main(){
// your program here
getch();
}
Another way is to use
system("pause"); at the end of your program.
You can pause the execution of the program for a certain amount of time with:
sleep(5); // sleep for 5 seconds
You could place that at the end of the program before return 0;.
If you don't mind waiting for a keypress at the end of your program, you could put something in.
The simplest way in Windows is to do:
system("pause");
Don't do this if you are releasing your software though. You can implement the behaviour of the pause command easily enough.
std::cout << "Press any key to continue . . . " << std::flush;
while( !_kbhit() ) Sleep(25);
getch();
That's using stuff from conio.h.
However, I'm concerned about the cmd shell itself closing. When you say you "run with cmd", are you actually running up a shell, then typing in your program name and hitting Enter? If that closes the shell, then something is wrong. More likely, you're running it by double-clicking the file in Explorer, right?