C++ 所以我';我在拒绝((a%1)!=0)中的小数时遇到了这个问题
所以我有一个拒绝小数的问题,我拒绝任何字符,但当涉及到小数时,它似乎不起作用。((a%1)!=0)是我使用的方程式 另外,我在do循环中遇到了一个问题,当用户输入“y”或“y”时,它似乎是上一次测试中记录的最后一个值的两倍。我是一名新生,只是想学习更多,所以请帮帮我,伙计们C++ 所以我';我在拒绝((a%1)!=0)中的小数时遇到了这个问题,c++,C++,所以我有一个拒绝小数的问题,我拒绝任何字符,但当涉及到小数时,它似乎不起作用。((a%1)!=0)是我使用的方程式 另外,我在do循环中遇到了一个问题,当用户输入“y”或“y”时,它似乎是上一次测试中记录的最后一个值的两倍。我是一名新生,只是想学习更多,所以请帮帮我,伙计们 #include <iostream> #include <sstream> #include <string> using namespace std; int whatItDoes(
#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 = '\0'; //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 = '\0'; //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;
}
``````
#包括
#包括
#包括
使用名称空间std;
int-whatittos(int*a,int*b)
{
int temp=*a;
*a=*b*10;
*b=温度*10;
返回*a+*b;
}
int main()
{
INTA;
int b;
布尔输入1=假;
布尔输入2=假;
双温;
再次烧焦;
cout推荐:
参考通过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表和字符的数值。打印计算结果a%1
。它可能会给出一些提示a
是一个int
,它不能有小数。你可以读取整行,检查它是否有任何无效字符,然后将其转换为int,如果有效。其他er的想法:我的作业是“首先,重写函数,使其使用指针而不是引用变量”。因此我将其改为指针,并从那里继续。