OK, i removed the spaces = spaces, it was originally int here as a fail safe, hopefully preventing errors.
Here is what I get after I select '3' from the menu
Please enter an uppercase character
Please enter your choice
**************************
0. Display Help (this menu)
1. Determine the squares and cubes or a range of integers
2. Determine a suitable sport
3. Display a pyramid
4. Calculate how long a bank balance will last
5. Exit the program
From here I can either enter values for the pyramid function or the menu function and it will work accordingly. There it asks for a charachter, but doesnt give me a chance to enter it until after it has displayd the menu again. I dont understand why it is displaying the menu again. This time I also included the full code, a bit long winded, but Im pretty thrown
Code:
#include <stdio.h> /*standard input output*/
#include <stdlib.h> /*for extra commands, eg. exit command */
#include <conio.h> /*for extra commands, eg. clear screen command*/
#include <ctype.h> /*for extra commands, eg. uppercase command*/
/*Declares Functions*/
void SquareCube();
void Sport();
void Pyramid();
void Bank();
void Exit();
void StudentDetails();
void menu();
/***************************************************************************************************
* Main method *
***************************************************************************************************/
main ()
{
StudentDetails(); /*calls studentdetails*/
menu(); /*calls menu*/
} /*end main*/
/***************************************************************************************************
* Contains user menu to run program *
***************************************************************************************************/
void menu()
{
int user_option;
while (user_option) { /*takes users choice to goto seperate program functions*/
printf("\n");
printf(" Please enter your choice\n");
printf("**************************\n");
printf("0. Display Help (this menu)\n");
printf("1. Determine the squares and cubes or a range of integers\n");
printf("2. Determine a suitable sport\n");
printf("3. Display a pyramid\n");
printf("4. Calculate how long a bank balance will last\n");
printf("5. Exit the program\n");
scanf("%d", &user_option);
if (user_option == 0) menu();
else if (user_option == 1) SquareCube(); /*calls SquareCube function*/
else if (user_option == 2) Sport(); /*calls Sport function*/
else if (user_option == 3) Pyramid(); /*calls Pyramid function*/
else if (user_option == 4) Bank(); /*calls Bank function*/
else if (user_option == 5) Exit(); /*calls Exit function*/
else {
printf("Invalid option. Please try again...\n");
menu();
}
}
} /*end menu*/
/***************************************************************************************************
* Determines squares and cubes of entered numbers *
***************************************************************************************************/
void SquareCube()
{
int i,m,n;
int square;
int cube;
int swap;
clrscr(); /*clears screen*/
printf("Please enter two integers:\n"); /*takes integers from user*/
scanf("%d%d", &m,&n);
while (m < 0 || n < 0) { /*checks for numbers below zero*/
printf("Invalid entry. Use numbers above 0\n");
printf("Please re-enter two integers:\n");
scanf("%d%d", &m,&n);
}
if (n < m) { /*swaps digits to make first calculation the smallest number*/
swap = n;
n = m;
m = swap;
}
printf("\n");
printf("Number\t\tSquare\t\tCube\n"); /*displays column headings with tab breaks in between*/
for(i = 0; m <= n; m = i++) {
square = m * m; /*calculates square of current number*/
cube = m * m * m; /*calculates cube of current number*/
printf("%d\t\t%d\t\t%d\n", m, square, cube); /*displays results under column headings*/
}
return;
} /*end squarecube*/
/***************************************************************************************************
* Takes an entered number and determines which sport is best *
***************************************************************************************************/
void Sport()
{
float temp;
float highest;
float lowest;
float running_temp;
float average = 0;
float counter = 0;
clrscr(); /*clears screen*/
while(temp) {
printf("Please enter the temperature (enter 99 to exit):\n"); /*takes temperature from user*/
scanf("%f", &temp);
if (temp >= 99 || temp <= -99) { /*checks whether to exit this function*/
if (counter == 0) { /*safeguards against division by zero program crash*/
printf("No temperatures entered\n");
return 0;
}
else {
average = running_temp / counter; /*calculates average on function exit*/
printf("Highest temperature: %f\n", highest); /*displays results*/
printf("Lowest temperature: %f\n", lowest);
printf("Average temperature: %f\n", average);
return 0;
}
}
else {
if (counter == 0) { /*sets benchmark highest and lowest temperatures*/
lowest = temp;
highest = temp;
}
if (temp > highest) { /*checks for new highest temperature*/
highest = temp;
}
else if (temp < lowest) { /*checks for new lowest temperature*/
lowest = temp;
}
running_temp = running_temp + temp; /*sums up entered temperatures to find average*/
counter = counter + 1; /*counter to work out average, determines dividing number*/
if (temp > 30) printf("Suitable sport is swimming\n"); /*determines which sport is best*/
else if (temp <= 30 && temp > 20) printf("Suitable sport is tennis\n");
else if (temp <= 20 && temp > 0) printf("Suitable sport is golf\n");
else if (temp <=0 && temp > -10) printf("Suitable sport is skiing\n");
else printf("Suitable sport is chinese checkers\n");
}
}
} /*end sport*/
/***************************************************************************************************
* Displays a pyramid of characters *
***************************************************************************************************/
void Pyramid()
{
char character;
char current;
char current_line;
int spaces = 0;
clrscr(); /*clears screen*/
printf("Please enter an uppercase character\n");
scanf("%c", &character);
character = toupper(character); /*converts to uppercase*/
for (current_line = 'A'; current_line <= character; current_line++) {
spaces = (character - current_line - 1); /*sets number of spaces for a particular line*/
for (; spaces >= 0; spaces--) { /*displays spaces*/
printf(" ");
}
for (current = 'A'; current < current_line + 1; current++) { /*displays ascending characters*/
printf("%c",current);
}
current = current - 1; /*prevents repeating characters*/
for (current = current - 1; current >= 'A'; current--) { /*displays decending characters*/
printf("%c",current);
}
printf("\n"); /*starts next line*/
}
//return 0;
} /*end pyramid*/
/***************************************************************************************************
* Determines the time an amount of money will last in a bank account *
***************************************************************************************************/
void Bank()
{
float bal_new;
float bal;
float interest_rate;
float earned;
float withdrawl;
int years = 1;
clrscr(); /*clears screen*/
printf("Please enter account balance\n"); /*takes starting values from user*/
scanf("%f", &bal);
printf("Please enter yearly interest rate as percentage\n");
scanf("%f", &interest_rate);
printf("Please enter yearly withdrawl\n");
scanf("%f", &withdrawl);
printf("\nYear\t\tBalance\t\tInterest\tWithdrawn\tNew Balance\n"); /*displays column headings*/
for (bal = bal; bal >= 0; years++) { /*loops until balance is less than 0*/
earned = (bal * (1 + (interest_rate /100))) - bal; /*calculates earnt interest*/
bal_new = ((bal * (1 + (interest_rate / 100))) - withdrawl); /*calculates the new balance*/
if (bal < withdrawl) { /*displays last wihdrawl and number of years account lasts for*/
withdrawl = bal + earned;
printf("%d\t\t%f\t%f\t%f\t0\n", years, bal, earned, withdrawl);
printf("Number of years for account to reach zero is %d\n", years);
return;
}
else { /*displays running totals*/
printf("%d\t\t%f\t%f\t%f\t%f\n", years, bal, earned, withdrawl, bal_new);
bal = bal_new;
}
}
} /*end bank*/
/***************************************************************************************************
* Exits program after completing a loop *
***************************************************************************************************/
void Exit()
{
int pause;
clrscr(); /*clears screen*/
printf("Please wait while program closes.....");
for (pause = 0; pause <= 10000000; pause = pause +1); /*counter to delay exit*/
exit(0);
} /*end exit*/
/***************************************************************************************************
* Prints out student details *
***************************************************************************************************/
void StudentDetails()
{
clrscr(); /*clears screen*/
printf("Name: \n");
printf("Unit: \n");
printf("Tutors Name: \n");
printf("Tutorial Time: \n");
return;
} /*end studentdetails*/