简体   繁体   中英

Why isn't my program displaying the integer I'm outputting?

#include<iostream>  
#include<string>  
using namespace std;  
int main()  
{  
    int i;  
    cout<<"enter ur no. plz";  
    cin>>i;  
    cout<<"ur no. is:"<<i;  
    cin.get();  
    return 0;   
}  

This code is not displaying the integer I entered. It returns back after entering an integer and hitting enter. I am using dev C++.

After the user enters the integer, there is still a newline character left in the input buffer. cin.get() reads that character, then the program immediately ends. You could put an additional call to get if you want the program to stay open. Or, before the call to get, you could have a call to ignore:

std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );

That would clear the newline character from the buffer.

Or you could run your program from the command line, you'll see the output then.

Add some endl s:

#include<iostream>  
#include<string>  
using namespace std;  
int main()  
{  
    int i;  
    cout<<"enter ur no. plz"<<endl;  
    cin>>i;  
    cout<<"ur no. is:"<<i<<endl;  
    cin.get();  
    return 0;   
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM