Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Need Help In C


  • Please log in to reply
1 reply to this topic

#1 vinch

vinch

  • Members
  • 19 posts
  • OFFLINE
  •  
  • Local time:06:38 AM

Posted 10 December 2006 - 07:20 AM

here's the code:

#include<stdio.h>
void asc(void), dsc(void);

void main()
{
char ch;
clrscr();
printf("Choose One:(1 or 2 only)\n");
printf("1: Ascending Order\t");
printf("2: Descending Order\n");
printf("\nEnter Choice:");scanf("%c",&ch);

switch(ch){
case '1':
printf("Ascending Option Selected\n\n");
asc();
break;
case '2':
printf("Descending Option Selected\n\n");
dsc();
break;
}
}

void asc(void)
{ float num[5],ctr1,ctr2,temp;
printf("Enter 5 numbers:\n");
for(ctr1=0;ctr1<=4;ctr1++)
{scanf("%f",&num[ctr1]);}
for(ctr1=0;ctr1<=3;ctr1++)
for(ctr2=ctr1+1;ctr2<=4;ctr2++)
if (num[ctr1]>num[ctr2])
{ temp=num[ctr1];
num[ctr1]=num[ctr2];
num[ctr2]=temp;
}
printf("The numbers in Ascending Order\n");
for(ctr1=0;ctr1<=4;ctr1++)
printf("%.2f ",num[ctr1]);
getch();
}

void dsc(void)
{ float num[5],ctr1,ctr2,temp;
printf("Enter 5 numbers:\n");
for(ctr1=0;ctr1<=4;ctr1++)
{scanf("%f",&num[ctr1]);}
for(ctr1=0;ctr1<=3;ctr1++)
for(ctr2=ctr1+1;ctr2<=4;ctr2++)
if (num[ctr1]<num[ctr2])
{ temp=num[ctr1];
num[ctr1]=num[ctr2];
num[ctr2]=temp;
}
printf("The numbers in Descending Order\n");
for(ctr1=0;ctr1<=4;ctr1++)
printf("%.2f ",num[ctr1]);
getch();
}

the problem here is when i input 12 in the Enter choice field, I would want to force the user to only input one character. how can I do that? plus can you teach me how to make the text blink.. any help is greatly appreciated!! thanx in advance!!

BC AdBot (Login to Remove)

 


#2 projectfocus

projectfocus

  • Members
  • 479 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Brighton
  • Local time:12:38 PM

Posted 11 December 2006 - 10:28 AM

instead of this

printf("\nEnter Choice:");scanf("%c",&ch);

try

ch = getchar();

That should set the char that is entered straight into the variable ch.
I would also think about adding a control comand in after that checks to see if it is of the value "1" or "2". If not to ask them to enter again.
FocusToonSigStreched.jpg




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users