【问题标题】:(How to) Input 2 Values at Once using Single 'cin' Statement in a Specific format(如何)使用特定格式的单个 'cin' 语句一次输入 2 个值
【发布时间】:2020-10-30 15:51:31
【问题描述】:

是否可以显示如下输入消息?

Enter First Fraction:_/_
Enter Second Fraction: _/_

_ 是输入空格吗?

使用以下某种代码??

cout<<"Enter First Fraction: ";
cin>>N1>>"/">>D1;
cout<<"Enter Second Fraction: ";
cin>>N2>>"/">>D2;

cout<<"Enter First Fraction: ";
cin>>N1>>/>>D1;
cout<<"Enter Second Fraction: ";
cin>>N2>>/>>D2;

【问题讨论】:

  • 不,你不能那样做。您需要与终端进行高级交互才能执行此类操作。我打赌ncurses 可以做到
  • 诅咒?那是什么?
  • 我在另一个堆栈问题中看到了这段代码.. ``` char plus{},img{};双 x{},y{}; cin>> x >> 加 >> y >> img; if (plus!='+' || img!='i') cout
  • 噢,选项卡在 cmets 中消失了。 ://
  • lemme 将该代码作为建议的答案..

标签: c++ dev-c++ tdm-gcc


【解决方案1】:

这是我的问题的解决方案,以防万一其他人面临它.. 致谢@qPCR4vir

#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

main()
{   //This program encourages the user to perform a sum of two fractions.
    int N1, D1, N2, D2, N, D;
    char divide{};
    system("cls");

    cout<<"The Format is: 'A/B' & 'C/D'..\n\n";
    cout<<"Enter First Fraction: ";
    cin>>N1>>divide>>D1;
    cout<<"Enter Second Fraction: ";
    cin>>N2>>divide>>D2;
    
    if (divide=='/')
    {
        N=(N1*D2)+(D1*N2);  //Numerator
        D=D1*D2;            //Denominator
        cout<<"Sum of Both Fractions is: "<<N<<"/"<<D;
    }
    else
    {
        system("cls");
        cout<<"The Correct Format is: A/B & C/D\nWhere these alphabets are Integers..\n\n";
        cout<<"Example: 4/5";
    }

    getch();
    system("cls");
    return(0);
}

以下是仅指定“cin”语句格式的代码部分。

#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

int main() 
{
    char divide{};                  //iota{};
    int x{},y{};
    
    cout<<"Enter Dividion of Two Numbers (A/B): ";
    cin>>x>>divide>>y;              //>> iota;
                
    if (divide=='/')                //&& iota=='i') 
    {
                                    //x=(N1*D2)+(D1*N2);    //Numerator
                                    //y=D1*D2;              //Denominator
        cout<<"The Fractional Form is: "<<x<<"/"<<y;
    }
    else
    {
        system("cls");
        cout<<"The Correct Format is: A/B & C/D\nWhere these alphabets are Integers..\n\n";
        cout<<"Example: 4/5";
    }


    getch();
    return 0;
}

注意:这是@qPCR4vir 的简化/修改解决方案; Reading in a specific format with cin

【讨论】:

    【解决方案2】:

    我在另一个堆栈问题中看到了这段代码..

    #include <iostream>
    using namespace std;
    
    int main() 
    {
        char plus{},img{};
        double x{},y{};
        cin>> x >> plus >> y >> img;
        if (plus!='+' || img!='i') 
            cout << "\nError: "<< "x=" << x <<", plus="  << plus <<", y="  << y <<", img="  << img;
        else
            cout << "\nComplex: " << x << plus << y << img;
    
    
        return 0;
    }
    

    似乎需要用户在中间输入一个“+”和一个“!”最后... 但我试过了,还是不行。

    【讨论】:

    • 该代码希望用户输入1.3 + 2.5 之类的内容,这与您的要求无关
    • 哦是的..真的。让我试试
    • 其实是用来输入复数的。用户可以输入例如3.5 + 1.3 i,然后代码将打印3.5 + 1.3 i(或者当用户输入的格式不正确时打印错误消息)
    • 不清楚这与您的问题有什么关系。如果您希望用户输入分数为3 / 5,那么您可以这样做(我的意思是用户输入这 5 个字符,在用户输入之前屏幕上不会出现/),但这不是您要问的问题
    • 是的,现在我添加了空格并遵循了格式。这正是我在我的程序中想要的。只是格式略有不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2014-07-31
    • 1970-01-01
    相关资源
    最近更新 更多