Haven
Haven

Reputation: 1

Why is the output a question mark?

I just started to learn C++ on YouTube. Below is my code (which is exactly the instructor typed in his visual studio)

#include <stdio.h>

int main() {

    char alpha;

    scanf_s("%c", &alpha);

    char nextalpha = alpha + 1;
    
    printf("%c\n", nextalpha);
}

When I typed A, the output is ?.

I want my output to be B. (Because the number of B in the ASCII table is one more than that of A.)

my output in visual studio

Upvotes: 0

Views: 389

Answers (1)

chux
chux

Reputation: 154602

At least this problem:

scanf_s("%c", &alpha); has the wrong number of arguments. A size argument is missing

That argument is immediately followed in the argument list by the second argument, which has type rsize_t and gives the number of elements in the array pointed to by the first argument of the pair.

I'd expect

scanf_s("%c", &alpha, (rsize_t) 1);
// or 
scanf_s("%c", &alpha, sizeof alpha);

A good compiler with warnings well enabled will warn and save you time.


I just started to learn C++

Yet this code is C and tagged C. Consider C and C++ as different languages and focus your studies initially on one of them.

Upvotes: 1

Related Questions