I'm currently taking a class to learn 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. Not exactly sure why the new variable does not receive whats held by 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. 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.
In this code
long checksum(num)
{
long placeHolder1 = num;
}
placeHolder1
will only exist during the function's execution. Once execution leaves this function, placeHolder1 will go out of scope and effectively disappear.
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:
Number: 12
12
Also, there's no meaning of this: long checksum(num)
declaration. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.