Jump to content

Help with exiting my Program

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
shinilover

shinilover

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,

I am writing a program where the user has to input 10 temperatures and then select an option on what to do with the 10 temps. I am having problems with the exit part of the program. I want it to exit when the user inputs the letter e. Thank you in advance for your opinions and help. Here is my program:

//Source Code -> Temp.c
//Purpose ->To work with array of MAX temps, to define a constant for the array size, to use sort and search algorithms, and to write a menu driven program.

#include<stdio.h>
#include<math.h>
#define MaxTemp 10

void getTemps(int[],int);
void printTemps(int[],int);
void AvgTemps(int[],int);
void HighTemps(int[],int);
void LowTemps(int[],int);
void SortTemps(int[],int);

int main(void)

{
	
	int option;
	int Temps[MaxTemp] = {0};
	
do
{

	printf("\n Temperature Calc:\n\n");
	printf("\n 1. Input Temperatures");
	printf("\n 2. Print Temperatures");
	printf("\n 3. Print Temperature Average");
	printf("\n 4. Print the Highest Temperature");
	printf("\n 5. Print the Lowest Temperature");
	printf("\n 6. Sort Temperature:");
	printf("\n 7. Press 'E' in operation to exit\n\n");
	printf("Please select a number:");
	scanf("%d", &option);


switch(option)

{

	case 1: 
	
	printf("Input \n");
	getTemps(Temps,10);

	break;

	
	case 2:

	printf("Show Temps\n");
	printTemps(Temps,10);
	break;
	
	case 3:
	
	printf("Average\n");
	AvgTemps(Temps,10);

	break;
	
	case 4:

	printf("High\n");
	HighTemps(Temps,10);
	break;
	
	case 5:
	
	printf("Low\n");
	LowTemps(Temps,10);
	break;
	
	case 6:
	
	printf("Sort\n");
	SortTemps(Temps,10);
	break;


	default: 
		
	printf ("Error");


	

}	

}while(option != 'E');


return 0;

}



void getTemps(int temp[], int size)

	{
		int counter = 0;
		for(counter = 0; counter < size; counter++)
		{
			printf("Enter Temp %d:\n", counter + 1);
			scanf("%d", &temp[counter]);
		}
}

void printTemps(int temp[], int size)

	{
		int counter = 0;
		for(counter = 0; counter < size; counter++)
		{
		 printf("Temperature %d \n", temp[counter]);
		}
}

void AvgTemps(int temp[], int size)

	{
		int counter = 0;
		int AvgTemps = 0;
		int average = 0;
		for(counter = 0; counter < size; counter++)
		{
			AvgTemps +=temp[counter];
		}

		AvgTemps  /= size;
		printf("average %d \n", AvgTemps);
}


void HighTemps(int temp[], int size)

	{
		int counter = 0;
		int high = -1000; 
		for(counter = 0; counter < size; counter++)
		{
			if(high < temp[counter])
				high = temp[counter];
			
			
	}
printf("high %d \n", high);

}
void LowTemps(int temp[], int size)

	{
		int counter = 0;
		int low = 1000; 
		for(counter = 0; counter < size; counter++)
		{
			if(low > temp[counter])
				low = temp[counter];
			
			

	}
printf("low %d \n", low);

}

void SortTemps(int temp[], int size)
{
	int counter1,counter2;
	int tempValue;

	for(counter1=0; counter1 < size - 1; counter1++)
		for(counter2 = 0; counter2 < size -1-counter1; counter2++)
			if(temp[counter2 + 1] < temp[counter2])
			{
				tempValue = temp[counter2];
				temp[counter2] = temp[counter2 + 1];
				temp[counter2 + 1] = tempValue;

		
					}
	}

Edited by v0id, 01 May 2008 - 09:13 PM.
Remember code-tags!


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I notice that it will exit on 'E' but not 'e', is that your issue?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
You need to either use a logical OR statement to decide whether both cases of 'e' are captured, or convert the keystroke to upper case. It may be better to use the Escape key, as it is more intuitive for a user.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#4
shinilover

shinilover

    Newbie

  • Members
  • Pip
  • 2 posts
I found the problem!! Thanks everyone for helping me with this program.

#5
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
What was it in the end?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums