【问题标题】:How to add char directly after (cin) in c++如何在c ++中的(cin)之后直接添加char
【发布时间】:2018-02-22 15:16:36
【问题描述】:

我试图在用户输入后直接添加一个百分号(这样用户就不必键入百分号)。当我尝试这个时,它要么转到下一行,要么根本不起作用。

我想要什么: _%
// 空白用于用户输入。

对不起,如果这很混乱,我不知道如何在这里添加 c++。

以下是我尝试过的一些事情:

// used a percent as a variable:
const char percent = '%';

cout << "Enter the tax rate: " << percent; // obviously here the percent 
symbol goes before the number. 

double taxRate = 0.0;
cin >> taxRate >> percent; // here I tried adding it into the cin after the cin.

cin >> taxRate >> '%'; // here I tried adding the char itself, but yet another failed attempt...

那么,有没有可能做我想做的事?

【问题讨论】:

  • 这是不可能的(或不可取的)。而且 iostream 库并不是真正用于交互式使用的。
  • 好吧 idk,这正是我们在学校学习的内容,所以我想我想使它对自己用户友好......
  • 不要浪费你的时间 - 这是不可能的。
  • 你必须放弃cin 并使用类似curses 库的东西。尼尔·巴特沃思拥有它的权利。除非作业要求,否则你最好把时间花在可以提高成绩的事情上。

标签: c++ windows visual-studio window


【解决方案1】:

这绝对是可能的,但是iostream 并没有真正提供适当的接口来执行它。通常实现对控制台 io 的更大控制需要使用一些特定于平台的功能。在带有 VS 的 Windows 上,这可以使用 _getch 来完成,如下所示:

#include <iostream>
#include <string>
#include <conio.h>
#include <iso646.h>

int main()
{
    ::std::string accum{};
    bool loop{true};
    do
    {
        char const c{static_cast<char>(::_getch())};
        switch(c)
        {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            {
                //  TODO limit accumullated chars count...
                accum.push_back(c);
                ::std::cout << c << "%" "\b" << ::std::flush;
                break;
            }
            case 'q':
            {
                loop = false;
                accum.clear();
                break;
            }
            case '\r': // Enter pressed
            {
                //  TODO convert accumullated chars to number...
                ::std::cout << "\r" "Number set to " << accum << "%" "\r" "\n" << ::std::flush;
                accum.clear();
                break;
            }
            default: // Something else pressed.
            {
                loop = false;
                accum.clear();
                ::std::cout << "\r" "oops!!                              " "\r" << ::std::flush;
                break;
            }
        }
    }
    while(loop);
    ::std::cout << "done" << ::std::endl;
    return(0);
}

【讨论】:

    【解决方案2】:

    我一直有同样的问题,但我找到了一个替代方案,它不会自动放置 % 符号,但它可以让你在 cin 之后添加 %sign,而不会在运行代码时搞砸:> 这是我的作业,希望它作为一个例子有所帮助: enter image description here 这是输出的样子:enter image description here

    //Program that computes the total amount of savings after being invested
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
        char percent [1];
        float IR, IRp, TC, P, I, A;
        cout << "Investment Rate:" << setw(10) << left << "";
        cin>> IR >> percent;
        IRp = IR*.01;
        cout << "Times Compounded: " <<setw(10)<<""; //TC
        cin>> TC;
        cout<<"Principal:" << setw(13) << right << "$"; //P
        cin>> P;
        A = P*(pow(1 + (IRp/TC), TC));
        I=A-P;
        cout<<"Interest: " <<setw(15)<<"$  " <<fixed<<setprecision(2)<<I<<endl;
        cout<< "Amount in Savings:" <<setw(5)<<"$"<<fixed<<setprecision(2)<<A<<endl;
    
        return 0;
    

    【讨论】:

      猜你喜欢
      • 2013-07-04
      • 1970-01-01
      • 2020-05-17
      • 2010-09-17
      • 1970-01-01
      • 2022-08-11
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      相关资源
      最近更新 更多