最近,我一直在尝试从这个网站学习C++ .不幸的是,每当我尝试运行其中一个代码示例时,我会看到该程序打开大约半秒然后立即关闭.有没有办法阻止程序立即关闭,以便我可以看到我努力的成果?
因此,谷歌快速搜索fflush(stdin)清除输入缓冲区会发现许多网站警告不要使用它.然而,这正是我的CS教授教授课程的原因.
使用有多糟糕fflush(stdin)?即使我的教授正在使用它并且似乎完美无缺地工作,我是否真的应该放弃使用它?
我试图按住屏幕上使用头文件我的输出<iostream.h>,但我不知道任何同等功能的getch()和clrscr()功能<conio.h>的<iostream.h>或任何其他C++库.有没有这样的功能?
我在Objective-C类中使用此代码(在Objective-C++文件中):
+(NSString *)readString
{
string res;
std::getline(cin, res);
return [NSString stringWithCString:res.c_str() encoding:NSASCIIStringEncoding];
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我每次都得到一个零长度的字符串.从未有机会在命令行输入.没有.当我逐字复制这段代码时main(),它就可以了.我在Build Settings下有ARC.我不知道它发生了什么.OSX 10.7.4,Xcode 4.3.2.
它是一个控制台应用程序.
我不知道如何在中间停止程序有人可以帮助我我希望我的程序停止定义的秒数,例如 3 秒或 6 秒,如果您按任意键,它会立即开始运行。
#include <iostream>
using namespace std;
int main()
{
cout << "Please enter your name and age\n";
string first_name;
int age;
cin >> first_name;
cin >> age;
cout << "Hello, " << first_name << " (age " << age << ")!\n";
}
Run Code Online (Sandbox Code Playgroud)
如果我输入Carlos对age或任何其他字符串,我收到了0.这是如何工作的(为什么)?
我有这个C ++简单程序;
#include <iostream>
using std::endl;
using std::cout;
using std::cin;
using std::getline;
#include <string>
using std::string;
struct Repository
{
string name;
string path;
string type;
string command;
};
int main()
{
Repository rp;
cout << "\nEnter repo name: ";
cin >> rp.name;
cout << "Enter repo path: ";
cin >> rp.path;
cout << "Enter repo type: ";
cin >> rp.type;
cout << "Enter repo command: ";
getline(cin, rp.command);
cout << "\nRepository information: " << endl;
cout << rp.name << "\n" …Run Code Online (Sandbox Code Playgroud) 标题是自我解释的.出于某种原因,在int main()的循环中,如果用户想要输入另一本书,循环将跳过第一个函数的输入并直接询问作者的姓名.例如:
标题:指环王
作者:JRR Tolkien
版权:1954
输入以空格分隔的ISBN编号:1 2 3 x
签出?(Y或N):Y
你完成了吗?(Y或N):N //这会开始循环,Y会发出一个休息时间
标题://这一行和下一行之间实际上没有空格,应该有
作者://它跳到这一行,不允许用户输入标题,如果用户继续输入信息,这个循环继续 - 总是跳过标题的输入
码:
#include "std_lib_facilities.h"
class Book{
public:
vector<Book> books; // stores book information
Book() {}; // constructor
string what_title();
string what_author();
int what_copyright();
void store_ISBN();
void is_checkout();
private:
char check;
int ISBNfirst, ISBNsecond, ISBNthird;
char ISBNlast;
string title;
string author;
int copyright;
};
string Book::what_title()
{
cout << "Title: ";
getline(cin,title);
cout << endl;
return title;
}
string Book::what_author()
{
cout << "Author: "; …Run Code Online (Sandbox Code Playgroud) 当调用cin >>(int)和cin >>(string)时,当第一个输入对整数不正确时,似乎cin >>(string)将无法检索第二个输入,即使它是正确的字符串.
源代码很简单:
cout<<"Please enter count and name"<<endl;;
int count;
cin>>count; // >> reads an integer into count
string name;
cin>>name; // >> reades a string into name
cout<<"count: "<<count<<endl;
cout<<"name: "<<name<<endl;
Run Code Online (Sandbox Code Playgroud)
测试用例是:
情况1:键入字符(不适合int)和字符
请输入计数和名称
广告
数:0
名称:
案例2:键入数字和字符
请输入计数和名称
30广告
数:30
名称:广告
案例3:键入数字和数字(可以作为字符串)
请输入计数和名称
20 33
数:20
名称:33
char buff[3];
cout<<"From: ";
cin.getline(buff, 3);
//something something
cout<<"To: ";
cin.getline(buff, 3);
Run Code Online (Sandbox Code Playgroud)
如何在评论中清除缓冲区,以便额外的字符不会出现在我的第二个cin?