-1

I wrote this code to see how many digits are in a number. I'm assuming that the user uses a number with no more than four digits. For some reason, the code is skipping the if statement. I've checked other questions related to this, but I don't have the same errors. Can you help me? Here is the code that I wrote:

#include "stdafx.h"

int main()
{
    char d1;
    char d2;
    char d3;
    char d4;
    printf("Please enter a number with four or less digits:");
    scanf("%c%c%c%c", &d1, &d2, &d3, &d4);
    printf("%c%c%c%c\n", d1, d2, d3, d4);
    int d1v2 = d1;
    int d2v2 = d2;
    int d3v2 = d3;
    int d4v2 = d4;
    printf("%c%c%c%c\n", d1v2, d2v2, d3v2, d4v2);
    int num = (d1v2 * 1000) + (d2v2 * 100) + (d3v2 * 10) + d4v2;
    printf("%d", num);
    int numv2 = num;

    if (numv2 < 10) {
        printf("Your number has 1 digit\n");
    }
    else if (numv2 >= 10 && numv2 < 100) {
        printf("Your number has 2 digits\n");
    }
    else if (numv2 >= 100 && numv2 < 1000) {
        printf("Your number has 3 digits\n");
    }
    else if (numv2 >= 1000 && numv2 < 10000) {
        printf("Your number has 4 digits\n");
    }
} 
  • 5
    What does your debugger show you? – Richard Critten Apr 12 '17 at 18:10
  • 2
    Print out `numv2` and make sure it is what you expect. – NathanOliver Apr 12 '17 at 18:11
  • 1
    Why so many numbers in variable names? – Evan Carslake Apr 12 '17 at 18:13
  • 1
    This looks more like C than C++. We'd use iostreams and string in C++, unless we were purposely coding C for some performance reason in a particular spot. You'd probably have a much easier time with your logic using iostreams and string. – Christopher Pisz Apr 12 '17 at 18:19
  • 1
    You are already printing the value with `printf("%d", num);` what is it printing??? – AndyG Apr 12 '17 at 18:24
  • @EvanCarslake this is a counting program, is it not? The way I see it, OP has ensured they have 4 digits come hell or high water: `d1`, `d2`, `d3`, and `d4`. Some of them may not ave been provided by the user, though. Come to think of it, the return value of `scanf` will at least tell them how many characters were input. `isdigit` would handle the rest. – user4581301 Apr 12 '17 at 18:56
  • [Convert a character digit to the corresponding integer in C](http://stackoverflow.com/q/628761/669576) – 001 Apr 12 '17 at 19:21
  • Prefer to use `fscanf` rather than `scanf`. Also, check the return value to see how many digits were input. – Thomas Matthews Apr 12 '17 at 19:31
  • You could simplify your program by using `std::string` and `std::getline`. Use `std::string::length` as a first check on the number of digits. Then use `isdigit` on each slot on the string to verify that the slot contains a digit. – Thomas Matthews Apr 12 '17 at 19:33
  • You can simplify your program by reusing variables. For example, you could use `num` instead of creating `numv2`. You don't need to duplicate variables for each new purpose. Reduce, reuse, recycle. – Thomas Matthews Apr 12 '17 at 19:36

1 Answers1

1

Im not sure if you are aware what is happening in this fragment of code:

int d1v2 = d1;
int d2v2 = d2;
int d3v2 = d3;
int d4v2 = d4;
printf("%c%c%c%c\n", d1v2, d2v2, d3v2, d4v2);
int num = (d1v2 * 1000) + (d2v2 * 100) + (d3v2 * 10) + d4v2;
printf("%d", num);
int numv2 = num;

d1, d2, d3 and d4 are char's and when you cast them to int's what you get is ASCII value of given character -> wiki. To make this work you can try for example this:

int d1v2 = d1 - '0';

Note that there are many other ways to get specific number value. Then in your printf change displayed type from char (%c) to int (%i). At this point your conversion from string to int should work, but with one little problem.

You should think a bit more what is happening when somebody enters for example 3 numbers only. Are you aware what is stored in d4 variable after scanf? But this my friend i prefer to leave you as next challange :-)

  • 1
    `int d1v2 = d1 - "0";` should use single quotes. – Steve Apr 12 '17 at 19:57
  • @Steve you're right, sorry for that! – Krzysiek Przekwas Apr 12 '17 at 20:03
  • Thank you for helping me out. I have one more question. Would d4's value be 0? – user7428275 Apr 13 '17 at 16:14
  • Unfortunately, it's not that easy. When you are entering 3 digit number and then hit enter to invoke scanf, what you get is d1,d2 and d3 as numbers but d4 = '\n' - what as you know form using this in printf is representation of newline. This may be quite easy to overcome but lets look what will happen if you enter 2 digit number. d1 and d2 are numbers and d3 will be newline sign, but what about d4? Actually scanf won't have enough characters and will wait until you provide them (and hit enter). At this point you should realise that it's not best way to count digits. Maybe scanf whole int? :-) – Krzysiek Przekwas Apr 14 '17 at 16:57
  • Thank you, that fixed everything! – user7428275 Apr 17 '17 at 14:12