-4

The following code lets the user write a number in a string so it will be converted to an integer automatically. However, if he writes something else than a number there should be an error. So far so good it worked.

The problem is now: I need to point an arrow to the letter which is not allowed by the user. Example: He writes 45. This is right. He writes 423a4. This is wrong. Then there should be an arrow point on the letter a.

But I don't know how and where I should put the arrow.

NOTE: NO LIBRARY FUNCTIONS ALLOWED except stdio.h

Here is the code:

#include <stdio.h>

void main()
{
    char input[100];
    printf("Type a String which will be converted to an Integer: ");
    scanf("%s", input);

    int number = 0;
    int i = 0;
    int x = 0;
    bool success = true;

    while (input[i] != '\0')
    {
        if (input[i] >= '0' && input[i] <= '9')
        {
            number = number * 10 + input[i] - '0';
            i++;
        }
        else
        {
            success = false;
            printf("\n");
            printf("Character %c is not a number! \n", input[i]);
            break;
        }
    }
    if (success)
    {
        printf("string %s -> number %d \n", input, number);
    }
}
DeathToxic
  • 157
  • 1
  • 7
  • 5
    "NO LIBRARY FUNCTIONS ALLOWED" - So remove `stdio` and all it provides, e.g. `printf`. Looks like you have to revert to Assembler. – too honest for this site Feb 09 '16 at 15:03
  • Print the original string and then `^` on the next line after `i-1` spaces. – Eugene Sh. Feb 09 '16 at 15:04
  • 1
    Is using `scanf()`, `fgets()`, `printf()` allowed even though "NO LIBRARY FUNCTIONS ALLOWED"? – chux - Reinstate Monica Feb 09 '16 at 15:07
  • The aim of your homework is to write a number parser. So forget scanf and write it on you own. Then you know the position where it fails. – ceving Feb 09 '16 at 15:09
  • 1
    Please clarify your question. What exactly don't you understand? And how come you're using `printf()` if no library calls are allowed? As it stands, your question is liable to be closed as off-topic. See http://stackoverflow.com/help/how-to-ask for guidelines on how to ask a good question. – r3mainer Feb 09 '16 at 15:09
  • There's _a lot_ of questions available on SO regarding how to do string to integer conversions. I [posted one](http://stackoverflow.com/questions/35219839/kr-atoi-general-memory-leak/35221005#35221005) as late as last week, as another of my futile attempts to teach Kernighan & Ritchie modern C programming. – Lundin Feb 09 '16 at 15:13
  • @DeathToxic: This is the, what, fourth, fifth time you are posting a question from that C++ tutor of yours to SO to have it answered. [My comment](http://stackoverflow.com/questions/34858150/convert-string-user-input-to-a-double#comment57524906_34858150) from your last question still holds true. You are not doing anyone any favors by having the SO community do your homework. **The solution is right there.** It's biting you in the nose, really. Look at `i`. **Use** `i`. – DevSolar Feb 09 '16 at 15:22
  • I edited it. No functions except the stdio.h This is the third question I ever asked here. And I ask if I have tried anything else. This one is always my last option. If you have a better idea how I could choose another way to get ideas I would be glad to hear them. Thanks. – DeathToxic Feb 09 '16 at 15:42
  • @DeathToxic: The idea is to train yourself in taking big (abstract) problems apart until you're looking at small (concrete) problems, then solve them piecemeal. In this case, "print an arrow that points at the first illegal character". What does it take? Picture the desired output in your mind. The input in one line, and in the next a number of spaces, and the arrow. How many spaces? Equal to the index of the illegal character (`i`). The rest is *easy*! Every time you go to SO for an *idea*, you take the easy way out, robbing yourself of a valuable learning experience. No offense intended! – DevSolar Feb 10 '16 at 09:17

1 Answers1

0

If you want to point the first not allowed char.

int main()
{
    char input[100];
    char arrow[100] = {0};
    printf("Type a String which will be converted to an Integer: ");
    scanf("%s", input);

    int number = 0;
    int i = 0;
//    int x = 0;
    bool success = true;

    while (input[i] != '\0')
    {
        if (input[i] >= '0' && input[i] <= '9')
        {
            number = number * 10 + input[i] - '0';
            arrow[i] = ' ';
            i++;
        }
        else
        {
            success = false;
            arrow[i]='^';
            printf("\nInput number is wrong!\n%s\n%s\n", input, arrow);
            break;
        }
    }
    if (success)
    {
        printf("string %s -> number %d \n", input, number);
    }

    return 0;
}

output is

Input number is wrong!
423432rr55
      ^
LPs
  • 16,045
  • 8
  • 30
  • 61
  • @DeathToxic The arrow is under the first wrong char. – LPs Feb 09 '16 at 15:39
  • thank you very much! it worked, but could you explain shortly for what the {0} in char arrow[100] stands for? would be very helpful. – DeathToxic Feb 09 '16 at 16:04
  • @DeathToxic It init all array/string to 0. Like `memset(arrow, 0x00, sizeof(arrow));` Without that instruction will be [UB](https://en.wikipedia.org/wiki/Undefined_behavior) because of you cannot be sure that the string is `NULL` terminated, in this code. [THIS](http://stackoverflow.com/questions/629017/how-does-array100-0-set-the-entire-array-to-0) explain it better. – LPs Feb 09 '16 at 16:05