CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2011
    Posts
    2

    why my code doesnt work

    #include <iostream>
    #include <iomanip>

    using std::cout;
    int main()
    {
    char newline = '\n';
    cout << newline;
    cout << "\We\'ll make our escapes in sequence\", he said.";
    cout << "\n\tThe program\'s over, it\'s time to make a beep beep.\a\a";
    cout << newline;
    return 0;
    }




    it just beep doesnt show the output line it flashes cmd then beep its gone.i tried f5 and ctrl f5

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: why my code doesnt work

    It is working. You haven't given it any reason to keep the command window open, so it doesn't. There are threads on this every week or so. Try running it from the command line or putting a breakpoint before your return.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: why my code doesnt work

    Quote Originally Posted by xxvoodooxx View Post
    #include <iostream>
    #include <iomanip>

    Code:
    using std::cout;
    int main()
    {
    	char newline = '\n';
    	cout << newline;
    	cout << "\We\'ll make our escapes in sequence\", he said.";
    	cout << "\n\tThe program\'s over, it\'s time to make a beep beep.\a\a";
    	cout << newline;
    	return 0;
    }
    it just beep doesnt show the output line it flashes cmd then beep its gone.i tried f5 and ctrl f5
    This code makes the following output in cmd window:
    Code:
    We'll make our escapes in sequence", he said.
            The program's over, it's time to make a beep beep.
    The reason you don't see it is the window closes just after return statement.
    Try to set the breakpoint at the line with "return" and then press F5.
    Or add system("pause"); statement.
    Victor Nijegorodov

  4. #4
    Join Date
    Jan 2011
    Posts
    2

    Re: why my code doesnt work

    the pause work..funny how my book doesnt write it in every exercise i do and it never tells me to write system("pause");

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: why my code doesnt work

    Quote Originally Posted by xxvoodooxx View Post
    the pause work..funny how my book doesnt write it in every exercise i do and it never tells me to write system("pause");
    Because that isn't technically the correct thing to do. It's a hack to make it easier for students to see outputs in their first few programs, that's all.

    When you open Firefox, you don't expect the program to quit until you select "Quit" from the file menu. But if you open a command prompt and type "dir" (or "ls" in linux), you *do* expect the ls program to quit immediately after running without waiting.

    You're creating a console app, which means your program is going to behave like dir, not like Firefox. Once it's over it exits. If the console was opened by the program start, then it will go away upon program end. (If you opened it yourself and manually ran the program it wouldn't disappear.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured