C ++-退出程序 [英] C++ - Quitting a program

查看:83
本文介绍了C ++-退出程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第(8)章的 C ++无需恐惧:使您感到聪明的初学者指南中,试图显示文本文件的代码的一部分如下:

In the C++ Without Fear: A Beginner's Guide That Makes You Feel Smart book in chapter(8), part of a code trying to display a text file is the following:

while(1)
{
    for(int i=1; i <= 24 && !file_in.eof(); i++)
    {
        file_in.getline(input_line,80);
        std::cout<<input_line<<std::endl;
    }

    if(file_in.eof())
    {
        break;
    }

    std::cout<<"More? (Press 'Q' and ENTER to quit.)";
    std::cin.getline(input_line,80);
    c=input_line[0]; // <<<<<<
    if(c=='Q'||c=='q')
    {
        break;
    }
}

我没有到这里的部分是:

The part I'm not getting here is:

c=input_line[0];

我认为应该将其读为 Q或 q。但是,为什么要使用这种形式(数组)?而且,没有办法直接读取'Q'或'q'吗?

I think it is put to read 'Q' or 'q'. But, why using this form (Array)? And, isn't there a way to read 'Q' or 'q' directly?

我尝试了 std :: cin>> c ; 但似乎不正确。

I tried std::cin>>c; but seemed to be incorrect.

有什么想法吗?

谢谢。 / p>

Thanks.

推荐答案

因为 input_line 是字符串(中的数组char s),因此 input_line [0] 得到第一个字母-这是为了防止用户写 quit或 Quit ,而不仅仅是 Q

Because input_line is string ( array from chars), so input_line[0] gets the first letter - this is in case, that the user write "quit" or "Quit", not just "Q"

std :: cin>> c; 是正确的,如果您只输入一个 char 并按 Enter

std::cin >> c; would be correct, if you enter just one char and press Enter

这篇关于C ++-退出程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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