Thread: Need Help...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    18

    Need Help...

    Hello fellow peeps!!!!
    Ok, here it goes.... I have been messing around with Microsoft Visual Studio for Vista. I have been writing code, simple and advanced, and in some instances when i compile and run the program it will exit way too fast and not let me read the response to the code.
    Here is a simple example in which the code exits before you can even attempt to read the result.
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int gallons, liters;
    
    	cout << "Enter number of gallons: ";
    	cin >> gallons;
    
    	liters = gallons * 4;
    
    	cout << "Liters: " << liters;
    
    	return 0;
    }
    Let me thank you in advance, any help with this will be appreciated.

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    have the program ask for input from the user so it waits for an answer
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    in my computer it does wait for input after you enter it and press enter it just jets thru it and terminates

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    My mantra has always been "run command line programs from the command line". Pop open a dos box, find your program, and run it.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    cin >> gallons; ----> Doesn't this get input from the user?

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    yeah, do that right before you return 0, although it would probably be better to use cin.get() (if that's the right function)
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    ok i added the: cin.get() before the return 0; and i was able to read the result for a split second but still way too fast to accurately read it.

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by francusmag View Post
    ok i added the: cin.get() before the return 0; and i was able to read the result for a split second but still way too fast to accurately read it.
    yeah, it "got" the '\n' that was in the buffer, I use system("PAUSE") anyway
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    lol sorry dont get it.!!!! o.o

  10. #10
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    when you read in for gallons you used the enter/return bar which sent a newline character '\n' to the input buffer, cin >> gallons left that in the buffer and cin.get() grabbed it from the buffer, if you are using windows you can use system("PAUSE"), you might need to include windows.h
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    how would i do this? This is what i have so far:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
     	int gallons, liters;
       
    
    	cout << "Enter number of gallons: ";
    	cin >> gallons;
        liters = gallons * 4;
    
    	cout << "Liters: " << liters;
    	
    	cin.get();
    	return 0;
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What is being suggested is not really a hard substitution here:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
     	int gallons, liters;
       
    
    	cout << "Enter number of gallons: ";
    	cin >> gallons;
        liters = gallons * 4;
    
    	cout << "Liters: " << liters;
    	
    	system("PAUSE");
    	return 0;
    }
    (Technically you need #include <cstdlib> at the top as well, I guess.)

  13. #13
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by francusmag View Post
    how would i do this? This is what i have so far:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
     	int gallons, liters;
       
    
    	cout << "Enter number of gallons: ";
    	cin >> gallons;
        liters = gallons * 4;
    
    	cout << "Liters: " << liters;
    	
    	cin.get();
    	return 0;
    }
    change cin.get() to system("PAUSE")
    If it says it's not defined then include the windows header
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If it says it's not defined then include the windows header
    system() is in cstdlib, not windows.h.

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    ok this does work to perfection pausing the code so that you can read it....
    Thank you guys so much!!!
    Now a new problem arrives... The response to the code after compiling and running is:

    Enter number of gallons: (number entered appears)
    Liters: (answer) (and directly following the last digit of the answer)Press any key to continue...

    Question how can i make that last text after the last digit appear in a new line.

Popular pages Recent additions subscribe to a feed