Why am I getting no output in Code::Blocks for my C++ program?

In summary: I was looking at said that when I press F9 to build and run, it would open two windows. But after I hit F9 it only opens the first window and doesn't let me hit any other keys to run the program. I've tried it multiple times and it's the same result.I'm not sure what you are trying to do, but it sounds like you might be running into problems with the MinGW compiler. It seems like maybe when you hit F9 it tries to load the Code::Blocks program, but it is not currently running because the MinGW executable is not currently running. You can try running the MinGW executable directly instead of through Code::Blocks. Alternatively
  • #1
Ascendant78
328
0
I am trying to do an introductory course on C++ through OCW (MIT). However, there is this one they tell you to try that I keep running into a problem with. This is the information they tell us to input:

#include <iostream>

using namespace std;

int main (){
int x;
cin >> x;
cout << x/2 << "\t" << x*2;

return 0;
}

-----------------------------

For some reason, I get no output. It seems like it is going to run, but nothing pops up in the window. After I close out the small dos window (or whatever the small black window that opens is called), I get this message at the bottom of Code::Blocks "Process terminated with status -1073741510 (2 minutes, 25 seconds)" Of course the time depends on how long I let it run, but obviously I'm doing something wrong. Can someone tell me what please? Oh, and I am running Windows 7 and have the latest Code::Blocks and MinGW running on my system.
 
Technology news on Phys.org
  • #2
I've never used Code::Blocks, but from looking at your code, I suspect the program is simply waiting at the 'cin >> x;' statement for you to enter a number. Up to that point, you haven't told the program to display anything, so that's why you don't see anything.

When you see the small black window, simply enter an integer on the keyboard and see what happens.

Then, try adding the following statement before the 'cin >> x;' statement:

cout << "Please enter an integer: ";
 
Last edited:
  • Like
Likes 1 person
  • #3
@jtbell's answer is almost certainly right. Another nitpick(?) -- remember to add a newline at the end of the printed output. You can use

cout << x/2 << "\t" << x*2 << "\n";

or

cout << x/2 << "\t" << x*2 << endl;
 
  • #4
Thanks for the information. The OCW never said exactly what the code did, so I had no idea it was waiting for input from me on the black window. Appreciate the help. Also, thanks FactChecker for your feedback as well. I haven't seen endl yet.
 
  • #5
Ran into yet another issue. This time, the problem is completely different.

for (int i = 0; i < 10;)
{
cout << i++ << "\n";
}

Those are the only 4 lines of the code and it tells me the code should output 0 to 9, but I instead get error messages. It tells me "i does not name a type" and "expected unqualified-id before ')' token." What am I doing wrong? I triple checked the code and even deleted it and re-wrote it, but I keep having the same issue.
 
  • #6
I assume that you are just not showing the surrounding lines of code. Sometimes compilers have options turned on that are more strict about programming standards, even if the statement is legal. Try declaring i as integer before the loop and also try incrementing i in the usual way. See if one or both make a difference.

#include <iostream>
using namespace std;
int main (){
int i;
for ( i = 0; i < 10; i++)
{
cout << i << "\n";
}
return 0;
}
 
  • Like
Likes 1 person
  • #7
If you want to run a snippet of code like this, you have to included it in a complete program.

Try the same "wrapper" as your first example:
Code:
#include <iostream>

using namespace std;

int main (){

... your snippet of code goes here ...

return 0;
}

Most of the examples in books on programming only show the lines of code they are talking about, not a complete program.

When starting to learn programming you have to accept some things (like this code wrapped round your snippet) as "magic stuff to make it work", but as you progress you will learn what all of the "magic" actually does, and why it is needed.

EDIT: FactChecker's suggestion about rewriting the code are unnecessary, and (again for reasons that you will learn later on) arguably not good programming style here, even though they "work". The code as you gave it is standard C++.
 
Last edited:
  • Like
Likes 1 person
  • #8
Ah yes, I see now that the program wasn't complete. It wasn't clear until examples a few pages later on into the book that they weren't include complete codes at that point, but just examples of what the code would look like. Thanks to both of you for the help.
 
  • #9
One other issue I'm running into... When I press F-9 to build and run my program, it opens two black windows. Once the first one finishes running through my program, it immediately closes. The second one waits until I press a key to close out. Can anyone tell me why this is happening?
 
  • #10
