admin管理员组文章数量:1405392
Scenario
I have an odd bug where when I add a random variable to the code it changes the output. I figured out what was causing the issue, however I am not entirely sure why it is causing the issue.
The code is intended to validate credit card numbers using the Luhns Algorithm. It is part of the edX CS50 course, however I decided to try it without their custom-built libraries in an attempt to learn as much as possible.
The issue I am encountering is, if I run the code below as-is, then the answer comes out wrong (i.e., a '9' appended to the integer output). Now if I uncomment either int random_integer = 0;
or char random_character = 'M';
, then the output works entirely as anticipated.
- Wrong output (leaving those random variables commented):
209
- Correct output (uncommenting one (or both) of those random variables):
20
Oddly enough, I noticed that if I change char sum_to_str[3];
to char sum_to_str[10];
, the problem goes away entirely.
Please let me know your thoughts as I am newer to C and am interested in understanding the nuances.
Code
#include <stdio.h> // For Standard Input/Output
#include <stdlib.h> // For the atoi() function
#include <string.h> // For the strchr() and strlen() functions
// This value is the length plus one
#define MAX_INPUT_LENGTH 255
int main(void) {
char user_input[MAX_INPUT_LENGTH];
char *p;
// Output CLI instructions
printf("Welcome to the Credit Card Validator!!\n");
printf("INSTRUCTIONS: At the prompt, please provide a CC number.\n");
printf("NOTES ON LENGTH: Visa -> 13 || 16, AmEx -> 15 and MC -> 16\n");
// Algorithm
char example_card_num[] = "4003600000000014";
// Check the length
int card_num_length = strlen(example_card_num);
int skip_flag = 0;
int sum_of_values = 0;
char value_at_index;
char str_of_evens[20];
for (int i = card_num_length - 1; i >= 0; i--) {
char sum_to_str[3];
switch (skip_flag) {
case 0:
// Add 'odd' values together
value_at_index = example_card_num[i];
sum_of_values = sum_of_values + atoi(&value_at_index);
// Toggle flag
skip_flag = 1;
break;
case 1:
// Add 'even' values together (with multiplier)
value_at_index = example_card_num[i];
// 1. Convert each str to int
// 2. Multiply by two
// 3. Convert back to str in new variable
sprintf(sum_to_str, "%d", (atoi(&value_at_index) *2));
// Concatenate each substring to a new string
strcat(str_of_evens, sum_to_str);
// Toggle flag
skip_flag = 0;
break;
}
}
// int random_integer = 0;
// char random_character = 'M';
char value_at_index_two;
for (int i = 0; i < strlen(str_of_evens); i++) {
value_at_index_two = str_of_evens[i];
sum_of_values = sum_of_values + atoi(&value_at_index_two);
}
printf("~~~~~~~~~~~\n");
printf("Sum of Values 01: %d\n", sum_of_values);
// Terminate the program
return 0;
}
I tried the following:
- Commenting/uncommenting the
int random_integer = 0;
then recompiling - Adding the
char random_character = 'M';
then toggling it as a comment and recompiling - Modified the
char sum_to_str[3];
tochar sum_to_str[3];
I also tried to change the input (i.e., example_card_num[]
) slightly to determine if there was something throwing an arbitrary "9" or if the appended number changed, which it did.
Scenario
I have an odd bug where when I add a random variable to the code it changes the output. I figured out what was causing the issue, however I am not entirely sure why it is causing the issue.
The code is intended to validate credit card numbers using the Luhns Algorithm. It is part of the edX CS50 course, however I decided to try it without their custom-built libraries in an attempt to learn as much as possible.
The issue I am encountering is, if I run the code below as-is, then the answer comes out wrong (i.e., a '9' appended to the integer output). Now if I uncomment either int random_integer = 0;
or char random_character = 'M';
, then the output works entirely as anticipated.
- Wrong output (leaving those random variables commented):
209
- Correct output (uncommenting one (or both) of those random variables):
20
Oddly enough, I noticed that if I change char sum_to_str[3];
to char sum_to_str[10];
, the problem goes away entirely.
Please let me know your thoughts as I am newer to C and am interested in understanding the nuances.
Code
#include <stdio.h> // For Standard Input/Output
#include <stdlib.h> // For the atoi() function
#include <string.h> // For the strchr() and strlen() functions
// This value is the length plus one
#define MAX_INPUT_LENGTH 255
int main(void) {
char user_input[MAX_INPUT_LENGTH];
char *p;
// Output CLI instructions
printf("Welcome to the Credit Card Validator!!\n");
printf("INSTRUCTIONS: At the prompt, please provide a CC number.\n");
printf("NOTES ON LENGTH: Visa -> 13 || 16, AmEx -> 15 and MC -> 16\n");
// Algorithm
char example_card_num[] = "4003600000000014";
// Check the length
int card_num_length = strlen(example_card_num);
int skip_flag = 0;
int sum_of_values = 0;
char value_at_index;
char str_of_evens[20];
for (int i = card_num_length - 1; i >= 0; i--) {
char sum_to_str[3];
switch (skip_flag) {
case 0:
// Add 'odd' values together
value_at_index = example_card_num[i];
sum_of_values = sum_of_values + atoi(&value_at_index);
// Toggle flag
skip_flag = 1;
break;
case 1:
// Add 'even' values together (with multiplier)
value_at_index = example_card_num[i];
// 1. Convert each str to int
// 2. Multiply by two
// 3. Convert back to str in new variable
sprintf(sum_to_str, "%d", (atoi(&value_at_index) *2));
// Concatenate each substring to a new string
strcat(str_of_evens, sum_to_str);
// Toggle flag
skip_flag = 0;
break;
}
}
// int random_integer = 0;
// char random_character = 'M';
char value_at_index_two;
for (int i = 0; i < strlen(str_of_evens); i++) {
value_at_index_two = str_of_evens[i];
sum_of_values = sum_of_values + atoi(&value_at_index_two);
}
printf("~~~~~~~~~~~\n");
printf("Sum of Values 01: %d\n", sum_of_values);
// Terminate the program
return 0;
}
I tried the following:
- Commenting/uncommenting the
int random_integer = 0;
then recompiling - Adding the
char random_character = 'M';
then toggling it as a comment and recompiling - Modified the
char sum_to_str[3];
tochar sum_to_str[3];
I also tried to change the input (i.e., example_card_num[]
) slightly to determine if there was something throwing an arbitrary "9" or if the appended number changed, which it did.
1 Answer
Reset to default 4You should not use atoi on a non string array char variable because there is no guarantee of it to be null terminated, so atoi would yield unpredictable results.
本文标签: cDeclaringdefining an unused variable changes the output from an unrelated variableStack Overflow
版权声明:本文标题:c - Declaringdefining an unused variable changes the output from an unrelated variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744888914a2630654.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
atoi
takes a pointer to a string as input, not a pointer tochar
. – ad absurdum Commented Mar 8 at 18:42char
s denoting digits happens to be in sequence, so'0' + 5
gets you a'5'
. And, for the reverse,'5' - '0'
gives you the value5
. Now use that onvalue_at_index
. – BoP Commented Mar 8 at 18:57