Hey, I have created this program that displays a menu and asks the user to select their option and program will do that option. I tried to make my code as easy to understand but if you need help understanding please ask me.
Code:
char user1; // Variable for users menu option selection
int totalCounter = 0; // counter for how many times 'E' is executed
int array[10]; // Array for the user inputted number
int positiveCount; // variable for how many times positive numbers were entered
int positiveArray[10]; // array for storing the positive numbers
int positiveSum; // variable for calculating the sum of the positive numbers
int negativeCount; // variable for how many time negative numbers were entered
int negativeSum; // variable for the sum of the negative numbers
int negativeArray[10]; // array for storing the negative numbers
int j=0; // used for storing integers into the array ('array')
int main(void)
{
/* Main Menu Start */
printf ("\n*======================================================*"
"\nE (e): Enter a new number *" // prompts the user to enter a number
"\nD (d): Display all the numbers *" // displays all the numbers
"\nP (p): Display all positive numbers and statistics *" // displays all positive numbers and its total count and sum
"\nN (n): Display all negative numbers and statistics *" // displays all negative numbers and its total count and sum
"\nC (c): Clear all data in the memory *" // clears all the elements in the 'array'
"\nQ (q): Quit Program *" // exists the program
"\n*========================================================*"
"\n*Enter your choice: "); // promps the user to enter a selection
scanf (" %c", &user1); // saves it to 'user1'
/* Main Menu End */
user1 = toupper(user1); // Turns all the user inputs for the menu into to upper case
while ( j != 10 ) // Need to loop until all the elements in the variable 'array' are filled (not working)
{
switch (user1)
{
case 'E':
printf ("\nPlease enter an integer number: "); // asks to enter an integer
scanf ("%d", &array[j]); // saves it to the variable 'array' in the 'j' index (set to 0)
j++; // j is increased by one
totalCounter++; // adds one to the total counter
break;
case 'D':
printf ("\nSo far these are the numbers you entered: "); // prints all the numbers entered
for ( int i=0; i < totalCounter; i++ ) // does the loop the same number of times that the total counter is equal to
{
printf ("%d, ", array[i]); // prints all the numbers in each of the index's at the array 'array'
}
printf ("\n\nTotal numbers: %d\n", totalCounter); // prints the total count of the numbers
break;
case 'P':
printf ("\nThese are all the positive numbers you entered: "); // prints all the positive numbers
for ( int i=0; i < totalCounter; i++ ) // does the loop the same number of times that the total counter is equal to
{
if ( array[i] > 0 ) // checks for positive numbers in the index's in 'array'
{
printf ("%d, ",array[i]); // prints them
positiveCount++; // increases the positive count by 1
positiveArray[i] = array[i]; // saves all the positive numbers in 'array' to 'positiveArray'
}
}
for ( int i=0; i < 10; i++ ) // does the loop for 10 times because 10 integers in the 'positiveArray'
{
positiveSum = positiveSum + positiveArray[i]; // positiveSum is calculated (not working after one loop)
}
printf ("\n\nTotal Numbers: %d\n\n", positiveCount); // displays total positive count
printf ("The total sum is: %d\n", positiveSum); // displays total positive sum
break;
case 'N':
printf ("\nThese are all the negative numbers you entered: "); // prints all the negative numbers
for ( int i=0; i < totalCounter; i++ ) // does the loop the same number of times that the total counter is equal to
{
if ( array[i] < 0 ) // checks for negative numbers in the index's in 'array'
{
printf ("%d, ",array[i]); // prints them
negativeCount++; // increases the negative count by 1
negativeArray[i] = array[i]; // saves all the negative numbers in 'array' to 'negativeArray'
}
}
for ( int i=0; i < 10; i++ ) // does the loop for 10 times because 10 integers in the 'negaiveArray'
{
negativeSum = negativeSum + negativeArray[i]; // negativeSum is calculated (not working after one loop)
}
printf ("\n\nTotal Numbers: %d\n\n", negativeCount); // displays total negative count
printf ("The total sum is: %d\n", negativeSum); // displays total negative sum
break;
case 'C':
printf ("\nClearing all data in the memory!!\n"); // clears all data in the elements of the main 'array'
j = 0; // by making j=0 (does not work)
printf ("\nDone!\n"); // prints done
break;
case 'Q':
exit(1); // exists if 'user1' is equal to q
break;
}
printf ("\n*Enter your choice: "); // loops again by asking another choice
scanf (" %c", &user1); // save to 'user1'
user1 = toupper(user1); // makes it uppercase
}
system("PAUSE");
return 0;
}
It works fine but there are 3 problems that occur after looping the program first..
1) The positive and negative sum only works if I do them in order so if I choose option 'E' two times in a row and then press 'P' or 'N' it will work and show me the total sum and the numbers but if i press 'E' two times in a row and then press 'P' or 'N' and then press 'E' again and then press 'P' or 'N' again, it will show me the wrong sum and the wrong count of the numbers.
2) I am trying to loop this program until j doesn't equal to 10 which is the same as while 'array' is not upto its [10] index, the problem is I can keep looping the program even after j=10 or in other words I can keep looping even after I used all the 10 elements in 'array'.
3) In option 'C', I tried to clear the memory of 'array' by setting j=0 which will clear all the elements in 'array' but that does not work. How can I clear all the elements in 'array'?
I hope someone can help me. Thanks a lot in advance.