0

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

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Haven
  • 1
  • 9
    Doesn't [`scanf_s`](https://en.cppreference.com/w/c/io/fscanf) take a size argument? – YiFei Dec 20 '20 at 04:12
  • 2
    Try using plain `scanf` –  Dec 20 '20 at 04:14
  • Take a look at this comment https://stackoverflow.com/a/24651312/5265174 or the whole post to understand the difference between `scanf` and `scanf_s`. – Riasat Ali Akash Dec 20 '20 at 04:17
  • Try this code:- #include int main() { char alpha; scanf("%c", &alpha); char nextalpha = alpha + 1; printf("%c\n", nextalpha); } scanf_s is Microsoft-specific. – sonulohani Dec 20 '20 at 04:18
  • I'm on Mac OSX so I changed to `scanf` but otherwise the same code and it works as intended. Are you sure you're running the right binary – sedavidw Dec 20 '20 at 04:18
  • 5
  • @justANewbie, why do you say that? This is all code that's standard to `c++`. If the user meant for it to be `c++`, then he's probably using a `c++` compiler, which means that it's better labelled as `c++`. – Elliott Dec 20 '20 at 04:24
  • Yeah, `scanf_s` does require the buffer size following each argument, so `scanf_s(“%c”, &alpha, 1);` probably works? See fourth paragraph down here: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l?view=msvc-160#remarks – i_am_jorf Dec 20 '20 at 04:27
  • 5
    Personally I'd avoid all the MS propriety stuff. It makes dubious claims about making it more secure, but really it just make the code non-portable (which, if we're honest, is probably their real goal). If you really need extra security, there are standard ways and libraries to help you with that in C/C++ land. – Elliott Dec 20 '20 at 04:31
  • 2
    It appears your `scanf_s("%c", &alpha);` fails to limit the number of bytes attempted to be placed in `alpha`. You must specify the buffer size. See [scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/scanf-s-scanf-s-l-wscanf-s-wscanf-s-l?view=msvc-160). It looks like what is happening is both `'A'` and `'\n'` are place in the buffer `alpha` and `alpha+1` isn't simply adding `1` to `'A'` resulting in a non-printable character that is then output as `?`. You likely have corrupted your stack so `nextalpha` is no longer valid. – David C. Rankin Dec 20 '20 at 04:51
  • Place `scanf_s("%c", &alpha,1);` – Sagun Devkota Dec 20 '20 at 05:19
  • @Elliott the standard input-output stream in C++ is ``````, and it use ```std::cin``` and ```std::cout``` instead of ```scanf```,```scanf_s``` and ```printf``` – justANewb stands with Ukraine Dec 20 '20 at 09:45
  • If the video tutorial has such serious flaws, drop it. Try some other sources to learn programming. – the busybee Dec 20 '20 at 13:04
  • @justANewbie, I'm afraid that's not right. `cin`/`cout` is often considered more "c++ style", but using functions from the `stdio.h` file is very much within the scope of `c++`. The `c++` standard deal with that library, and - more importantly - aren't necessarily identical to the standards for `c`, hence if you want to use `scanf` for `c++` then we should really specify `c++`. It may be pedantic, but remember that the questioner originally put `c++` correctly, then someone incorrectly changed it to `c`. – Elliott Dec 21 '20 at 00:06
  • Thanks very much for your replies. Your comments are really helpful. – Haven Dec 22 '20 at 04:49

1 Answers1

1

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.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • I'd just like to point out that the OP initially tagged it as `c++` - it was someone else who changed it to `c`. As it's valid `c++` code, and the OP is probably using a `c++` compiler which has it's own set of standards associated with the `stdio.h` file that isn't guaranteed to always be the same standards as that set in the `c` standards, then I'd argue that the tag was incorrectly changed to `c`, and that the questioner was right on this point. As you point out, they're different languages and should be treated as such. – Elliott Dec 21 '20 at 05:02
  • @Elliott Fair and good points. Yet if OP is starting "to learn C++", `cin` make more sense to first learn than `scanf_s(), if ever. – chux - Reinstate Monica Dec 21 '20 at 05:23