Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Issues with C++ errors?

  1. Jul 27, 2014 #1
    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.
     
  2. jcsd
  3. Jul 27, 2014 #2

    jtbell

    User Avatar

    Staff: Mentor

    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: Jul 27, 2014
  4. Jul 27, 2014 #3

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    @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;
     
  5. Jul 27, 2014 #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.
     
  6. Jul 29, 2014 #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.
     
  7. Jul 29, 2014 #6

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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;
    }
     
  8. Jul 29, 2014 #7

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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 (Text):

    #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: Jul 29, 2014
  9. Jul 29, 2014 #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.
     
  10. Jul 30, 2014 #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?
     
  11. Jul 30, 2014 #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: Jul 30, 2014
  12. Jul 31, 2014 #11

    Borek

    User Avatar

    Staff: Mentor

    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.
     
  13. Jul 31, 2014 #12

    jtbell

    User Avatar

    Staff: Mentor

    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.
     
  14. Jul 31, 2014 #13
    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.
     
  15. Jul 31, 2014 #14
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook