Output Issue in dev c++ - c++

when i run this code mentioned below, The output console appears for fraction of second and then disappear, but every thing is fine in this code.there is no compile error or warning. i also use get character function but same issue.
i am using dev-c++ 4.9.9.2 version on win 7, 32 bits. what to do?
#include <iostream>
using namespace std;
int main ()
{
cout<<"welcm";
return 0;
}

You can use cin.get() before your return statement to avoid the console window from closing.

You need to tell the program to wait before closing.
cout<<"welcm";
system("pause"); // add this line
return 0;

Related

Terminal can't print vector in c++

I am new to C++ and trying my hands on std::vector. But for some reason even a single std::cout doesn't work with the vector I have declared.
Here is the sample code:
#include <iostream>
#include <vector>
using namespace std;
int main(){
std::vector <int> test_score;
test_score.push_back(10);
cout << test_score.at(0);
return 0;
}
The terminal output shows nothing. Here it is.
The same code works fine on IDEs like CodeBlocks.
Just for your info, your code works fine on my terminal without the image description part.
Maybe try to comment these lines out as they are not involve in the problem or try to compile a Hello World program first may help.

What's wrong with std::strings

Hi I am new to C++ and Code::Block
I am trying to make a simple code to test it, using strings.
when I compile the code there is no problem, but when I try to debug it, Code::Block gives me the following warning:
Cannot open file:
File:../../../../../src/gcc-4.9.2/libgcc/unwind-sjlj.c
Info: "Multiple information windows with the same message have been
supressed."
Image of the error FYI:
Part of the code that gives me an error.
inside main function
#include <iostream>
#include <string>
int main ()
{
std::mystring("What's wrong with strings");
return 0;
}
I realise that this error only occurs when I try to debug a string or a file containing a string.
Any help would be appreciated.
some other information that might help:
Code::Block 16.01
Compiler MinGW gcc4.9.2
Windows 7 Professional 32 bits SP1
First of all, to use strings you must include the file header string. And the name of the type string is..std::string, not std::mystring.
#include <string>
int main(int argc, char** argv)
{
std::string mystring("Nothing's wrong with strings");
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
string mystring = "Whats wrong with my string";
return 0;
}
If you write it in the following way, it should work.
It's safer to define strings like I showed it. It will be also easier for you if you add using namespace std in the beginning of every program if you are new to C++.

Why Dev-C++ does not run my program

I'm learning Cpp programming and I'm using Dev-C++ as compiler. I made this example to see how class & objects works in this programming language but the problem is the compiler does not even running the code! Here's the code:
#include <iostream>
using namespace std;
class BuckysClass{
public:
void coolSaying(){
cout << "Some Sentences" << endl;
}
};
int main(){
BuckysClass buckysObj;
buckysObj.coolSaying();
return 0;
}
I have saved the file with .cpp extension and tried to run it by pressing Ctrl+F10.
Please if u know what I'm doing wrong ,let me know I really appreciate that. Thanks in advance...
First of all:
F9 - compile the source program
F10 - run the source program
In case if your terminal disappears, you can add getchar() before return 0. This will make the command prompt wait for your input, and thus you will be able to see the results.
PS: Don't use Dev-C++. It hasn't been updated for a long time.
You just need to add a getchar() or system("pause") before return 0
Nothing! The command prompt just appears in just 1 second and then
disappear...
Need to add a system("PAUSE"); (I'm supposing you are under Windows)
Try this:
class BuckysClass{
public:
void coolSaying(){
cout << "Some Sentences" << endl;
}
};
int main(){
BuckysClass buckysObj;
buckysObj.coolSaying();
system("PAUSE");
return 0;
}
Try to build it first then run it. also you need to add system("pause") before return 0 to pause the screen so you can see the result.
I highly recommand you use either CodeBlocks or Eclipce rather than Dev-C++.

No console output in simple c++ program

I noticed a weird behaviour of some of my programs in c++ and when I was trying to figure out what coused it, I found out, that something wrong is going on with my console outputs. I used iostream and cstdio functions with the same behaviour. When I print something on console It doesn't display at all. Here are codes that I used for observing this strange behaviour.
This piece of code outputs everything propertly (even if it shouldn't IMO):
#include <cstdio>
using namespace std;
int main(void) {
int a = 0;
scanf("%d", &a);
a++;
printf("result is %d", a);
}
This one however(correct I think) doesn't display anything, only the run finished message
#include <cstdio>
using namespace std;
int main(void) {
int a = 0;
scanf("%d", &a);
a++;
printf("result is %d \n", a);
}
I also tried it with removed space before "\n" with no difference. However, when I place more the same printf functions with "\n" at the end to the program, everything displays correctly (multiple times of course). Iostream behaves in similar way - using endl doesn' t cause anything to appear on console. What am I doing wrong? As to the original code that caused malfunctioning, I noticed that on my output nothing appeared but in my school, the same code output everything correctly. I am working under NetBeans 8.0.2. Thanks in advance for help
All output data is buffered, before it getting printed to console. You can use fflush or \n to flush the output stream and print all data.

how can i execute a file kept on desktop in c++?

my file structure for executing a .exe is something like this
c:\Documents and settings\Desktop\Release\abc.exe
i want to execute this from other c++ program in vb c++ after building, it generates an error that c:\Document is not external or internal command
few lines of code are as follows:
#include<stdlib.h>
#include<stdio.h>
int main( void ) {
int result;
result=system("c:\\Documents and settings\\Desktop\\Release\\abc.exe");
getchar();
return 0;
}
As I suspected when writing an earlier comment, the way to do it is to wrap the entire string in double-quotes. 'Escaping the spaces' sounds non-sensical to me. 25 seconds of googling and I don't see (nor have I heard of in over 20 years) an escape-sequence for a space character in C.
The solution is indeed to include quotes in the string - not to just wrap the string in a single pair of them, as you've done. The following will do the trick:
#include <stdlib.h>
#include <stdio.h>
int main()
{
int result;
result = system("\"c:\\Documents and settings\\Desktop\\Release\\abc.exe\"");
getchar();
return 0;
}
However, that said - you shouldn't really be using the system call for this job. Since you're on a windows machine, you should use the ShellExecute function instead. There are many reasons for this, which I wont go into here, you can look them up yourself. But suffice to say it's an infinitely better way to invoke another program.
More on ShellExecute: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx