I must write some C++ on Ubuntu Linux. After many searches I found Qt Creator IDE and G++ compiler. So, the problem is: how to run a C++ program and show console black window? For example, how can I get result from this code :
#include <QtCore/QCoreApplication>
#include <iostream>
using namespace std;
int main()
{
cout << "Hello";
int a[5];
cout << "Enter 5 numbers";
for(int i=0;i<5;i++)
cin >> a[i];
for(int i=0;i<5;i++)
cout << a[i] << " ";
}
I am very in a hurry and I will be very thankful for your answer.
I changed code to this:
#include <QtCore/QCoreApplication>
#include <iostream>
using namespace std;
int main()
{
// QCoreApplication a(argc, argv);
cout<<"Hello";
int a[5];
cout<<"Enter 5 numbers";
for(int i=0;i<5;i++)
cin>>a[i];
for(int i=0;i<5;i++)
cout << a[i] << " " << flush;
// return a.exec();
}
but still isn't working. I enter each number in one line, is it correct? How can I enter data for array in true way?
Well... there is the play button that will run your application. On lowest-right part of QTCreator, there is a tab "3 application output" that will show you the output.
If you want to run the application in a console without QtCreator, go to the folder where you have your source code. There should be the executable that you can run with
./excutable_name
There is a checkbox "Run in terminal" in the run configurations (left rim, "Projects" tab, "Run", "Run Configuration"). If you check that you get an external terminal opened, by default xterm. You can configure another terminal in the lobal setting ("Tools", "Options", "Environment", "General", "System")
Related
I used to make basic program of C/C++ in vs code. After 2-3 weeks. in it's terminal it is not showing variable in output window. after making program when i run code.when i hit enter key it shows in terminal--it is not an error but code is not working.
PC C:\User\Owner\Desktop\C-C++>
WHEN I HIT ENTER IT AGAIN SHOW
PS C:\User\Owner\Desktop\C-C++>
IT DOES NOT SHOW 'ENTER VALUE OF B=='
IN THE TERMINAL OUTPUT WINDOW:-
for ex-:
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "ENTER VALUE OF A==";
cin >> a;
cout << "ENTER VALUE OF B==";
cin >> b;
c = a + b;
cout << "ADDITION OF VALUES" << c;
return 0;
}
IN THE TERMINAL WINDOW IT SHOWS.
ENTER VALUE OF A==5 ( WHEN I PRESS ENTER )
THEN IT SHOWS
PS C:\User\Owner\Desktop\C-C++>5
5
AGAIN
PS C:\User\Owner\Desktop\C-C++>
PLEASE ANSWER THIS PROBLEM :(
Whenever I compiled my C++ program in codeblocks ide, it runs but after once it show error that
"cannot open output file C:\Users\AkM\Desktop\code\g1.exe Invalid argument|" .
I had tried killing process(.exe file) from Task Manager, opening and closing codeblocks but same things happen all the time.
Please help, it really creates trouble for me while coding. This happens with almost every C++ program.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n ,m,k;
cin >> n >> m >> k;
int arr[n];
for(int i = 0;i<n;i++)
cin >> arr[i];
int l = n + m;
vector<int>vec[l];
int x,y;
for(int i =0;i<m;i++)
{
cin >> x>>y;
vec[x].push_back(y);
vec[y].push_back(x);
}
cout << vec[1][3] << endl;
vector<int >vv;
vector<int> v;
for(int i = 0;i<n;i++)
{
for(int j=0;j<vec[i].size();j++)
{
v.push_back(vec[i][j]);
cout << "hello" << endl;
cout << vec[i][j]<<" ";
cout << "hello" << endl;
cout << "hello" << endl;
}
sort(v.begin(),v.end(),greater<int>());
if(v.size()>k)
{
vv.push_back(1);
}
else
{cout << "hello" << endl;
vv.push_back(v[k-1]);
}
v.clear();
cout << endl;
}
for(int i =0;i<vv.size();i++)
cout << vv[i] << endl;
}
ERROR LINK :
This is a common error on IDE and modern systems. Most of the time, when you run a program, the executable file is kept open by the running program, and this is enough to prevent changes to the file => the link phase cannot complete.
The normal solution is just to ensure to close any running instance of the program before a build.
When there is a crash in a program launched from the IDE, the IDE can open the process in debug mode to allow the programmer to see what could have happened. In that case you must close that debug session before a new build.
Create a new project and by default it will create a main.cpp file in the project. Try to build the project and see if you face errors. If you don't see any error, replace the main.cpp contents with your code and build the project again. It did not result in runtime error for me and a console opened when I ran your program.
I used visual studio 2013 two days ago.
But now... It does not working.
I tested below c code and cpp code.
//#include <stdio.h>
#include <iostream>
using namespace std;
void main(){
//printf("hi");
//cout << "test" << endl;
int a;
cin >> a;
cout << a;
}
The result of run is just blinking console.
And does not appear "Press Enter..."
result
If I try to debug then Visual studio stop and no response.
I do reset all options :(
Your console is showing the output but not waiting for you to see it.
you need to add break point or for workaround you can use
cout << a;
int ch = std::cin.get();
}
you must be able to see the value of a.
EDIT: ohh you would need fflush too, my fault too miss it.
cout << a;
fflush(stdin);
int ch = std::cin.get();
}
I am trying to learn basic C++ after being a Java developer. So I decided to give CLion a try. I wrote this basic code just to familiarize myself with some C++ syntax.
#include <iostream>
using namespace std;
int main() {
string word;
cout << "Enter a word to reverse characters: " << endl;
getline(cin, word);
for(int i = word.length(); i != -1; i--) {
cout << word[i];
}
return 0;
}
The code is functional. It reverses whatever word you input. I wanted to step through it to see variables and what not, and to test out CLion's debugger.
My problem occurs when I get to
getline(cin, word);
When I step onto this line, I enter a word and hit enter. Then step over. After I do this nothing happens; all the step over, in, etc. buttons are disabled. I am unable to continue through the loop, or run the remainder of the code.
I have used Eclipse's debugger many times for Java development without any issues. Any ideas could be helpful.
TL;DR How do I step through a C++ command line program with basic input and output using CLion?
I've replicated the problem - looks to me like when debugging the newline is being swallowed by the IDE and not passed back to the program. I've submitted a bug to JetBrains. I don't see a way to work around this aside from getting out of the IDE and debugging directly with GDB or another IDE.
UPDATE: This issue was fixed in the Clion EAP Build 140.1221.2. It even made the first change listed in the release notes:
The most valuable changes are:
Debugger doesn’t hang on ‘cin >>’ operator any more.
Looking at your code, if everything is correct, you need to add #include <string>.
When I run this, it compiles and completes the output.
#include <iostream>
#include <string>
int main() {
std::string word;
std::cout << "Enter a word to reverse chars: ";
std::getline(std::cin, word); //Hello
for (int i = word.length() - 1; i != -1; i--) {
//Without - 1 " olleh"
//With - 1 "olleh"
std::cout << word[i];
}
std::cout << std::endl;
system("pause");
return 0;
}
Use the following code. I have modified your code to make it workable for your purpose. :)
#include <iostream>
#include <string>
using namespace std;
int main() {
string word;
cout << "Enter a word to reverse characters: " << endl;
getline(cin, word);
for(int i = word.length() - 1; i != -1; i--) {
cout << word[i];
}
printf("\n");
system("pause");
return 0;
}
I'm new to c++ I'm trying to do a simple programme that calculates the weekly pay. See source code below. When I run the programme it lets me enter the workHours but when I click enter the programme closes n does not proceed to the rest of the code. Not sure where I'm going wrong please help.
#include <iostream>
using namespace std;
int main ()
{
int workDays;
float workHours, payRate, weeklyPay;
workDays = 5;
payRate = 38.55;
cout<< "Enter the number of hours worked: ";
cin >> workHours;
cin.ignore ();
weeklyPay = workDays * workHours * payRate;
cout << "Weekly Pay = ";
cout << weeklyPay;
cout << '\n';
return 0;
}
The DevC++ doesn't pause the program after it ends you have to add this feature by yourself:
First include conio.h
#include <conio.h>
Then at the bottom of the main add getch()
getch();
What compiler you are using as i am to run this code?
I am using visual studio 10 as IDE and it is working for me.