Page 1 of 2 1 2 LastLast
Results 1 to 20 of 28

Thread: C++ help     submit to reddit submit to twitter

  1. #1
    Title: "HUBBLE GOTCHU!" (without the quotes, of course [and without "(without the quotes, of course)", of course], etc)
    Join Date
    Jul 2006
    Posts
    3,141
    BG Level
    7

    C++ help

    As those of you who read spam or visit Shue's IRC may already know, I really really really suck at programming.

    So someone from the physics department who found out I suck at programming helped me find a good book on how to program.

    I'm only on the second chapter and I'm already stuck. I'm using microsoft visual c++ 2008 express edition. I'm taking a program straight from the book with no modifications at all whatsoever and I'm getting errors.

    Code:
     1  // Fig. 2.1: fig02_01.cpp
     2  // Text-printing program.
     3  #include <iostream> // allows program to output data to the screen
     4
     5  // function main begins program execution
     6  int main()
     7  {
     8     std::cout << "Welcome to C++!\n"; // display message
     9
    10     return 0; // indicate that program ended successfully
    11
    12  } // end function main
    This is a direct copy/paste from the electronic version of this book. When I compile, I get

    Practice.cpp
    c:\documents and settings\lawrenzo\my documents\visual studio 2008\projects\practice\practice\practice.cpp(1) : error C2059: syntax error : 'constant'
    c:\documents and settings\lawrenzo\my documents\visual studio 2008\projects\practice\practice\practice.cpp(3) : error C2014: preprocessor command must start as first nonwhite space
    c:\documents and settings\lawrenzo\my documents\visual studio 2008\projects\practice\practice\practice.cpp(7) : error C2143: syntax error : missing ';' before '{'
    c:\documents and settings\lawrenzo\my documents\visual studio 2008\projects\practice\practice\practice.cpp(7) : error C2447: '{' : missing function header (old-style formal list?)
    Build log was saved at "file://c:\Documents and Settings\Lawrenzo\My Documents\Visual Studio 2008\Projects\Practice\Practice\Debug\BuildLog.htm "
    Practice - 4 error(s), 0 warning(s)
    Four errors. I figured that somehow the numbering was causing them so I changed it to

    Code:
    // Fig. 2.1: fig02_01.cpp
    // Text-printing program.
    #include <iostream> // allows program to output data to the screen
     // function main begins program execution
    int main()
     {
    std::cout << "Welcome to C++!\n"; // display message
    return 0; // indicate that program ended successfully
    } // end function main
    and I get

    Practice - 0 error(s), 0 warning(s)
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    So it would seem that it was successful, but I'm not getting any output at all. It doesn't say "Welcome to C++!" anywhere. If I go to compile instead of build, I get the exact same thing. This problem is probably due to the fact that I have no clue how to use microsoft visual c++ 2008 express. Can anyone either explain to me what I'm doing wrong/how to do it right or tell me about another free C++ compiler that would be easier for a beginner to understand?

  2. #2
    Relic Horn
    Join Date
    Dec 2005
    Posts
    3,363
    BG Level
    7
    FFXIV Character
    Xanthe Celaeno
    FFXIV Server
    Hyperion
    FFXI Server
    Carbuncle

    Your project is working as intended. The only issue is that the console window closes immediately after the program finishes running so you never see the output. Try opening a command window and navigate <project folder> > Debug. Then run <project name>.exe. You should be able to see your output now.

  3. #3
    Ive sucked 27 dicks, in a row.
    Join Date
    Apr 2006
    Posts
    1,569
    BG Level
    6

    No, compile/build doesn't run the program after it finishes compiling, which is his first problem. Go to Debug -> Start Debugging to watch your program run.

    However, as Xanthe posted, it'll print the line and close the program quickly. Change it to this:

    Code:
    // Fig. 2.1: fig02_01.cpp
    // Text-printing program.
    #include <iostream> // allows program to output data to the screen
     // function main begins program execution
    int main()
     {
    std::cout << "Welcome to C++!\n"; // display message
    system("PAUSE");
    return 0; // indicate that program ended successfully
    } // end function main
    The book probably won't explain it, so I will. The system("PAUSE"); line just displays a "Press any key to continue..." and waits for a keypress so you can read the output of the program before it closes.

  4. #4
    Nikkei's Hoe
    Worse than her at uno

    Join Date
    Dec 2006
    Posts
    6,236
    BG Level
    8
    FFXIV Character
    Eanae Hikari
    FFXIV Server
    Gilgamesh
    FFXI Server
    Cerberus
    WoW Realm
    Hyjal

    You know if you ever need help Woozie, there's 5 or 6 of us who know their stuff in Shue's irc channel.

  5. #5
    Title: "HUBBLE GOTCHU!" (without the quotes, of course [and without "(without the quotes, of course)", of course], etc)
    Join Date
    Jul 2006
    Posts
    3,141
    BG Level
    7

    Is there any way to make it print the line and not close up quickly (other than the pause thingy)? Like is there a default setting that would make the window stay open so that I can actually read the text then close the window myself?

    edit:
    Quote Originally Posted by Xanthe View Post
    Your project is working as intended. The only issue is that the console window closes immediately after the program finishes running so you never see the output. Try opening a command window and navigate <project folder> > Debug. Then run <project name>.exe. You should be able to see your output now.
    I don't know how to navigate any folders from the command window.

  6. #6
    The Mizzle Fizzle of Nikkei's Haremizzle

    Join Date
    Feb 2006
    Posts
    22,049
    BG Level
    10
    FFXI Server
    Bismarck

    Quote Originally Posted by Eanae View Post
    You know if you ever need help Woozie, there's 5 or 6 of us who know their stuff in Shue's irc channel.

    ^ Ding Ding Ding! And on Yahoo *cough *cough

  7. #7
    Title: "HUBBLE GOTCHU!" (without the quotes, of course [and without "(without the quotes, of course)", of course], etc)
    Join Date
    Jul 2006
    Posts
    3,141
    BG Level
    7

    You're never online anyways, Mizango D:

  8. #8
    E. Body
    Join Date
    Jun 2006
    Posts
    2,181
    BG Level
    7
    FFXIV Character
    Bro Teampill
    FFXIV Server
    Gilgamesh
    FFXI Server
    Ifrit

    while(1);

    There ya go, infinite pause.

    lol

  9. #9
    Chram
    Join Date
    Jun 2006
    Posts
    2,539
    BG Level
    7

    I don't know.. programming is kind of pointless unless you start young and can figure stuff out by yourself.

  10. #10
    Relic Horn
    Join Date
    Dec 2007
    Posts
    3,411
    BG Level
    7
    FFXIV Character
    Purrrfect Lee
    FFXIV Server
    Hyperion
    FFXI Server
    Cerberus

    Quote Originally Posted by Woozie View Post
    I don't know how to navigate any folders from the command window.
    You just need someone to set up a machine with something like dos 6 on it and hide all your porn on there! Then you'll learn how to navigate in a command prompt lol. I agree the command window closing on exit is kind of annoying for beginning programming.

  11. #11
    Chram
    Join Date
    Jun 2006
    Posts
    2,539
    BG Level
    7

    Quote Originally Posted by Purrrfect View Post
    You just need someone to set up a machine with something like dos 6 on it and hide all your porn on there! Then you'll learn how to navigate in a command prompt lol. I agree the command window closing on exit is kind of annoying for beginning programming.
    Microsoft's IDE doesn't have a console window like every single IDE in the entire world does?

    Maybe he should use something better.

    Example:

    http://img11.imageshack.us/img11/7429/anjuta.png

  12. #12
    Bagel
    Join Date
    Jan 2006
    Posts
    1,428
    BG Level
    6

    Quote Originally Posted by Devek View Post
    Microsoft's IDE doesn't have a console window like every single IDE in the entire world does?

    Maybe he should use something better.
    It does, it just closes when the end of the program is reached. It's not open all the time (maybe changeable).

  13. #13
    Chram
    Join Date
    Jun 2006
    Posts
    2,539
    BG Level
    7

    Look at the picture above, that is what is called a console window.

  14. #14
    Nidhogg
    Join Date
    Oct 2005
    Posts
    3,612
    BG Level
    7
    FFXIV Character
    Glick Wick
    FFXIV Server
    Ultros
    FFXI Server
    Bahamut

    Both Visual Studio 2005 and 2008 have console windows that do not close automatically if you are using the proper settings. You could also just set a breakpoint in your code. (If you're not aware how to do that, learn now. It's invaluable)

  15. #15
    Vic
    Vic is offline
    CoP Dynamis
    Join Date
    May 2006
    Posts
    251
    BG Level
    4

    How many programming languages do you need to learn Woozie?

    First anyways with vs 2008 you don't need to have std:: in front of cin or cout if you put using namespace std; in front of the main() class.

    There are two simple solutions to preventing the console window from closing: first is to place cin.get(); at the end of the code block. This will require enter to be pressed before the program can finish.
    The second and more efficient way, especially with a program that simple is to run the program without debugging.

  16. #16
    Relic Weapons
    Join Date
    Oct 2006
    Posts
    335
    BG Level
    4

    I figured that somehow the numbering was causing them so I changed it to...
    Looking at the way you worded this, do you understand why the numbers were causing it to fail though? The example number looks like you're using a Deitel & Deitel text, which means all of the example code should be formatted the same: the numbers on each line are a reference to be used by you as they describe the contents of each example (and thus, not just something that can be copied directly into the IDE you're using).

    To be perfectly honest, if you've been having a tough time with programming in general (including that recent java experience), you shouldn't be copy/pasting anything right now. Type out the examples--especially while they're still relatively small--and make sure you understand, line-by-line, what it is you're doing with each instruction. You're not doing yourself any favors by choosing to shortcut this early.

    Is there any way to make it print the line and not close up quickly (other than the pause thingy)? Like is there a default setting that would make the window stay open so that I can actually read the text then close the window myself?
    I'm not sure if it works the same way in VS 2008 C++ Express, but in Visual Studio 2005/2008 (the full versions), you don't need to use getline/get at the end of your main function to keep the console window open IFF you are NOT debugging. Use Control-F5 instead of F5 to build and run the program while you're in Visual Studio and it will automatically give you a non-closing console window with a "Press any key to continue . . . " at the end.

    The advice people have given obviously works (though I would avoid anything system() this early), but make sure you understand why it works and not just what the end result is. What does "system("pause")" do? What do "cin.getline" and "cin.get" do?

    I don't know how to navigate any folders from the command window.
    cd directory_name - changes current directory to directory_name

    Most Visual Studio projects by default are stored in a Projects directory somewhere within your My Documents folder. Your project, according to the output, is stored in:

    c:\documents and settings\lawrenzo\my documents\visual studio 2008\projects\practice

    The actual location of the executable you would run is usually in a Debug directory below this:

    c:\documents and settings\lawrenzo\my documents\visual studio 2008\projects\practice\debug\practice.exe

    So, you would open a command window (Start > Run > cmd), and then change your directory to the debug directory by typing:

    cd c:\documents and settings\lawrenzo\my documents\visual studio 2008\projects\practice\debug\

    The prompt changes to indicate your new directory, and then you just run your program:

    practice.exe

    , and the console window stays open after it's done because it wasn't created specifically for that process.

    You can verify the location of your executable by looking at the Output window when you run Debug in Visual Studio. One of the very first lines should be something like:

    'Practice.exe': Loaded 'c:\documents and settings\lawrenzo\my documents\visual studio 2008\projects\practice\debug\Practice.exe', Symbols loaded.

    Both Visual Studio 2005 and 2008 have console windows that do not close automatically if you are using the proper settings.
    I'm pretty sure this is not the case, though I'd love to be wrong. I have never seen a setting that forces the console window to stay open in any version of Visual Studio when in debug.

    First anyways with vs 2008 you don't need to have std:: in front of cin or cout.
    Actually, you do need to specify the std namespace unless you've utilized "using" earlier; in this case the example doesn't. Even if VS2008 did though, it's not good advice to give without also explaining why you would be able to omit it.

    I don't know.. programming is kind of pointless unless you start young and can figure stuff out by yourself.
    Anyone who can solve a problem can learn how to program if they are sufficiently motivated and have the resources available (the latter of which, no excuses can be made for given how ubiquitous they are).

  17. #17
    Relic Horn
    Join Date
    Dec 2007
    Posts
    3,411
    BG Level
    7
    FFXIV Character
    Purrrfect Lee
    FFXIV Server
    Hyperion
    FFXI Server
    Cerberus

    Thanks for the Ctrl-F5 point, I like.

  18. #18
    Ive sucked 27 dicks, in a row.
    Join Date
    Apr 2006
    Posts
    1,569
    BG Level
    6

    Teorem: In the interests of following your own advice, you should mention that Ctrl-F5 isn't just "insert a magic pause", it's also "run without debugging". The "magic pause" on run in debug was indeed a default behavior of some older versions of VC++, but not 2008.

    It also only works when you're running from VS, so you SHOULD learn the very simple system("pause") for situations where you want a pause in the actual program that will execute regardless of whether you're debugging or not.

    Devek: If you're going to bash VS for not having an integrated terminal, at least offer a solution that runs on Woozie's platform. Eclipse with CDT has a nice integrated terminal tab, although I really don't think I'd recommend using it simply because you can't deal with external console windows.


  19. #19
    Vic
    Vic is offline
    CoP Dynamis
    Join Date
    May 2006
    Posts
    251
    BG Level
    4

    First anyways with vs 2008 you don't need to have std:: in front of cin or cout.

    Actually, you do need to specify the std namespace unless you've utilized "using" earlier; in this case the example doesn't. Even if VS2008 did though, it's not good advice to give without also explaining why you would be able to omit it.
    Correct; I saw my mistake and fixed it after realizing it heh.

    Well to explain namespaces, a namespace is a grouping a global classes, objects, functions, variables, and include files contained within a library. By specifying the std namespace with "using", you gain access to the information within the C++ standard lib.

  20. #20
    Relic Weapons
    Join Date
    Oct 2006
    Posts
    335
    BG Level
    4

    Quote Originally Posted by Zosi View Post
    Teorem: In the interests of following your own advice, you should mention that Ctrl-F5 isn't just "insert a magic pause", it's also "run without debugging". The "magic pause" on run in debug was indeed a default behavior of some older versions of VC++, but not 2008.
    I thought I did in the previous sentence where I mentioned it works when you're not debugging, but you're right, I could and should have made that clearer.

    The console preservation on debug isn't in VS2005, though it might have been in 6.0. Not that that really matters anyway since it shouldn't be offered through MSDNAA anymore and no one should really see it in academic environments, much less the workplace (...hopefully.)

    It also only works when you're running from VS, so you SHOULD learn the very simple system("pause") for situations where you want a pause in the actual program that will execute regardless of whether you're debugging or not.
    There's two reasons why I say it's too early to mention system("pause"):

    - It's platform-specific. The call system() may be consistent across platforms for invoking a new shell, but pause is not. If another IDE on another operating system is used--which isn't out of the realm of possibility as development or coursework progresses--it isn't going to work and whoever uses it should understand why (and of course later, how they can get it to work in those environments if they really want/need it).

    - It (in my opinion) can be easily misused and promote poor programming practices, particularly when it comes to debugging. If someone is learning how to use Visual Studio, they should learn how to use breakpoints first to stop the program's execution (and be able to use the IDE to read current values, step through loops, etc.). If debugging isn't involved, learn "get" first and then shell invocation calls.

    I'm not saying it's invalid or a bad suggestion, but I just don't think it's appropriate this early in the learning process, especially without understanding what it does. There's already a lot of black box just-accept-it-works-because-it-does in the initial learning curve.

    Correct; I saw my mistake and fixed it after realizing it heh.
    Sorry, it took a while to compose because I was playing around with Visual Studio and didn't notice the edit after I submitted the post.

Page 1 of 2 1 2 LastLast

Similar Threads

  1. WAR/NIN help plz
    By cyphx in forum General Discussion
    Replies: 18
    Last Post: 2006-04-28, 14:11
  2. Ouch, Call for help on Cassie{/comfort}
    By Deadkennedys in forum General Discussion
    Replies: 3
    Last Post: 2004-09-27, 07:48
  3. Need help on money making methods
    By in forum General Discussion
    Replies: 2
    Last Post: 2004-09-16, 21:57