Ok, yet more issues again. The OCW I'm working on told me to save various programs as different names ending in .cpp. So, I have kept the same project open, but have been saving the different programs as different .cpp files. However, when I tried to run any new programs, it kept opening one of the older ones in the same project, even if I don't have it open in the window. I tried a few things, like "rebuild" and "compile current file," and now it is giving me an error saying "permission denied."

I looked around the net and can't seem to find how any of this works? Is there maybe some kind of basic guide as to when I should be saving something as a .cpp, when I should save it as a project, etc? I just really don't know what the difference is at this point, hence I have no idea what I'm doing wrong?
 
Last edited:
  • #11
Looks to me like your questions are not related to the C++, but to the environment (OS, compiler, IDE) you are working in. What is the environment you work with?

Having several files open in a project is OK, but only one of them can contain the main() procedure, which is a programs starting point. You need a separate project for each program.
 
  • Like
Likes 1 person
  • #12
You're using Code::Blocks. Does the OCW course assume you're using Code::Blocks? If not, then you should look for a tutorial on using Code::Blocks and become at least somewhat comfortable with it before getting too far into the OCW course. I tried a Google search for "Code::Blocks tutorial" and saw several plausible-looking candidates.

I don't know how the OCW course works, but there are so many different programming environments that most programing textbooks don't assume a specific one. When I taught introductory programming, I supplemented the textbook with my own handouts and lab exercises about the mechanics of writing and running a program on our system.
 
  • #13
Borek said:
Looks to me like your questions are not related to the C++, but to the environment (OS, compiler, IDE) you are working in. What is the environment you work with?

Having several files open in a project is OK, but only one of them can contain the main() procedure, which is a programs starting point. You need a separate project for each program.

My OS is Win 7, compiler is MinGW, and IDE is Code::Blocks (hopefully I categorized those right).

Thanks for the info about the separate projects. The OCW I am using never explained that.
 
  • #14
jtbell said:
You're using Code::Blocks. Does the OCW course assume you're using Code::Blocks? If not, then you should look for a tutorial on using Code::Blocks and become at least somewhat comfortable with it before getting too far into the OCW course. I tried a Google search for "Code::Blocks tutorial" and saw several plausible-looking candidates.

I don't know how the OCW course works, but there are so many different programming environments that most programing textbooks don't assume a specific one. When I taught introductory programming, I supplemented the textbook with my own handouts and lab exercises about the mechanics of writing and running a program on our system.

Yes, the OCW actually suggested we get the exact setup that I have (Code::Blocks with MinGW). However, I am realizing that like with most OCW from MIT, this material seems to be incomplete. There is either explanations given in lecture that aren't covered in the online material or other documentation provided that isn't available on the site. So, while their course is pretty decent in many ways, it is not comprehensive enough for me to get a solid foundation.

I'll go ahead and search around for some tutorials and see what I can find. I also have a textbook that was suggested, but I was hoping to focus on the OCW first.
 

FAQ: Why am I getting no output in Code::Blocks for my C++ program?

1. What is the most common type of C++ error?

The most common type of C++ error is a syntax error. This occurs when code is written in an incorrect format or structure, such as missing a semicolon or using the wrong type of brackets.

2. How do I debug C++ errors?

To debug C++ errors, you can use a debugger tool such as GDB or Visual Studio. These tools allow you to step through your code and track the values of variables to identify where the error is occurring.

3. What is a segmentation fault in C++?

A segmentation fault in C++ occurs when a program tries to access memory that it does not have permission to access. This can happen due to a variety of reasons, such as accessing a null pointer or going out of bounds in an array.

4. How can I prevent C++ errors in my code?

To prevent C++ errors, it is important to follow good coding practices such as using comments, writing clear and organized code, and testing your code frequently. Additionally, using a debugger and carefully checking for syntax errors can also help prevent errors.

5. Can I use try-catch blocks to handle C++ errors?

Yes, you can use try-catch blocks to handle C++ errors. This allows you to catch and handle specific types of errors, such as exceptions, in a controlled manner. However, it is important to properly handle the error or pass it on to be handled by another part of your code.

Similar threads

Replies
22
Views
3K
Replies
8
Views
2K
Replies
6
Views
10K
Replies
118
Views
7K
Replies
5
Views
2K
Replies
39
Views
4K
Replies
40
Views
3K
Replies
10
Views
2K
Back
Top