所以我在拒绝小数时遇到了这个问题 ((a % 1) != 0)

  • 本文关键字:问题 拒绝 小数 遇到 c++
  • 更新时间 :
  • 英文 :


所以我拒绝小数有问题,我拒绝任何字符,但当涉及到小数时,它似乎不起作用。((a%1(!=0(是我使用的方程式

此外,我在do循环中遇到了一个问题,当用户输入"y"或"y"时,它似乎是上一次测试中记录的最后一个值的两倍。我是大一新生,只是想学习更多,所以请帮帮我,伙计们。

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int whatItDoes(int* a, int* b)
{
int temp = *a;
*a = *b * 10;
*b = temp * 10;
return *a + *b;
}

int main()
{
int a;
int b;
bool input1 = false;
bool input2 = false;
double temp;
char again;
cout << "------------------" << endl;
cout << "solovesa WELCOME " << endl;
cout << "------------------" << endl;
do
{

while (!input1)                                 //Validation                                   
{
cout << "First integer: ";
string line;
cin >> line;
istringstream is(line);
char dummy = '';                           //null termination char marks end of string
if (!(is >> a) || (is >> ws && is.get(dummy)))           //ws meaning 
{
cout << " " << endl;
cout << "WARNING: Characters are not allowed" << endl;
cout << " " << endl;
}
else
input1 = true;

if ((a % 1) != 0)
{
cout << " " << endl;
cout << "WARNING: Decimals are not allowed" << endl;
cout << " " << endl;
}
else
input1 = true;      
}
while (!input2)                                  //Validation   
{
cout << "Second integer: ";
string line;
cin >> line;
istringstream is(line);
char dummy = '';                           //null termination char marksd end of string
if (!(is >> b) || (is >> ws && is.get(dummy)))           //ws meaning 
{
cout << "Characters are not allowed" << endl;;
}
else
input2 = true;
}
cout << "   " << endl;
whatItDoes(&a, &b);
cout << "Final: " << (a + b) << endl;
cout << " " << endl;
cin.clear();
//repeat program prompt
cout << "Would u like to do another set ? (Y/N)";               //ask user if they want to repeat
cin >> again;
//if they say no then exits program but if yes it repeats to top program
} 
while (again == 'Y' || again == 'y');

system("pause");
return 0;
}
``````

建议:通过引用传递a和b。

int whatItDoes(int* a, int* b) --> int whatItDoes(int& a, int& b)

您的问题:如果我正确理解您的代码-ascii字符"。"转换为数值46(谷歌Ascii表(。因此,46%1==0。

如果((a%46(==0(,您可以显式检查小数,但要小心,因为0%46==0(但字符零('0'(的数值为48(。

考虑一下我提到的ASCII表和字符的数值。

最新更新