基本I / O在Visual C ++ 2010中不工作? [英] Basic I/O not working in Visual C++ 2010?

查看:144
本文介绍了基本I / O在Visual C ++ 2010中不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让一个程序请他们的名字,然后说你好,(他们的名字)!背部。
这里是我的代码到目前为止,getchar()只是暂停,我可以看到输出。

  #include< iostream> 
#include< string>
using namespace std;

int main()
{
string name;
cout<<你叫什么名字?
cin>> name;
cout<<Hello,<< name<<!
getchar();
return 0;
}

这会要求我输入,然后输入我的名字,关闭!
我不知道为什么,如何解决它!请帮助!



编辑:找到如何解决它。完成代码:

  #include< iostream> 
#include< string>
using namespace std;

int main()
{
string name;
cout<<你叫什么名字?
cin>> name;
cout<<Hello,<<< name<<!\\\
;
系统(PAUSE);
return 0; Dietmar给出了正确的答案,你可以在这里找到解决方案。



getchar()已经是一个黑客,但我会让你关闭。用系统(PAUSE)替换它更像是一个黑客,所以我们不去那里。



您的 getchar() 工作,但仍然有 \\\
(记住,你不得不键入 ENTER 提交它!),这是满足 getchar()



您可以删除该ghost换行符:

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

但是,请考虑配置您的执行环境以保留控制台窗口,责任这个。



如果您使用的是Windows操作系统,我会发现 cmd.exe / K myProgram 是有帮助的— / K 运行您的程序,然后保持命令提示符打开


I am trying to make a program to ask for their name, and then say "Hello, (their name)!" back. Here's my code so far, the "getchar()" is just so it pauses and I can see the output.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;
    cout<<"What is your name?:";
    cin>>name;
    cout<<"Hello, "<<name<<"!";
    getchar();
    return 0;
}

This asks me for input, and I input my name, and then the application closes! I don't know why and how to fix it! Please help!

EDIT: Found out how to solve it. Finished code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string name;
    cout<<"What is your name?: ";
    cin>>name;
    cout<<"Hello, "<<name<<"!\n";
    system("PAUSE");
    return 0;
}

解决方案

Dietmar wrote the correct answer, unfortunately as a comment for some strange reason.

getchar() is already a hack but I'll let you off. Replacing it with something like system("PAUSE") is even more of a hack, so let's not go there.

Your getchar() is working, but there is still a \n in the buffer from just after the name (remember, you had to type ENTER to submit it!) and this is satisfying getchar() without further user intervention.

You can get rid of that ghost newline:

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

However, please consider configuring your execution environment to keep the console window, rather than making your program take on responsibility for this. It's there to take user input and give computed output, not to manage terminal windows.

If you're on Windows, I find cmd.exe /K myProgram to be helpful — the /K runs your program then keeps the command prompt open.

这篇关于基本I / O在Visual C ++ 2010中不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