0

Hi I am very new to the language 'C' I am struggling to print my returned value from the function get_type_of_card() I would like to print it in main on the same line. If I entered in the string "VISA" then the output = VISA.

As of right now the output is just 'V' out of "VISA".

I have one warning I was wondering how to get rid of it.

WARNING: assignment makes pointer from integer without a cast [-Wint conversion] card[i] = c;

I have tried a for loop with putchar().

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

//Function prototypes
const char* get_type_of_card();

int main(void)
{   
    const char* type_of_crd = get_type_of_card();
    printf("\nYou entered: %c", type_of_crd);


    return 0;
}

const char* get_type_of_card()
{   
    const char* card[50];
    char c;
    int i;

    do
    {
        printf("\nPlease select a credit card: ");
        for (i = 0; (c = getchar()) != '\n'; ++i)
        {
            card[i] = c;
            return card[i]; // Holds value of "VISA"
        }
        card[i] = '\0';
    } while ((c != '\n') && (c != EOF));


}
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
  • 2
    `%c` prints a single char. `%s` prints a string. – 001 Aug 22 '19 at 18:13
  • In your printf, you have used %c which prints one character instead use %s to print the complete string. – Ashish Kumar Aug 22 '19 at 18:13
  • 2
    Also, `return card[i]` in the loop returns after one char is entered.... [The Definitive C Book Guide and List](//stackoverflow.com/q/562303) – 001 Aug 22 '19 at 18:13
  • Ashish Kumar: I have tried "%s" and it wont print anything to the console –  Aug 22 '19 at 18:14
  • Johnny Mopp: If I return `card[i]` at the end of the do while like a regular do while loop it just prints (null). –  Aug 22 '19 at 18:21
  • 2
    @BraidenGole There are other problems as well. `const char* card[50];` is an array of `char` pointers. To store a string, you want `char card[50]`. Then at the end of the function, you would `return card;`. However, doing that returns a pointer to a local variable, which you shouldn't do. – 001 Aug 22 '19 at 18:25
  • 1
    @BraidenGole Do not make sweeping changes to your code once others have already answered. – S.S. Anne Aug 22 '19 at 19:31
  • It is now not clear how this code differs from the answer - you the code in the question no longer exhibits the problem you are asking about, you have invalidated the question. You should role it back or delete it as no longer useful. – Clifford Aug 22 '19 at 19:37
  • @JL2210 I just realized that actually the post is still the original post all I did was edit it to add a comment which was ("Holds the value of visa"). –  Aug 22 '19 at 19:41
  • [`int c;`](https://stackoverflow.com/q/35356322/918959) – Antti Haapala -- Слава Україні Aug 22 '19 at 21:31
  • You have two consecutive lines: `//Function prototypes` and `const char* get_type_of_card();`. However, in C (but not C++), there is no prototype for the function `get_type_of_card()`; there is a declaration, but the parameter list is unspecified — the only reasonably certain conclusion is that it is not a variadic function like `printf()` because it is undefined behaviour to call such a function without a full prototype in scope. Use `const char *get_type_of_card(void);` if the function takes no arguments — in both the declaration and the definition of the function. – Jonathan Leffler Aug 22 '19 at 21:39

1 Answers1

0

I have achieved my result not sure if it is programmatically correct.

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

//Function prototypes
const char* get_type_of_card();

int main(void)
{   
    const char* type_of_crd = get_type_of_card();
    printf("\nYou entered: %s", type_of_crd);


    return 0;
}

const char* get_type_of_card()
{   
    static char card[50];
    char c;
    int i;

    do
    {
        printf("\nPlease select a credit card: ");
        for (i = 0; (c = getchar()) != '\n'; ++i)
        {
            card[i] = c;
        }
        card[i] = '\0';
    } while ((c != '\n') && (c != EOF));

    return card;
}
  • 4
    This isn't your problem yet, but keep in mind that static variables will get overwritten the next time you call the function. So if you did, `const char *a = get_type_of_card(); const char *b = get_type_of_card(); printf("%s\n", a);`, it would print the thing you entered the *second* time, because both a and b are pointing at the same memory, and it got overwritten. It's not wrong, but it's something to be aware of and watch out for. – Ray Aug 22 '19 at 19:26
  • 2
    More importantly, look at what happens if someone enters a line with more than 49 characters. It'll keep writing off the end of the array, and clobber whatever variables happen to live in the adjacent memory. (Likely victims include the other local variables, the function parameters, and the place where the program keeps track of where it should go after the function returns.) This is called "buffer overflow" and is a big source of both crashes and security issues. Look at the `fgets` function for a way to read lines into fixed-size buffers safely. – Ray Aug 22 '19 at 19:28
  • Yep, this is good. Perhaps use `return strdup(card)`, however. – S.S. Anne Aug 22 '19 at 19:30
  • 2
    Beware buffer overflow — someone might type more than 50 character before hitting return. Your inner loop (`for (i = 0; (c = getchar()) != '\n'; i++)`) also doesn't handle EOF but should. The code will go into an infinite tail spin until it crashes if the user indicates EOF. Also, `getchar()` returns an `int`; you should use `int c;` and not `char c;` — again, funny things can happen if you don't get this detail right. – Jonathan Leffler Aug 22 '19 at 21:44
  • @JonathanLeffler Thanks for writing in I appreciate it Ill be sure to make the necessary changes to my source code, this is great advice :) –  Aug 26 '19 at 02:13