Search code examples
c++dev-c++tdm-gcc

(How to) Input 2 Values at Once using Single 'cin' Statement in a Specific format


Is it possible display an input message as follows?

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

Where _ are Input spaces?

Using some sort of the following code??

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

OR

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

Solution

  • Here is the Solution to my problem just in case anyone else is facing it.. Credits go to @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);
    }
    

    Here's the Part of code that Specifies the Format ONLY for 'cin' Statement.

    #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;
    }
    

    Note: This is Simplified/Modified Solution of @qPCR4vir at; Reading in a specific format with cin