【发布时间】:2021-04-27 08:26:19
【问题描述】:
我是一个自学者,我正在参加免费的在线课程。我正在尝试将 everyOther 的值放入 an array 中,以便以后可以访问它。我浏览了互联网,但找不到任何足智多谋的东西。你能告诉我如何将 everyOther 的输出值存储到一个数组中。提前致谢。
#include <cs50.h>
#include <stdio.h>
#include <math.h>
long countDigit(long long n);
int main(void)
{
long n;
//This is asking for the input
do
{
n = get_long("Number: ");
}
while(!(countDigit(n)>13));
//Checksum math
long everyOther = 0;
while(n > 0)
{
long lastNumber = n/10;
everyOther = lastNumber % 10;
n = n / 100;
printf("%li\n", everyOther);
}
}
//This function helps us with the counting of the number
long countDigit(long long n) {
return floor(log10(n) + 1);
}
【问题讨论】:
-
标记 C 和 C++ 时要小心。它们是不同的语言。它们都起源于 1980 年代的 C,但都偏离了那个共同的祖先。
-
声明一个数组,声明一个初始化为 0 的索引,将
everyOther存储到当前索引处的数组中,然后增加索引。您对这些步骤中的哪一个有困难?我很难相信您找不到任何将值存储到数组中的示例。 -
@kaylum 如何将 everyOther 存储到当前索引处的数组中?
-
my_array[index] = everyOther; -
理想情况下,您应该从一本好书学习 C++。它涵盖了数组等基础知识!
标签: c++ arrays c cs50 storing-data