Thread: The disappearing act.

  1. #1
    Student Forever! bookworm's Avatar
    Join Date
    Apr 2003
    Posts
    132

    The disappearing act.

    Hi,
    My problem is that any program that I create involving entering of data by the user,in other words,programs having command line, std::cin>> turn themselves off and the dos window shuts down before the printing of the final calculated answer.
    Note that this is not the case where the window pops for a split second,I am aware of the cin.get(); command.
    For example,

    #include <iostream>
    int main()
    {int x;
    int y;
    std::cout<<"I no.:";
    std::cin>>x;
    std::cout<<"\nII no.:";
    std::cin>>y;
    std::cout<<"\nSum is:<<add(x,y)<<std::endl;
    cin.get();
    return 0; }
    int add(int x, int y)
    {return (x+y) }

    The program will shut itself before showing the sum.However,this will not happen if I assign values to x and y in the program itself.
    Please suggest me a remedy or else all my hopes of learning c++ will be in vain.

  2. #2
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    If you are running the program from the command line, then it should shut down as soon as the program ends, as the program doesn't specifically wait for input after the sum is printed. I don't know what cin.get(); does, but apparently it isn't waiting for anything (maybe it is getting a \0 left in the input stream?). If you are running it from a compiler such as MSVC++, then the compiler automatically stalls the program when it ends, so you can view your input.

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    MSVC++, then the compiler automatically stalls the program when it ends, so you can view your input.

    jus a little edit : )

    MSVC++, then the compiler automatically stalls the program when it ends, so you can view your output.

    cin.get forces the program stop and wait for a user to strike a key. Try including stdlib.h and using system("pause"); right before your return.

    I think JasonD hit your problem already, tho.

  4. #4
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by JasonD
    If you are running it from a compiler such as MSVC++, then the compiler automatically stalls the program when it ends, so you can view your input.
    I wrote "input?" Of course, I meant "output".

  5. #5
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    we all have our typos, im the king of them.

    basically if you have ever used lets say

    getch();

    the cin.get() is doing the same thing (i think, i never did it that way).

  6. #6
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    bookworm, you had numerous errors in your code. Here is the fixed code (I have no idea how your code compiled):

    Code:
    #include <iostream>
    int add(int x, int y);
    
    int main()
    {
    	int x;
    	int y; 
    	std::cout<<"I no.:";
    	std::cin>>x;
    	std::cout<<"\nII no.:";
    	std::cin>>y;
    	std::cout<<"\nSum is:"<<add(x,y)<<std::endl;
    	std::cin.get();
    	return 0;
    }
    
    int add(int x, int y)
    {
    	return(x+y);
    }
    In MSVC++, this still does not wait for user input with the cin.get() command. The reason is that >> does NOT remove the carriage return from the input stream, and when you do not pass any parameters to cin.get(), it takes input until a \0 is found, which is still on the input stream. You can test this by calling std::cin.get() twice in a row - the second one 'works', as the first one grabs the \0 off of the stream.

    You must call cin.ignore(), as this will remove the stray carriage return from the input buffer, before you call std::cin.get()

    (corrected from elad's comment)

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    it's the >> not the <<, but the results the same. another alternative to all those expressed so far is just ask for input into any "dummy" variable, just before the return statement.

    char ch;
    cout << "to end this program press any key followed by enter.";
    cin >> ch;
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Disappearing terrain patches
    By VirtualAce in forum Game Programming
    Replies: 1
    Last Post: 05-17-2008, 11:50 AM
  2. instance of a class disappearing after function ends
    By combatdave in forum C++ Programming
    Replies: 14
    Last Post: 10-20-2006, 08:59 AM
  3. Apps that act "differently" in XP SP2
    By Stan100 in forum Tech Board
    Replies: 6
    Last Post: 08-16-2004, 10:38 PM
  4. HELP! Need mouse to act like it does in FP Shooters!
    By vulcan_146 in forum Windows Programming
    Replies: 5
    Last Post: 04-04-2004, 08:19 PM
  5. ACT scores
    By confuted in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 05-06-2003, 02:17 PM