简体   繁体   English

变量值未分配给 C 中的另一个变量

[英]Variable value not being assigned to another variable in C

I'm currently taking a class to learn C.我目前正在服用 class 来学习 C。 I have gotten my code to set the value input by the user to num but when I try to assign another variable(placeHolder1) the value of num and return that new variable, I am given a bunch of random numbers.我已经获得了将用户输入的值设置为 num 的代码,但是当我尝试为另一个变量(placeHolder1)分配 num 的值并返回该新变量时,我得到了一堆随机数。 Not exactly sure why the new variable does not receive whats held by num.不完全确定为什么新变量没有收到 num 持有的内容。 In my original post I left out a lot of the code because I didn't know if I was able to share it all.在我的原始帖子中,我遗漏了很多代码,因为我不知道我是否能够全部分享。 Yes this is a CS50 problem, no I am not asking for a solution to the problem set altogether just the specified issue with the variable.是的,这是一个 CS50 问题,不,我不是要求解决问题集,只是要求变量的指定问题。 A lot of it is commented out because I was trying to find out why I was getting a different value than expecting.其中很多都被注释掉了,因为我试图找出为什么我得到的价值与预期不同。

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

string cardType;
long get_card_num(string prompt);
long length;
long tempNumL;
long tempNumS;
int startNum;
long sum1;
long sum2;


int main(void)
{
        long n = get_card_num("Number: ");      //prompts user for input
        printf("%li\n",n);
}

long checksum(num)
{
    long placeHolderA=num;
    long placeHolderB=num/10;

    /*do
    {
        sum1+=(placeHolder1%10);
        placeHolder1=placeHolder1/100;
    }
    while(placeHolder1/100>1);

    do
    {
        sum2+=(placeHolder2%10);
        placeHolder2= placeHolder2/100;
    }
    while(placeHolder1/100>0);
    */
    return num;
}

long get_card_num(string prompt)
{
    long num;

            num = get_long("%s", prompt);           //assigns num to the value of what user input

        tempNumL=num; // used to get length
        tempNumS=num; // used to get starting num

    do
    {
        tempNumL = tempNumL/10;                 //gets length
        length++;
    }
    while(tempNumL>0);

    if(length<13)
    {
        get_card_num(prompt);
    }


    checksum(num);




    /*
    if((length==13 || length==16) && startNum==4)
    {
    }

    if(length==15)
    {
    }

    if(length==16 && startNum!=4)
    {
    }
    else
    {
        printf("INVALID");
    }
*/
return num;
}

It appears that you're having difficulty with a concept called variable scope.看来您在使用变量 scope 的概念时遇到了困难。

In this code在这段代码中

long checksum(num)
{
    long placeHolder1 = num;
}

placeHolder1 will only exist during the function's execution. placeHolder1只会在函数执行期间存在。 Once execution leaves this function, placeHolder1 will go out of scope and effectively disappear.一旦执行离开此 function,placeHolder1 将 go 超出 scope 并有效地消失。

You may need to do something like:您可能需要执行以下操作:

#include <stdio.h>

long checksum(long num) // it's in top because it's used after its declaration
{
    return num;
}

long get_num(char prompt[]) // 'string' is a concept of C++
{
    long num;

    printf("%s", prompt);
    scanf("%ld", &num); // assigns num to the value of what user input
                        // use %ld for 'long int'

    long placeHolder1 = checksum(num); // placeHolder1 wasn't declared here

    return placeHolder1;
}

int main(void)
{
    long n = get_num("Number: "); // prompts user for input
    printf("%ld\n", n);

    return 0;
}

Note: The mistakes are commented just after the working code.注意:错误在工作代码之后被注释。

Read about Variable Scopes in brief to know when and how should we declare the functions and variables when needed. 简要阅读变量作用域,了解何时以及如何在需要时声明函数和变量。

After correcting the silly errors, you'll get rid of incorrect output:纠正愚蠢的错误后,您将摆脱不正确的 output:

Number: 12
12

Also, there's no meaning of this: long checksum(num) declaration.此外,这没有任何意义: long checksum(num)声明。 You're taking the num which is unknown to the function as per of the rule of variable scope + if it's a struct or a data type, it must have a variable name.根据变量 scope 的规则,您正在使用 function 未知的num + 如果它是struct或数据类型,则它必须具有变量名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM