4 Replies - 2403 Views - Last Post: 12 April 2011 - 12:42 PM Rate Topic: -----

#1 Jujumagic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 11

improved menu system using do while loop to display input key

Posted 12 April 2011 - 03:47 AM

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>

void main(void)
{   
	char title[80];
	int i=0;
	int k=0;
	int len=0;
	int numSpacesLeft=0;
	int numSpacesRight=0;
	puts("\n\t\t\tCalculator Menu\n");
	puts("\n\t\t1)\tPSI to BAR\n");
	puts("\n\t\t2)\tBAR to PSI\n");
	puts("\n\t\t3)\tTime from Distance and Speed\n");
	puts("\n\t\t4)\tPower from Volt and Amps\n");
	puts("\n\t\t5)\t555 Timer\n");
	puts("\n\t\t6)\tExit the package\n");

	printf("\nSelect an option from the above menu:\n");
	scanf("%d",&k);


	switch(k)
	{
	case 1: puts("PSI to BAR\n"); k=1;break;
	case 2: puts("BAR to PSI\n"); k=2;break;
	case 3: puts("Time from Distance and Speed\n"); k=3; break;
	case 4: puts("Power from Volt and Amps\n"); k=4; break;
	case 5: puts("555 Timer\n"); k=5; break;
	case 6: puts("Exit the package\n"); k=6; break;
	default: puts("Error please select the correct key\n"); break;
	}
	gets(title);
	len=strlen(title);
	numSpacesLeft=(80-strlen(title))/2;
	numSpacesRight=80-numSpacesLeft;
	for(i=0;i<numSpacesLeft;i++)
	{
		printf(" ");
	}
	printf("*");
	for(i=0;i<len;i++)
	{
		printf("*");
	}
	printf("*");
	for(i=0;i<numSpacesRight;i++)
	{
		printf(" ");
	}
	printf("\n");
	for(i=0;i<numSpacesLeft;i++)
	{
		printf(" ");
	}
	printf("*");
	printf("%s",title);
	printf("*");
	for(i=0;i<numSpacesRight;i++)
	{
		printf(" ");
	}
	printf("\n");
	for(i=0;i<numSpacesLeft;i++)
	{
		printf(" ");
	}
	printf("*");
	for(i=0;i<len;i++)
	{
		printf("*");
	}
	printf("*");
	for(i=0;i<numSpacesRight;i++);
	{
		printf(" ");
	}
do{
	printf("\n\n\\n\nPress any key to continue");
	getch();
}
while(k>6);
getch();
}


Using a do while loop for the program to operate continuously until the exit option is selected. As each option is selected a new screen should be displayed showing the title of the option selected and output on the screen in a box positioned at the canter of the screen in asterisks.When a key is pressed the original menu should be displayed again ready for the user's next selection......... HOW do I get it to accept a string instead of a decimal interger.

Is This A Good Question/Topic? 0
  • +

Replies To: improved menu system using do while loop to display input key

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: improved menu system using do while loop to display input key

Posted 12 April 2011 - 04:37 AM

There are two ways to navigate through menus using string options:

1. You could keep your current switch-case structure. But instead of accepting int value, accept string. Then change that string value to an int value using a predefined index.
Like :
ABC - 0
BCD - 1 and so on.

This way you can retain your current system of switch case.

2. You could accept string choices, and use if-else structure to compare the choice and perform the corresponding action.

Like:
string choice;
scanf("%s",&choice);

if(strcmp(choice,"ABC"){
 // do somehting
}
else{
 // do something
}

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: improved menu system using do while loop to display input key

Posted 12 April 2011 - 05:48 AM

Look at this real careful, does it seem at all redundant to you?
switch(k)
{
case 1: puts("PSI to BAR\n"); k=1;break;


Because, you know, k is already that value?

Use some functions, they're your friends. Don't use gets, main is always "int main". Call an exorcist for the conio.h.

With functions, you could do something like:
int showMenu() {
	int k;
	
	puts("\n\t\t\tCalculator Menu\n");
	puts("\n\t\t1)\tPSI to BAR\n");
	puts("\n\t\t2)\tBAR to PSI\n");
	puts("\n\t\t3)\tTime from Distance and Speed\n");
	puts("\n\t\t4)\tPower from Volt and Amps\n");
	puts("\n\t\t5)\t555 Timer\n");
	puts("\n\t\t6)\tExit the package\n");

	printf("\nSelect an option from the above menu:\n");
	scanf("%d",&k);
	return k;
}

void option1() { /* your code here */ }
void option2() { /* your code here */ }
void option3() { /* your code here */ }
void option4() { /* your code here */ }
void option5() { /* your code here */ }

int main() {
	int exit;
	do {
		exit = 0;
		switch(showMenu()) {
			case 1: option1(); break;
			case 2: option2(); break;
			case 3: option3(); break;
			case 4: option4(); break;
			case 5: option5(); break;
			case 6: exit = 1; break;
			default: puts("Error please select the correct key\n");
		}
	} while(!exit);
	
	return 0;
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 Jujumagic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-April 11

Re: improved menu system using do while loop to display input key

Posted 12 April 2011 - 11:53 AM

View Postbaavgai, on 12 April 2011 - 05:48 AM, said:

Look at this real careful, does it seem at all redundant to you?
switch(k)
{
case 1: puts("PSI to BAR\n"); k=1;break;


Because, you know, k is already that value?

Use some functions, they're your friends. Don't use gets, main is always "int main". Call an exorcist for the conio.h.

With functions, you could do something like:
int showMenu() {
	int k;
	
	puts("\n\t\t\tCalculator Menu\n");
	puts("\n\t\t1)\tPSI to BAR\n");
	puts("\n\t\t2)\tBAR to PSI\n");
	puts("\n\t\t3)\tTime from Distance and Speed\n");
	puts("\n\t\t4)\tPower from Volt and Amps\n");
	puts("\n\t\t5)\t555 Timer\n");
	puts("\n\t\t6)\tExit the package\n");

	printf("\nSelect an option from the above menu:\n");
	scanf("%d",&k);
	return k;
}

void option1() { /* your code here */ }
void option2() { /* your code here */ }
void option3() { /* your code here */ }
void option4() { /* your code here */ }
void option5() { /* your code here */ }

int main() {
	int exit;
	do {
		exit = 0;
		switch(showMenu()) {
			case 1: option1(); break;
			case 2: option2(); break;
			case 3: option3(); break;
			case 4: option4(); break;
			case 5: option5(); break;
			case 6: exit = 1; break;
			default: puts("Error please select the correct key\n");
		}
	} while(!exit);
	
	return 0;
}



Hope this helps.

Baavgai thank you for simplifying the code...but main problem is from the---
}
	gets(title);
	len=strlen(title);
	numSpacesLeft=(80-strlen(title))/2;
	numSpacesRight=80-numSpacesLeft;
	for(i=0;i<numSpacesLeft;i++)
	{
		printf(" ");
	}
	printf("*");
	for(i=0;i<len;i++)
	{	
		printf("*");
	}
	printf("*");
	for(i=0;i<numSpacesRight;i++)
	{
		printf(" ");
	}
	printf("\n");
	for(i=0;i<numSpacesLeft;i++)
	{
		printf(" ");
	}
	printf("*");
	printf("%s",title);
	printf("*");
	for(i=0;i<numSpacesRight;i++)
	{
		printf(" ");
	}
	printf("\n");
	for(i=0;i<numSpacesLeft;i++)
	{
		printf(" ");
	}
	printf("*");
	for(i=0;i<len;i++)
	{
		printf("*");
	}
	printf("*");
	for(i=0;i<numSpacesRight;i++);
	{
		printf(" ");
	}
do{

part of the code it seems to input an integer value and output it instead of the string....because the string has to be output in a box of astericks on the middle of the screen which is not happening and and I cannot figure out WHY....thank you for assisting me with my code thou NUGH KUDOS to you man...still cracking my head.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5908
  • View blog
  • Posts: 12,814
  • Joined: 16-October 07

Re: improved menu system using do while loop to display input key

Posted 12 April 2011 - 12:42 PM

To be honest, I was unsure what you were after. I rewrote this a little:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void repeatChar(char ch, int times) {
	int i;
	for(i=0;i<times;i++) { printf("%c", ch); }
}

void strangeStuffWithString(char *title) {
	int len, numSpacesLeft, numSpacesRight;
	int width = 80;

	len = strlen(title);
	numSpacesLeft = (width - len)/2;
	numSpacesRight = width-numSpacesLeft;
	
	repeatChar(' ', numSpacesLeft);
	printf("*");
	repeatChar(' ', len);
	printf("*");
	repeatChar(' ', numSpacesRight);
	printf("\n");

	repeatChar(' ', numSpacesLeft);
	printf("*");
	printf("%s",title);
	printf("*");
	repeatChar(' ', numSpacesRight);
	printf("\n");
	
	repeatChar(' ', numSpacesLeft);
	printf("*");
	repeatChar(' ', len);
	printf("*");
	repeatChar(' ', numSpacesRight);
	printf("\n");
}



Even that's much, but I got the idea. There is a C gotcha here. When you use gets ( never use gets, more later), you also get the newline character, which I assume it the issue you're having.

Here's a quick test program for you:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void printLine(char *s, int len) {
	int width = 70;
	int numSpacesLeft = (width - len)/2;
	printf("%*c*", numSpacesLeft, ' ');
	printf("%*s", len, s);
	printf("*%*c\n", (width - numSpacesLeft - len), ' ');
}

void bannerPrint(char *title) {
	int len = strlen(title);
	if (title[len-1]=='\n') { title[--len]=0; }
	printLine("", len);
	printLine(title, len);
	printLine("", len);
}

int main(int argc, char *argv[]){
	char title[80];
	bannerPrint(fgets (title, 79, stdin));
	
	return 0;
}



I make use of a rather arcane printf function there with the "*". Look it up: http://www.cplusplus.../cstdio/printf/

Also, always prefer fgets to gets. Look that up too, on the same site.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1