![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 4
| i already know how to use do/while loop but i'm difficulty on using the looping in my code that i made. for example [1] Decimal to Binary [2] Decimal to Octal [3] Decimal to Hexadecimal [4] Exit Enter your choice: 1 Enter decimal number: 2009 Binary is: bla bla bla Do you want to continue?(Y/N): Y when i press Y i want that it will go back to first thing that you will choose again what conversion you want...can someone help me with my prob? here is the code: Code: #include <stdio.h>
void main()
{
void welcome();
void code();
int d;
int i=0,n,j,b[100];
printf("\t\t****************************************");
printf("\n\t\t*\t\t\t\t *");
printf("\n\t\t*\t\t\t\t *");
printf("\n\t\t* DECIMAL CONVERSION *");
printf("\n\t\t*\t\t\t\t *");
printf("\n\t\t*\t\t\t\t *");
printf("\n\t\t****************************************");
code();
scanf("%d", &d);
switch(d)
{
case 1:
printf("\n\t\tEnter decimal number: ");
scanf("%d", &n);
while (n>0)
{
b[i]=n%2;
n=n/2;
i++;
}
printf("\n\t\tBinary is: ");
j=i-1;
for (i=j;j>=0;j--)
{
printf("%d", b[j]);
}
break;
case 2:
printf("\n\t\tEnter decimal number: ");
scanf("%d", &n);
while (n>0)
{
b[i]=n%8;
n=n/8;
i++;
}
printf("\n\t\tOctal is:");
j=i-1;
for (i=j;j>=0;j--)
{
printf("%d", b[j]);
}
break;
case 3:
printf("\n\t\tEnter decimal number: ");
scanf("%d", &n);
while (n>0)
{
b[i]=n%16;
n=n/16;
i++;
}
printf("\n\t\tHexadecimal is:");
j=i-1;
for (i=j;j>=0;j--)
{
printf("%d", b[j]);
if(b[j]<10)
{
printf("%d", b[j]);
}
else
{
switch(b[j])
{
case 10:
printf("A");
break;
case 11:
printf("B");
break;
case 12:
printf("C");
break;
case 13:
printf("D");
break;
case 14:
printf("E");
break;
case 15:
printf("F");
break;
}
}
}
}
}
void code ()
{
printf("\n\n\t\tChoose:\n");
printf("\n\n\t\t[1] Decimal to Binary\n");
printf("\t\t[2] Decimal to Octal\n");
printf("\t\t[3] Decimal to Hexadecimal\n");
printf("\t\t[4] Exit\n");
printf("\n\n\t\tEnter your choice: ");
}
Last edited by juncas17; 10-10-2009 at 02:29 AM. Reason: fedit some text |
| juncas17 is offline | |
| | #2 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| Sight unseen, the mystic forces tell me, you need to pull the newline char off the keyboard buffer with something like this: Code: char ch; ch = getchar(); Numbers and strings are not affected this way, but char's with scanf(), always have this problem. If you still have problems, check that you have used two equal signs in your comparison with 'y' or 'Y', and not just one (which is an assignment). Code: do {
//your other code in here
}while (ch == 'Y' || ch == 'y');
Last edited by Adak; 10-10-2009 at 02:27 AM. |
| Adak is offline | |
| | #3 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Code: do
{
showmenu();
for( choice = getchoice(); !choiceinrange(); choice = getchoice() );
handlechoice( choice );
}
while( choice != exitchoice );
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
![]() |
| Tags |
| c programming, decimal to binary, hexadecimal, octal |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| funny-looking while loop | Aisthesis | C++ Programming | 3 | 08-30-2009 11:54 PM |
| My loop within loop won't work | Ayreon | C Programming | 3 | 03-18-2009 10:44 AM |
| Personal Program that is making me go wtf? | Submeg | C Programming | 20 | 06-27-2006 12:13 AM |
| A somewhat bizzare problem!!! - WHILE LOOP | bobthebullet990 | C Programming | 3 | 03-31-2006 07:19 AM |
| when a while loop will stop ? | blue_gene | C Programming | 13 | 04-20-2004 03:45 PM |