标签: cs50

为什么不是我的可变长度数组转换?

我正在编写将采用整数数组的代码,然后可以将其转换为所需的基数,例如base-16.

出于某种原因,终端通过该程序并打印出来

"转换数="

这是我的代码:

#include <stdio.h>
#include <cs50.h>

int convertedNumber[64];
int base;
int digit = 0;

void getNumberAndBase(void) {
    int size;

    printf("How many numbers to be converted??\n");
    size = GetInt();

    int array[size];

    for (int i = 0; i < size; i++) {
        printf("Number to be converted?\n");
        array[i] = GetInt();
    }

    printf("Base?\n");

    do {
        base = GetInt();

        if (base < 2 || base > 16) {
            printf("Bad base - must be between 2 and 16. Try again!\n");
        }
    } while (base …
Run Code Online (Sandbox Code Playgroud)

c arrays function function-calls cs50

2
推荐指数
1
解决办法
89
查看次数

为什么在C99中不允许隐式声明gets()?

我开始学习C语言编程,这是我所指代的代码,其中显示了一些源代码,gets()而我的IDLE也认识到它。但是仍然在编译时,我的编译器不同意它。

谁能帮我吗?我gets()在main函数中使用clang作为编译器。

c gets c99 clang cs50

2
推荐指数
1
解决办法
5213
查看次数

C - 在函数中使用未声明的标识符.CS50

我正在重写我的CS50信用解决方案以使用功能.定义readCardNumber()时偶然发现错误.

long long readCardNumber()
{
    do
    {
        long long result = get_long_long("Enter card number to verify: \n");
        if (result < 0)
        {
            printf("Retry: \n");
        }
    }
    while (result < 0);

    return result;
}
Run Code Online (Sandbox Code Playgroud)

我使用CS50.h https://reference.cs50.net/cs50/get_long_long来获取号码.由于以下原因,我无法编译此解决方案:

error: use of undeclared identifier 'result'
Run Code Online (Sandbox Code Playgroud)

有人可以体验一下这里的问题吗?我在我的代码开头声明了函数,并在函数中声明和初始化结果.验证该数字的更好方法是什么?

https://docs.cs50.net/2018/x/psets/1/credit/credit.html - 解决方案规范我试图重做.

c validation function cs50

2
推荐指数
1
解决办法
366
查看次数

错误:从“int”分配给“string”(又名“char *”)的整数到指针转换不兼容

我创建了一个包含 26 个字母的数组,我想将每个元素分配给它们的 ASCII 值,如下所示:

ASCII[0] = 65
ASCII[1] = 66
Run Code Online (Sandbox Code Playgroud)

我已经ASCII[0]在数组中定义为“A”。所以我希望的结果应该是:

A = 65
B = 66
Run Code Online (Sandbox Code Playgroud)

那么问题是什么?这是我的代码:

#include <cs50.h>
#include <stdio.h>

#define SIZE 26

int main(void)
{
    string ASCII[SIZE] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

    for(int i=0; i<26; i++)
    {
        ASCII[i] = i+65;
    }
}
Run Code Online (Sandbox Code Playgroud)

c cs50

2
推荐指数
1
解决办法
1225
查看次数

对编译 CS50 代码的 `get_string' 的未定义引用

我是一个完全的初学者,没有任何编码经验。这个问题的大部分答案都是用技术术语,但我无法理解它们。请以更适合初学者的语言提供帮助。

我的代码

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    string answer = get_string("What is your name");
    printf("Hello, %s\n", answer);
}
Run Code Online (Sandbox Code Playgroud)

运行之后,得到了这个。

> Executing task: C/C++: gcc.exe build active file <

Starting build...
C:\msys64\mingw64\bin\gcc.exe -fdiagnostics-color=always -g "C:\Users\MaNaS\Desktop\CS50x\C Code\0002.c" -o "C:\Users\MaNaS\Desktop\CS50x\C Code\0002.exe"
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\MaNaS\AppData\Local\Temp\ccIpKhYX.o: in function `main':
C:\Users\MaNaS\Desktop\CS50x\C Code/0002.c:6: undefined reference to `get_string'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).

Terminal will be reused by tasks, press any key to close it.
Run Code Online (Sandbox Code Playgroud)

我正在使用 VS 代码和 Mingw64。我已经下载了 CS50 库,并将 …

c cs50

2
推荐指数
1
解决办法
7754
查看次数

Cs50 VScode ide 仅在恢复模式下运行

每当我尝试打开 cs50 vscode 工作区时,它都会首先发出以下通知:

由于容器错误,此代码空间当前正在恢复模式下运行。查看创建日志,根据需要更新您的 devcontainer 配置,然后运行“重建容器”命令来重试。

然后预装的工具都不起作用(sqlite3、python 等)

这是创建日志的最后几行:

2022-04-21 01:59:29.258Z: Stop (3 ms): Run in container: cat /root/.vscode-remote/data/Machine/settings.json
2022-04-21 01:59:29.271Z: $ ls /root/.vscode-remote/bin/*/node | head -n 1
2022-04-21 01:59:29.284Z: 
2022-04-21 01:59:29.299Z: ls: cannot access '/root/.vscode-remote/bin/*/node': No such file or directory
2022-04-21 01:59:29.312Z: Stop (21 ms): Run in container: ls /root/.vscode-remote/bin/*/node | head -n 1
2022-04-21 01:59:29.330Z: userEnvProbe: none (default)
2022-04-21 01:59:29.343Z: $55.88679008Z' > '/root/.vscode-remote/data/Machine/.onCreateCommandMarker'
2022-04-21 01:59:29.358Z: 
2022-04-21 01:59:29.371Z: 
2022-04-21 01:59:29.385Z: Exit code 1
2022-04-21 01:59:29.400Z: && echo '2022-04-20T15:04:55.88679008Z' …
Run Code Online (Sandbox Code Playgroud)

ide visual-studio-code cs50

2
推荐指数
1
解决办法
2365
查看次数

我想通过 printf 在 C 中多次使用相同的字符串

我正在做哈佛 CS50,在 C 的第二节课上,老师说 %s 会自动连续打印每个额外的参数。

\n

好吧,那很酷。但是如果我想多次打印第一个字符串怎么办?

\n
#include <stdio.h>\n\nint main(void) {\n   char firstname[5] = "Bruce";\n   char lastname[5] = "Wayne";\n\n   printf("Hi, %s!\\nI am your virtual butler. I will call you Master %s from now on.\\nWelcome to %s Manor!", firstname, lastname);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

但是,它输出:

\n
Hi, BruceWayne!\nI am your virtual butler. I will call you Master Wayne from now on.\nWelcome to  q\xef\xbf\xbd\xef\xbf\xbdU Manor!\n
Run Code Online (Sandbox Code Playgroud)\n

代替:

\n
Hi, Bruce!\nI am your virtual butler. I will call you Master Wayne from now on.\nWelcome …
Run Code Online (Sandbox Code Playgroud)

c printf declaration string-literals cs50

2
推荐指数
1
解决办法
115
查看次数

为什么我的变量在除法时返回一个大负数?

我正在解决 cs50 的信用问题,我对一个部分的 for 循环感到困惑,因为我将一个长整数4003600000000014除以100,它返回一个大负数-1685133312

这是实际的代码:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    long int number;
    long int temp;
    do
    {
        number = get_long("Number: ");
    } while (number < 0);

    temp = number;
    int counter = 1;
    for (int i = 10; i <= number; i = 10)
    {
        number /= i;
        counter += 1;
    }
    printf("%i\n", counter);

    int product = 0;
    int divisor = 100;
    int modulo = 0;
    //printf("%li\n", (temp % 100) …
Run Code Online (Sandbox Code Playgroud)

c integer-division cs50

2
推荐指数
1
解决办法
82
查看次数

我应该如何让用户输入我想要的信息?

我刚开始学习计算机科学。

我正在通过在哈佛在线教授的 CS50 学习。好吧,我正在解决这个问题,我需要在命令行中从用户那里获取密钥,然后是明文,然后将该文本转换为 ASCII 中的密钥数量以生成密文。

这是我到目前为止所得到的。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
    }

    {
        string plaintext = get_string("plaintext:  ");

        int key = atoi(argv[1]);
        int n = strlen(plaintext);
        char chr[n];

        printf("ciphertext: ");
        for (int i = 0; i < n; i++)
        {
            chr[i] = plaintext[i];
            if (isalpha(chr[i]))
            {
                if (isupper(chr[i]))
                {
                    chr[i] = (chr[i] - 65 + key) % 26 + 65;
                    printf("%c",chr[i]);    
                } …
Run Code Online (Sandbox Code Playgroud)

c caesar-cipher cs50

1
推荐指数
1
解决办法
116
查看次数

这段代码有什么问题?我总是收到丢失终止 '"' 错误。我是 C 新手,希望使用此 ASCII 来提交 CS50x

我的程序根本不起作用。我收到这个错误。

population/ $ make test
test.c:6:9: error: missing terminating '"' character [-Werror,-Winvalid-pp-token]
printf(R"EOF(
        ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <cs50.h>

int main(void)
{
printf(R"EOF(
   _____       _               _ _   _           _   _              _____           _
  / ____|     | |             (_) | | |         | | | |            / ____|         (_)
 | (___  _   _| |__  _ __ ___  _| |_| |_ ___  __| | | |__  _   _  | |    _   _ …
Run Code Online (Sandbox Code Playgroud)

c string-literals rawstring cs50

1
推荐指数
1
解决办法
151
查看次数