Here is the entire program(with comments):
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void rectangle(); //function that will draw a rectangle with dimensions provided by the user
void number(); //function for printing the sum of positive integers whose digits are rising from left to right
void main()
{
int menu = 1; //while this variable is set to 1, the loop that shows the menu continues
int choice = 0; //a variable to store the menu choice the user makes
char exit = 'n'; //stores the choice the user makes in the 3rd menu item
while(menu == 1)
{
while(choice <= 0 || choice > 3) //if the user enters an invalid value, the menu will appear again
{
printf("\nChoose an item from the menu:\n");
printf("1. Draw a rectangle!\n");
printf("2. Calculate a sum!\n");
printf("3. Exit?\n");
fflush(stdin);
scanf("%d", &choice);
}
if(choice == 1) //for each choice, except 3, the appropriate function is executed
rectangle();
if(choice == 2)
number();
if(choice == 3) //this choice is built into the main function
{
printf("Do you really want to quit? Press 'y' or 'Y' to confirm, any other key to cancel");
scanf("%c", exit);
if(exit == 'Y' || exit == 'y')
menu = 0;
}
}
}
void rectangle()
{
int length, width; //dimensions of the rectangle
int dimensions; //variable to check if the coordinates of the symbol are okay
int row, col; //row and column coordinates of the symbol
int symbol_count; //how many “-“ were printed
int row_count; //used to track how many rows were printed in the rectangle
char symbol = 'n'; //the symbol the user chooses to print inside the coordinates
printf("Please enter the length and width of the rectangle, seperated by a space\n"); //requesting the dimensions of the rectangle from the user
scanf("%d %d",&length, &width);
if(length < 0) //checking if the coordinates of the rectangle given by the user are negative
length = -length;
if(width < 0)
width = -width;
dimensions = 0;
while(dimensions == 0) //checking if the coordinates given for the symbol are not negative or too big
{
printf("Please enter the row and column coordinates of a point in the rectangle,\n seperated by a space\n");
scanf("%d %d",&row, &col);
if(row >= 0 && row <= length)
{
if(col >=0 && col <= width)
dimensions = 1;
else
printf("Invalid column coordinates\n");
}
else
printf("Invalid row coordinates\n");
}
printf("Please enter a character to be printed in the rectangle\n"); //taking the character to be printed in the rectangle
fflush(stdin);
scanf("%c", symbol);
symbol_count = 0;
row_count = 0;
while(symbol_count < width*length) //once the symbol_count reaches the dimensions of the rectangle, the loop will break
{
if(symbol_count == col)
if(row_count == row)
printf("%c", symbol);
else if(symbol_count == width)
printf("\n");
else
printf("-");
symbol_count++;
}
}
void number()
{
int num; //this is the original number, which will be printed if it meets the criterion
int num_test; //the number will be working on in the function
int num_ok = 2; //the flag used to mark the number as good for adding to the sum that will be printed
int mod1, mod2; //variables that will be used to compare the digits of the number
int sum = 0; //the sum of "good" numbers, which is set to zero here and will be added to in the function.
while(num > 0) //the loop will continue for as long as the user enters positive numbers
{
printf("Please insert an integer:\n"); //taking the original integer
fflush(stdin);
scanf("%d",&num);
num_test = num; //setting test_number to the value of num, as entered by the user
while(num_test > 0 && num_ok != 0) //this loop will quit if the user enters a non-positive zero or a number with digits that aren't rising
{
mod1 = num_test%10;
mod2 = (num_test/10)%10;
if(mod2 > mod1) //once the user enters a number with digits that aren't rising, the expression in the if statement will be false and num_ok will be set to zero, which will break the loop
num_ok = 1; //if each of the digits passes the test, num_ok will remain equal to 1
else num_ok = 0;
if(num_test == 0 && num_ok == 1); //if the division reached the final stage(num_test is equal to zero, and the num_ok flag is still equal to 1, then the number is valid and will be added to the sum
sum = sum + num;
}
printf("%d\n", sum); //printing the final sum
}
}
It compiles but crashes on the first menu item when entering the character to be printed... (maybe it crashes later too, haven't checked yet)
Advice, anyone?