C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-10-2009, 02:13 AM   #1
Registered User
 
Join Date: Oct 2009
Posts: 4
Unhappy need help using do/while loop

i have code here in C programming that will convert decimal to binary, octal and hexa..
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   Reply With Quote
Old 10-10-2009, 02:22 AM   #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();
Put that ch = getchar(), right after your scanf(). It will remove the '\n' char from the keyboard buffer (which you get when you hit enter).

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   Reply With Quote
Old 10-10-2009, 02:25 AM   #3
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
Code:
do
{
    showmenu();
    for( choice = getchoice(); !choiceinrange(); choice = getchoice() );
    handlechoice( choice );
}
while( choice != exitchoice );
There's the basic logic behind it.


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Reply

Tags
c programming, decimal to binary, hexadecimal, octal

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:34 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22