Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 测试isbn编号以查看其是否有效_C++_Visual Studio_Visual C++ - Fatal编程技术网

C++ 测试isbn编号以查看其是否有效

C++ 测试isbn编号以查看其是否有效,c++,visual-studio,visual-c++,C++,Visual Studio,Visual C++,我的家庭作业有问题,我需要检查ISBN代码是否有效。这是一个初学者C++类,我对这一切都很陌生。现在,不管我输入了什么ISBN,它都告诉我它们都是有效的。如果有人能帮我指出正确的方向,在for()语句中找出我的公式是否正确 我对外部循环没有问题,只是转换我认为我做错了 这是一个C++类,我们仍然只使用基本函数,所以教授建议在ISBN中读取并将其存储在char变量下。 **#include <iostream> using namespace std; int main()* { i

我的家庭作业有问题,我需要检查ISBN代码是否有效。这是一个初学者C++类,我对这一切都很陌生。现在,不管我输入了什么ISBN,它都告诉我它们都是有效的。如果有人能帮我指出正确的方向,在for()语句中找出我的公式是否正确

我对外部循环没有问题,只是转换我认为我做错了

这是一个C++类,我们仍然只使用基本函数,所以教授建议在ISBN中读取并将其存储在char变量下。

**#include <iostream>
using namespace std;
int main()*
{
 int Counter;
 int WeightedSum;

char ISBN[11] = { 0 };
char Choice;
char Again{};
int Sum = 0;
const char X = 10;
int IterationCounter = 0;

do // begininng of do/while loop
{
    cout << "Would you like to check if your ISBN is valid or not? ";
    cout << "Press Y/y for yes or N/n for no and to end program: ";
    cin >> Choice; //choice input
    cout << endl << endl; // blank line 
    while (Choice != 'Y' && Choice != 'y' && Choice != 'N' && Choice != 'n') // beginning of while loop. checks user input to see if valid y/n choice
    {
        cout << "Invalid choice! Enter either Y/y or N/n"; // displays when anything other than y/n is entered 
        cout << "Press Y/y for yes or N/n for no and to end program: "; //gives user another chance to enter y/n
        cin >> Choice;
        cout << endl << endl;
    }
    if (Choice == 'Y' || Choice == 'y')
    {
        cout << "Enter the ISBN you wish to convert: " << endl;
        cin >> ISBN;
        cout << ISBN;
        cout << endl << endl;
        for (Counter = 0; Counter < 10; Counter++)
        {
            WeightedSum = ISBN[10] * (10 - Counter);
            Sum = WeightedSum + Sum;
        }
        cout << Sum;
        if (Sum % 11 == 0)
        {
            cout << Sum;
            cout << " Is a valid ISBN " << endl;
        }
        else 
        {
            cout << Sum;
            cout << " Is invalid " << endl;
        }*
**#包括
使用名称空间std;
int main()*
{
整数计数器;
整数加权和;
char-ISBN[11]={0};
字符选择;
字符{};
整数和=0;
常量字符X=10;
int迭代计数器=0;
do//do/while循环的开始
{
cout Choice;//选择输入

问题出在这条线上

WeightedSum = ISBN[10] * (10 - Counter);

您总是对位置11处的字符执行数学运算。因为您将其乘以
10-计数器
,即0到9,所以基本上是将其乘以1到10的和,或55。由于55可被11整除,因此如果(总和%11==0),您将始终得到
if
评估为true。最糟糕的情况是,您甚至可能遇到访问冲突,因为您在访问ISBN之前没有检查它的长度。

问题是这一行

WeightedSum = ISBN[10] * (10 - Counter);

您总是对位置11处的字符执行数学运算。因为您将其乘以
10-计数器
,即0到9,所以基本上是将其乘以1到10的和,或55。由于55可被11整除,因此如果(总和%11==0),您将始终得到
if
评估为true。最糟糕的情况是,您甚至可能遇到访问冲突,因为您在访问之前没有检查
ISBN
的长度。

ISBN是一个10位数字。它的前9位数字可以取0到9之间的任何值,但最后一位数字有时可能取10。当最后一位数字等于“10”时,ISBN有11位数字。为方便起见,可将其写为“X”

要验证ISBN,请计算第一个数字的10倍,再加上第二个数字的9倍,再加上第三个数字的8倍,依此类推,直到最后一个数字加上1倍。如果最后一个数字除以11时没有余数,则代码是有效的ISBN

您应该首先检查ISBN的长度

这是我的密码:

bool isValidISBN(string& ISBN)
{
    // length must be 10 
    int n = ISBN.length();
    if (n != 10)
        return false;

    // Computing weighted sum  
    // of first 9 digits 
    int sum = 0;
    for (int i = 0; i < 9; i++)
    {
        int digit = ISBN[i] - '0';
        if (0 > digit || 9 < digit)
            return false;
        sum += (digit * (10 - i));
    }

    // Checking last digit. 
    char last = ISBN[9];
    if (last != 'X' && (last < '0' ||
        last > '9'))
        return false;

    // If last digit is 'X', add 10  
    // to sum, else add its value. 
    sum += ((last == 'X') ? 10 :
        (last - '0'));

    // Return true if weighted sum  
    // of digits is divisible by 11. 
    return (sum % 11 == 0);
}

// Driver code 
int main()
{
    string ISBN;
    cin >> ISBN;
    if (isValidISBN(ISBN))
        cout << "Valid\n";
    else
        cout << "Invalid\n";


    system("pause");

    return 0;
}
bool-isValidISBN(字符串&ISBN)
{
//长度必须为10
int n=ISBN.length();
如果(n!=10)
返回false;
//计算加权和
//前9位
整数和=0;
对于(int i=0;i<9;i++)
{
整数位数=ISBN[i]-“0”;
如果(0>位| | 9<位)
返回false;
总和+=(数字*(10-i));
}
//检查最后一个数字。
char last=ISBN[9];
如果(最后一个!='X'&&(最后一个<'0'||
最后一个>‘9’)
返回false;
//如果最后一位数字为“X”,则加10
//总之,否则就要增加它的价值。
总和+=((最后一个=='X')?10:
(最后"0");;
//如果加权和为true,则返回true
//数字的位数可以被11整除。
返回(总和%11==0);
}
//驱动程序代码
int main()
{
字符串ISBN;
cin>>ISBN;
if(ISN(ISBN))

coutISBN是一个10位数字。它的前9位数字可以取0到9之间的任何值,但最后一位数字有时可以取等于10的值。当最后一位数字等于“10”时,ISBN有11位数字。为了方便起见,可以将其写为“X”

要验证ISBN,请计算第一个数字的10倍,再加上第二个数字的9倍,再加上第三个数字的8倍,依此类推,直到最后一个数字加上1倍。如果最后一个数字除以11时没有余数,则代码是有效的ISBN

您应该首先检查ISBN的长度

这是我的密码:

bool isValidISBN(string& ISBN)
{
    // length must be 10 
    int n = ISBN.length();
    if (n != 10)
        return false;

    // Computing weighted sum  
    // of first 9 digits 
    int sum = 0;
    for (int i = 0; i < 9; i++)
    {
        int digit = ISBN[i] - '0';
        if (0 > digit || 9 < digit)
            return false;
        sum += (digit * (10 - i));
    }

    // Checking last digit. 
    char last = ISBN[9];
    if (last != 'X' && (last < '0' ||
        last > '9'))
        return false;

    // If last digit is 'X', add 10  
    // to sum, else add its value. 
    sum += ((last == 'X') ? 10 :
        (last - '0'));

    // Return true if weighted sum  
    // of digits is divisible by 11. 
    return (sum % 11 == 0);
}

// Driver code 
int main()
{
    string ISBN;
    cin >> ISBN;
    if (isValidISBN(ISBN))
        cout << "Valid\n";
    else
        cout << "Invalid\n";


    system("pause");

    return 0;
}
bool-isValidISBN(字符串&ISBN)
{
//长度必须为10
int n=ISBN.length();
如果(n!=10)
返回false;
//计算加权和
//前9位
整数和=0;
对于(int i=0;i<9;i++)
{
整数位数=ISBN[i]-“0”;
如果(0>位| | 9<位)
返回false;
总和+=(数字*(10-i));
}
//检查最后一个数字。
char last=ISBN[9];
如果(最后一个!='X'&&(最后一个<'0'||
最后一个>‘9’)
返回false;
//如果最后一位数字为“X”,则加10
//总之,否则就要增加它的价值。
总和+=((最后一个=='X')?10:
(最后"0");;
//如果加权和为true,则返回true
//数字的位数可以被11整除。
返回(总和%11==0);
}
//驱动程序代码
int main()
{
字符串ISBN;
cin>>ISBN;
if(ISN(ISBN))

你有什么问题吗?“我的for()语句”、“外部循环”、“我的公式”、“转换”-你知道所有这些都是看不见的,不是吗?请将你的代码显示为“读取ISBN并将其存储在char变量下”这听起来是错误的,严格解释为您不能在单个
char
变量中存储一个多位数的ISBN。建议--不要使用所有提示,而是创建一个简单的函数,该函数接受一个ISBN号并返回它是否有效,然后使用简单的,可能是硬编码的输入进行测试。一旦成功,然后将其放入一个更大的程序中。看起来It’好像你花了很多时间在输入/输出上,而不是程序的重要部分。ISBN有两个版本。ISBN-10和ISBN-13。如果你继续使用
char[]
数组而不是
std::string
,那么为了避免缓冲区溢出,你应该使用
cin.get(ISBN,11)
而不是
cin>>ISBN
,或者至少使用
cin>>setw(11)>>ISBN
。但是请注意,这些都考虑了空终止符,因此您应该将11更改为12(如果您想支持ISBN-13,则应将14)