(如何)使用特定格式的单个 'cin' 语句一次输入 2 个值
(How to) Input 2 Values at Once using Single 'cin' Statement in a Specific format
是否可以显示如下输入信息?
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;
这是我的问题的解决方案,以防万一其他人遇到它..
致谢@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;
}
注意:这是Simplified/Modified @qPCR4vir 的解决方案; Reading in a specific format with cin
是否可以显示如下输入信息?
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;
这是我的问题的解决方案,以防万一其他人遇到它.. 致谢@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;
}
注意:这是Simplified/Modified @qPCR4vir 的解决方案; Reading in a specific format with cin