-3

Function of the code: The function of my code is pretty simple. It converts a phone number with numbers into a phone number.

#include <stdio.h>

main(){

  char num;

  printf("Please enter a phone number with letters to be converted into a phone number: \n");

  while ((num = getchar()) != '\n') {
    switch(num){
      case 'A': case 'B': case 'C': printf("2"); break;
      case 'D': case 'E': case 'F': printf("3"); break;
      case 'G': case 'H': case 'I': printf("4"); break;
      case 'J': case 'K': case 'L': printf("5"); break;
      case 'M': case 'N': case 'O': printf("6"); break;
      case 'P': case 'R': case 'S': printf("7"); break;
      case 'T': case 'U': case 'V': printf("8"); break;
      case 'W': case 'X': case 'Y': printf("9"); break;
      default: putchar(num);
    }

    return 0;

  }

}

The problem is that the getchar() is taking one value and changing that number/letter only when I want it to change the whole thing.

Things I have done: I found someone else code who did this already, copied it, changed it a bit (removed toupper function), stacked the cases side by side and it didn't work for some reason. Is it because of the placement of the switch statement? But I don't think it is because of that because it ends after printing one value.

What I want it to do:

Enter phone number: 1-800-COL-LECT
1-800-265-5328

What it does:

Enter phone number: 1-800-COL-LECT
1
Enter phone number: 5-469-COL-LECT
5
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Manny
  • 27
  • 8
  • 1
    `getchar` does not print anything. And it returns an `int` intentionally, not a `char`. – too honest for this site Mar 30 '17 at 19:54
  • 8
    `return 0;` What do you think that will do as the last line in the `while` loop? Use a debugger to step through your code line by line. That's the right way to debug such issues and will clearly show you the problem. – kaylum Mar 30 '17 at 19:54
  • So do you know any reason why this code isn't working? `getchar()` functions with a `char` type `var`. – Manny Mar 30 '17 at 19:56
  • 1
    `return 0; }` --> `} return 0;` or delete `return 0;` Also `main()` --> `int main(void)` – BLUEPIXY Mar 30 '17 at 19:56
  • 1
    You should filter out spaces, newlines and hyphens too. And deal with `'#'` and `'*'`. – Weather Vane Mar 30 '17 at 19:57
  • That is the stupidest thing I have ever done. Thanks @kaylum for finding that. Put that as your answer, so I could give it a check mark. – Manny Mar 30 '17 at 19:58
  • 1
    `getchar()` returns a result of type `int` so the caller can distinguish between `EOF` and any valid character value. You *can* assign the result to a `char`, but then you're discarding important information. And once you fix your code, it's likely to go into an infinite loop if it encounters end-of-file before reading a `'\n'` character. Also, `main()` should be `int main(void)`. – Keith Thompson Mar 30 '17 at 19:58
  • Isn't that clear that the real prolem was the `return 0;` in the `while` loop. – Roy Avidan Mar 30 '17 at 20:10
  • @רועיאבידן: That was the cause of the symptom the OP was asking about, but it's not the only problem in the code. – Keith Thompson Mar 30 '17 at 22:03

1 Answers1

0

The problem in the code is that return 0; is inside of your while loop. What you need to do is move the return 0; outside of the loop and it will fix it.

#include <stdio.h>

main(){

  char num;

  printf("Please enter a phone number with letters to be converted into a phone number: \n");

  while ((num = getchar()) != '\n') {
    switch(num){
      case 'A': case 'B': case 'C': printf("2"); break;
      case 'D': case 'E': case 'F': printf("3"); break;
      case 'G': case 'H': case 'I': printf("4"); break;
      case 'J': case 'K': case 'L': printf("5"); break;
      case 'M': case 'N': case 'O': printf("6"); break;
      case 'P': case 'R': case 'S': printf("7"); break;
      case 'T': case 'U': case 'V': printf("8"); break;
      case 'W': case 'X': case 'Y': printf("9"); break;
      default: putchar(num);
    }
  }
  return 0;
}

Originally founded by @kaylum

Manav Dubey
  • 780
  • 11
  • 26
  • 1
    This fixes the problem the OP asked about, but it leaves several other problems in place. See my first comment on the question. – Keith Thompson Mar 30 '17 at 22:06