C++ 如何比较用户c++;
我目前正在制作一个程序,要求用户输入两个整数,并比较它们是否正确。我需要帮助编写这个程序。(对不起,我是c++新手) 例如,这是我想要的输出 输入正整数:123 输入正整数:124 号码1:123 号码2:124 比较次数:3 3-4(不正确) 2--2(正确) 1--1(正确) 到目前为止,我有以下代码:C++ 如何比较用户c++;,c++,integer,compare,C++,Integer,Compare,我目前正在制作一个程序,要求用户输入两个整数,并比较它们是否正确。我需要帮助编写这个程序。(对不起,我是c++新手) 例如,这是我想要的输出 输入正整数:123 输入正整数:124 号码1:123 号码2:124 比较次数:3 3-4(不正确) 2--2(正确) 1--1(正确) 到目前为止,我有以下代码: void twoInt() { int first, second; cout << "\n\nEnter your positive integer : "; cin &g
void twoInt()
{
int first, second;
cout << "\n\nEnter your positive integer : ";
cin >> first;
cout << "\nEnter your positive integer : ";
cin >> second;
cout << "\n\nNumber 1: " << setw(10) << first;
cout << "\nNumber 2: " << setw(10) << second;
// how do i compare each digit that user has entered
//through keyboard and compare them to first and second integer variable
fflush(stdin);
cin.get();
}
void twoInt()
{
int第一,第二;
cout>首先;
cout>秒;
大致轮廓:
使用递归函数
在函数中,获取每个数字的最后一位
d1 = N1 % 10
d2 = N2 % 10
比较它们并产生合适的输出
然后用剩余的号码再次调用该函数:
N1 = N1 / 10
N2 = N2 / 10
当N1
或N2
为零时停止递归。大致轮廓:
void twoInt()
{
int first, second;
int fDigit;
int sDigit;
cout << "\n\nEnter your positive integer : ";
cin >> first;
cout << "\nEnter your positive integer : ";
cin >> second;
cout << "\n\nNumber 1: " << setw(10) << first;
cout << "\nNumber 2: " << setw(10) << second;
while ( (first ) && (second ))
{
fDigit = first % 10;
first = first/10;
sDigit = second % 10;
second = second / 10;
if (fDigit == sDigit )
{
printf(" %d - % d Correct\n",fDigit,sDigit);
}
else
{
printf(" %d - % d Incorrect\n",fDigit,sDigit);
}
}
fflush(stdin);
cin.get();
}
使用递归函数
在函数中,获取每个数字的最后一位
d1 = N1 % 10
d2 = N2 % 10
比较它们并产生合适的输出
然后用剩余的号码再次调用该函数:
N1 = N1 / 10
N2 = N2 / 10
当N1
或N2
为零时停止递归。void twoInt()
void twoInt()
{
int first, second;
int fDigit;
int sDigit;
cout << "\n\nEnter your positive integer : ";
cin >> first;
cout << "\nEnter your positive integer : ";
cin >> second;
cout << "\n\nNumber 1: " << setw(10) << first;
cout << "\nNumber 2: " << setw(10) << second;
while ( (first ) && (second ))
{
fDigit = first % 10;
first = first/10;
sDigit = second % 10;
second = second / 10;
if (fDigit == sDigit )
{
printf(" %d - % d Correct\n",fDigit,sDigit);
}
else
{
printf(" %d - % d Incorrect\n",fDigit,sDigit);
}
}
fflush(stdin);
cin.get();
}
{
int第一,第二;
国际数字图书馆;
国际数字图书馆;
cout>首先;
cout>秒;
coutvoid twoInt()
{
int第一,第二;
国际数字图书馆;
国际数字图书馆;
cout>首先;
cout>秒;
cout使用std::to_string()将两个数字转换为字符串
与您喜欢的算法进行比较:std::equal()或std::mismatch()
为什么不直接将它们作为整数进行比较?使用std::to_string()将两个数字转换为字符串
与您喜欢的算法进行比较:std::equal()或std::mismatch()
为什么不直接将它们作为整数进行比较呢?退一步--你真的关心用户输入了整数吗?看起来你更关心的是用户输入了数字串,你想对数字串进行分析
如果你真的把他的输入读成一串数字,那么程序就会更简单。退一步——你真的关心用户输入的是整数吗?看起来你更关心的是用户输入的是一串数字,你想对一串数字进行分析
如果你真的把他的输入读成一串数字,那么程序就会更简单。提示:123%10==3
和124%10==4
提示:123%10==3
和124%10==4