C 变量指定为 long long 但被识别为 int

标签 c variables cs50 long-long

我正在开发一个程序,检查我正在参加的 CS50 类(class)的信用卡号码的有效性(我发誓这是合法的哈哈),我目前正在努力正确获取每个 CC# 的前两个数字检查它来自哪家公司。为了清楚起见,我已经评论了每个部分的作用,并评论了我的问题出现的地方。

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
int main(void)
{
  long long ccn = get_long_long("Enter CCN: \n");
  int count = 0;
  long long ccn1 = ccn;
  // finds the amount of digits entered and stores that in int count.
  while (ccn1 != 0)
  {
     ccn1 /= 10;
     +count;
  }
  printf("%i \n", count);
  // ln 17- 19 should take int count, subtract two, put that # as the power of 10, 
  // then divide the CC# by that number to get the first two numbers of the CC#.
  long long power = count - 2;
  // here is where i get the error. its a long long so it 
  // should hold up to 19 digits and im only storing 14 max 
  // but it says that 10^14th is too large for type 'int'
  long long divide = pow(10,power);
  long long ft = ccn / divide;
  printf("power: %i \n", power); //ln 20-22 prints the above ints for debug
  printf("Divide: %lli \n", divide);
  printf("First two: %lli \n", ft);
  string CCC;
  // ln 24-35 cross references the amount of digits in the CC#
  // and the first two digits to find the comapany of the credit card
  if ((count == 15) && (ft =  34|37))
  {
    CCC = "American Express";
  }
  else if ((count == 16) && (ft = 51|52|53|54|55))
  {
    CCC = "MasterCard";
  }
  else if ((count = 13|16) && (ft <=49 && ft >= 40))
  {
      CCC = "Visa";
  }
  printf("Company: %s\n", CCC);
}

最佳答案

第一个问题是循环中的+count。这应该是++count。因此,count 保持在 0 且 power = -2。你可以避免所有那些权力的东西。您已经有了循环,您可以使用它来获取前两位数字:

int ft = 0;
while (ccn1 != 0)
{
    // When you are down to 10 <= ccn1 < 100, store it
    if (ccn1 < 100 && ccn1 > 9) ft = ccn1;
    ccn1 /= 10;
    ++count;
}

你的第二个问题是如何进行比较。

if ((count == 15) && (ft =  34|37))

首先,= 是赋值,== 测试相等性。其次,|是按位或,||是逻辑或。第三,你不能像这样测试多个值。正确做法:

if ((count == 15) && (ft == 34 || ft == 37))

关于C 变量指定为 long long 但被识别为 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46855246/

相关文章:

c - while 语句中的无限循环

c - 为什么我的冒泡排序程序的输出每次运行时都会改变?

c - 从输入中获取特定范围的 float

c - 在内置汇编程序例程中使用 EBX 寄存器

c - Openocd:将值写入闪存地址

php - 变量在 PHP 而不是在 Blade 中?

javascript - 有没有办法打印动态变量?

JavaScript 问号? : logical syntax, 防御性编程

c - 如何将空格分隔的 float 从字符串中提取到 c 中的另一个数组中?

c - c中的逻辑运算和预递增