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

  • Context: C/C++ 
  • Thread starter Thread starter Ascendant78
  • Start date Start date
  • Tags Tags
    C++ Errors Issues
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting issues encountered while using Code::Blocks for C++ programming. Participants share problems related to code execution, input handling, and project management within the IDE, as well as seeking clarification on programming concepts.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant reports receiving no output from their C++ program and a termination error, prompting inquiries about potential issues in their code.
  • Another participant suggests that the program is waiting for user input at the 'cin >> x;' statement and recommends adding a prompt for clarity.
  • A further reply emphasizes the importance of including a newline in the output for better readability.
  • Another participant expresses confusion about the program's requirement for a complete structure, indicating a lack of clarity in the instructional material.
  • A new issue is raised regarding a loop that fails to compile, with participants discussing the need for proper variable declaration and the structure of the program.
  • One participant advises that only one file in a project can contain the main() function, suggesting the need for separate projects for different programs.
  • Concerns are raised about the IDE's behavior, such as multiple black windows opening during execution and issues with file permissions when saving new programs.
  • Participants discuss the importance of familiarity with the IDE and suggest looking for tutorials to better understand its functionalities.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper program structure and input handling, but there are varying opinions on the best practices for managing projects within Code::Blocks. Some issues remain unresolved, particularly regarding IDE behavior and project management.

Contextual Notes

Limitations include potential misunderstandings about the IDE's requirements for project structure and the lack of comprehensive instructional material in the OCW course. Participants express uncertainty about the correct usage of the IDE and the implications of saving files as .cpp versus managing projects.

Who May Find This Useful

Individuals learning C++ programming, particularly those using Code::Blocks as their IDE, may find this discussion helpful for troubleshooting common issues and understanding project management within the environment.

Ascendant78
Messages
327
Reaction score
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;
count << 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
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:

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

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

or

count << x/2 << "\t" << x*2 << endl;
 
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.
 
Ran into yet another issue. This time, the problem is completely different.

for (int i = 0; i < 10;)
{
count << 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.
 
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++)
{
count << i << "\n";
}
return 0;
}
 
  • Like
Likes   Reactions: 1 person
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   Reactions: 1 person
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.
 
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   Reactions: 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.
 

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
5K
  • · Replies 39 ·
2
Replies
39
Views
5K