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

    Beginner in need of help

    Hi, I just started doing C++ on my own and have run into several problems. I don't really have a tutor of any sort to help me out with these basic things, so perhaps you guys could give me some pointers, as well as tell me what the hell is wrong with this:

    Code:
    /*5) Write a complete program that reads an integer from the user 
    (using cin, discussed in section 1.3), doubles it using the doubleNumber()
    function you wrote for question 4, and then prints the doubled value out to 
    the console.*/
    
    #include <stdafx.h>
    #include <iostream>
    
    int doubleNumber (int x)
    {
    		return 2 * x;
    }
    int main()
    {
    	using namespace std;
    	int x;
    	cin >> x;
    	cout << doubleNumber(x) << endl;
    	return 0;
    }
    I've done a little programming (long ago) and a lot of my problems stem from me not knowing what the hell the error is. I'm basically doing this online tutorial www.learncpp.com and the only way that I can keep up is if I use Windows Visual C++. The compiler won't tell me what line the problem is (which was a great help, if I could turn it on or something without using a different IDE that would be great), I can't understand many of the error messages, and I'm stuck with this problem (All I have to do is make the user imput a number, and double that number, and I can't even do that).

    In this case I don't get any sort of compiler error though, I'm able to run the program, but when I type in a number and hit enter the screen disappears and nothing is accomplished.

    Any and all help will be deeply appreciated.
    -Thanks ahead of time

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

    Re: Beginner in need of help

    If the console window opens specifically to run the program, then as soon as the program exits, the window is no longer needed. Odds are the output was produced, but the window closed immediately thereafter.

    Two approaches exist:
    1) Prevent the program from exiting immediately by trying to read something after the last output. This is a common approach for beginners but not recommended for "real" programs.
    2) Open the console window yourself, navigate to the directory containing the built executable, and run it manually. That way the window will have no reason to close just because the program ended.

    If you're using Visual Studio, the "Start Without Debugging" command will automatically do something similar to (1) for you. If you "Start Debugging", you can put a breakpoint on the last line of main to achieve the same result.

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