#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
int i, phoneDigit;
char c;
printf("If you want to just alphabetic phone number press 1 unless press 2: ");
scanf("%d", &i);
if (i == 1)
{
printf("Enter alphabetic phone number : ");
c = getchar();
for (;;)
{
switch (c)
{
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 'Y': case 'X': printf("9"); break;
}
c = getchar();
}
}
else.
{
printf("Enter a phone number : ");
scanf("%d", &phoneDigit);
for (;;)
{
if (phoneDigit == 0)
{
c = getchar();
c = toupper(c);
i = c;
switch (i)
{
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 'Y': case 'X': printf("9"); break;
default: printf("0"); break;
}
scanf("%d", &phoneDigit);
}
else.
{
printf("%d", phoneDigit);
}
scanf("%d", &phoneDigit);
}
}
return 0;
}
getchar tek bir karakter okuyor
if (i == 1)
{
printf("Enter alphabetic phone number : ");
c= getchar();
for(;;)
{
switch (c)
{
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 'Y': case 'X': printf("9"); break;
}
c = getchar();
}
}
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
int i, phoneDigit;
char c;
printf("If you want to just alphabetic phone number press 1 unless press 2: ");
scanf("%d", &i);
if (i == 1)
{
printf("Enter alphabetic phone number : ");
c = getchar();
for (;;)
{
switch (c)
{
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 'Y': case 'X': printf("9"); break;
}
c = getchar();
}
}
else
{
printf("Enter a phone number : ");
scanf("%d", &phoneDigit);
for (;;)
{
if (phoneDigit == 0)
{
c = getchar();
if (c == '\n')
return;
c = toupper(c);
i = c;
switch (i)
{
case 65: case 66: case 67: printf("2"); break;
case 68: case 69: case 70: printf("3"); break;
case 71: case 72: case 73: printf("4"); break;
case 74: case 75: case 76: printf("5"); break;
case 77: case 78: case 79: printf("6"); break;
case 80: case 81: case 82: printf("7"); break;
case 83: case 84: case 85: printf("8"); break;
case 86: case 87: case 88: printf("9"); break;
default: printf("0"); break;
}
}
else
{
printf("%d", phoneDigit);
}
scanf("%d", &phoneDigit);
}
}
return 0;
}
Inputta 1 diye başlamışsın diye öyle anladım. Şu an bilgisayarımda değilim, geçince mesajı düzenlerim. Yardımcı olacağım.Kodu biraz değiştirdim bir sorun da karakteri int tipine çevirip switchte karakterlere karşılık gelen tam sayı değerini yazmamışım onu da değiştirdim. Dediğin gibi eğer new line karakter ise return yapmasını da yazdım ama tekrardan aynı sorunu aldım. Karakterleri tamsayıya çevirip yazdırmadı.
İşte yeni kod. Sorun bu arada else kısmında oluşuyor. İf kısmında bir sorun yaşamadım.C:#include <stdio.h> #include <stdlib.h> #include <ctype.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int i, phoneDigit; char c; printf("If you want to just alphabetic phone number press 1 unless press 2: "); scanf("%d", &i); if (i == 1) { printf("Enter alphabetic phone number : "); c = getchar(); for (;;) { switch (c) { 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 'Y': case 'X': printf("9"); break; } c = getchar(); } } else { printf("Enter a phone number : "); scanf("%d", &phoneDigit); for (;;) { if (phoneDigit == 0) { c = getchar(); if (c == '\n') return; c = toupper(c); i = c; switch (i) { case 65: case 66: case 67: printf("2"); break; case 68: case 69: case 70: printf("3"); break; case 71: case 72: case 73: printf("4"); break; case 74: case 75: case 76: printf("5"); break; case 77: case 78: case 79: printf("6"); break; case 80: case 81: case 82: printf("7"); break; case 83: case 84: case 85: printf("8"); break; case 86: case 87: case 88: printf("9"); break; default: printf("0"); break; } } else { printf("%d", phoneDigit); } scanf("%d", &phoneDigit); } } return 0; }
for (;;)
{
if (phoneDigit == 0)
{
c = getchar();
if (c == '\n')
return;
c = toupper(c);
i = c;
switch (i)
{
case 65: case 66: case 67: printf("2"); break;
case 68: case 69: case 70: printf("3"); break;
case 71: case 72: case 73: printf("4"); break;
case 74: case 75: case 76: printf("5"); break;
case 77: case 78: case 79: printf("6"); break;
case 80: case 81: case 82: printf("7"); break;
case 83: case 84: case 85: printf("8"); break;
case 86: case 87: case 88: printf("9"); break;
default: printf("0"); break;
}
}
else
{
printf("%d", phoneDigit);
}
scanf("%d", &phoneDigit);
}
if (phoneDigit == 0)
bu bloğa girdik. Kullanıcıya hiçbir şey söylemeden bir char bekliyoruz ondan. Hadi diyelim anladı ve 'A' girdi. i = c;
bu doğru bir kullanım mı? İstediğini yaptığından emin misin?Bu soruyu yapmak istiyorum. Aslında bunu 11 tane değişken tanımlayıp scanf("%d-%d%d%d-%c....") diye götürebilirim ama kolaya kaçmak istemiyorum(ikinci örnek için). Yazılan telefon numarasınındaki harf nerde olursa olsun onu tamsayıya çevirmesini ardından eğer harf varsa tamsayıya, tamsayı varsa tamsayı olarak devam etmesini istiyorum.
Maalesef henüz diziler kısmına gelmedim. Programlamanın daha çok başındayım.