I am currently writing a simple program that matches a already written program, with certain specifications that are required. For the most part I have written what seems like working code, but I do have problems.

Here is the program that I have to make: Sample

1st problem: When the program first runs, it goes to updating the array, but the program ends after that. It's supposed to start over again, letting the user choose what they want to do next.

2nd problem: How do I display the printPostfix when displaying the array or updating it.

I know there is a lot of code below, but I've been trying for a while on how to fix the issue.

#include <iostream>
#include <iomanip>
#include <cctype>

#define myline 	cout << "==----------------------------------------------==\n"
#define mytab   cout<<"\t"
#define line	cout <<"\n+---------------------------------------------+\n";

using namespace std;

bool firstTry = true;

#include "menuFoos.h" 
#include "arrFoos.h"

int main()
{

	char again; 

	int lower = 0, upper = 6;

	//define my array
	const int size = 5;
	double arr[size];

	//start of do-while to control a text menu interface 	
	do{
		displayMenu();

		//first time, must start from option 1.
		//not first time, free to choose options.

		while(firstTry == true)
		{
			{
				cout << "It Is Your First Try, We Will Create My Array First:\n";
				updateArr(arr, size);
			}
			return firstTry = false;
		}

		int op = option(upper, lower);

		if(op == 0)
		{
			return 0;
		}
		else if(op == 1)
		{
			updateArr(arr, size);
		}
		else if(op == 2)
		{
			displayArr(arr, size);
		}
		else if(op == 3)
		{
			sumArr(arr, size);
		}
		else if(op == 4)
		{
			aveArr(arr, size);
		}
		else if(op == 5)
		{
			minArr(arr, size);
		}
		else
			maxArr(arr, size);


		char again = choice();

	}while(again=='y'); //end of do-while

	system("pause");
	return 0;	
}

"menuFoos.h" header file

//function prototype
void displayMenu( );    //display a text menu
char choice( );     	// get user�s answer on yes or no
int option(int, int);  	//read user�s option in the text menu


//function definitions
char again;

//not completed
// get user�s answer on yes or no
char choice()
{
	//insert code....
	{
		line;
		cout << "Want To Know More About Your Array? y(yes) or n(no):";
		line;
		cin >> again;
	}
	return again;
} 

//read user�s option in the text menu
int option(int upper, int lower)
{
	int op; 	
	cout<<"Enter "<<lower<< "-"<<upper
		<<", "<< lower <<" for exit:"<<endl;
	cin >> op;
	while(cin.fail() || op > upper || op < lower)
	{
		cin.clear();
		fflush(stdin); //for windows.
		cout << "You did not enter valid option, "
			<< "please enter again:" 
			<< lower <<" ~ "<< upper<<":\n";
		cin >> op;
	}
	return op;
}

void displayMenu()
{
	//system("command /c cls");
	cout << endl << endl;
	cout << "\t\t+++++++++++++++++++++++++++++++++++++++++++\n\n";
	cout << "\t\t\t    Play with my Array now " << endl << endl;
	cout << "\t\t+++++++++++++++++++++++++++++++++++++++++++\n";
	cout << "\t\t\t1:  creating/updating my array" << endl;
	cout << "\t\t\t2:  displaying my array" << endl;		
	cout << "\t\t\t3:  summing all the elements" << endl;
	cout << "\t\t\t4:  averaging the array" << endl;
	cout << "\t\t\t5:  largest element and its index" << endl;
	cout << "\t\t\t6:  smallest element and its index" << endl;
	cout << "\t\t\t0:  exit" << endl;		
	cout << "\t\t+++++++++++++++++++++++++++++++++++++++++++\n\n";
	cout << "\x3e"<<" Enter an integer (0-6), \n" ;
	cout << "\x3e"<<" Enter 0 to exit the program\n\n";
}

"arrFoos.h" header file

// data input functions
double getNumber( );  // read a number from keyboard
void printPostfix(int);   // print some

// array manipulations functions
void updateArr(double [ ], int);   	//create or update an array
double sumArr(double [ ], int);  	//return the sum of an array
double aveArr(double [ ], int);  	// return the average of an array
void displayArr(double [ ], int);  	//display array elements
double minArr(double [ ], int);    	//find and display the smallest element
double maxArr(double [ ], int);    	//find and display the largest element

//function definitions
int i;


//not completed: 
//read a number from keyboard
double getNumber()
{
	double num;
	cout << "Enter a number";
	cin >> num;
	return num;
}  

//complete
void printPostfix(int n)
{
	if ((n % 10 == 1) && !(n == 11))
		cout << "st"; 
	else if ((n % 10 == 2) && !(n == 12))
		cout << "nd"; 
	else if ((n % 10 == 3) && !(n == 13))
		cout << "rd"; 
	else
		cout << "th";
	return;
}


//not completed
//create or update an array
void updateArr(double arr[], int size)
{
		myline;
		cout<<" ---> Create/update my array by entering 5 integers:\n";
		myline;

		int elementNumber = 0;

		for(int i = 0; i < size; i++)
		{
			cout << "Enter " << elementNumber << printPostfix << " Element: ";
			cin >> arr[i];
			elementNumber++;
		}

		myline;

		cout<<"Congraduations! my array has been created "
			<<"or updated.\n\n";
	return;
}
//not completed.			  
double sumArr(double arr[], int size)
{
	double sum = 0;
	for(int i = 0; i < size; i++)
	{
		sum += arr[i];
		return sum;
	}	
}

//not completed
double aveArr(double arr[], int size)
{
	double sum = 0;
	double avg = 0;
	for(int i = 0; i < size; i++)
	{
		sum += arr[i];
		return sum;
	}
	return sum / size;
}

//not completed
void displayArr(double arr[], int size)
{
	cout<<"\n Displaying my Array:";
	for(int i = 0; i < size; i++)
	{
		cout << "The 0" << printPostfix << "Element: " << arr[i] << endl;
		cout << "The 1" << printPostfix << "Element: " << arr[i] << endl;
		cout << "The 2" << printPostfix << "Element: " << arr[i] << endl;
		cout << "The 3" << printPostfix << "Element: " << arr[i] << endl;
		cout << "The 4" << printPostfix << "Element: " << arr[i] << endl;
	}
	cout << endl;
}
}

Recommended Answers

All 3 Replies

while(firstTry == true)
		{
			{
				cout << "It Is Your First Try, We Will Create My Array First:\n";
				updateArr(arr, size);
			}
			return firstTry = false; //Don't return here, hehe. 
		}

Should be something along the lines of..

if(firstTry == true)
		{
			cout << "It Is Your First Try, We Will Create My Array First:\n";
			updateArr(arr, size);
			firstTry = false;
		}
while(firstTry == true)
		{
			{
				cout << "It Is Your First Try, We Will Create My Array First:\n";
				updateArr(arr, size);
			}
			return firstTry = false; //Don't return here, hehe. 
		}

Should be something along the lines of..

if(firstTry == true)
		{
			cout << "It Is Your First Try, We Will Create My Array First:\n";
			updateArr(arr, size);
			firstTry = false;
		}

Thanks, that worked. I got the program working.

Great, no problem. Good question-post. I take it for granted that you know why the program works like it does now?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.