the purpose of my assignment is to create a program using arrays and strings, with functions and parameter passing. i'm sort of having trouble with it anyway, but i'm currently stuck on the "load" function that i'm supposed to write. i've already written the isFull and addMessage (which i'm not positive about whether they're working correctly). in the load function i'm asked to prompt the user to enter a key and a message (stops asking when a negative key is entered) that will then store the input into corresponding arrays. the reference parameter "numElements" is supposed to be kept track of during the program.

if you find errors in the isFull and addMessage functions please let me know.
this is what i currently have:

#include <iostream> // For input and output to the monitor
#include <fstream> // For file input and output (given)
#include <string> // For text data
#include <cstring> // includes strcpy
using namespace std; // To make is easier put this in global namespace

/**** Constants ****/
// MAX_SIZE represents the maximum number of elements in the arrays
const int MAX_SIZE = 100;

/**** Function prototypes ****/

// Function:      print
// Parameters:    
// Functionality: 
void print(string[], int);
// Preconditions: 

// Function:      load
// Parameters:
// Functionality: 
void load(string[], int[], int&);
// Preconditions: 

// Function:      save
// Parameters:
// Functionality: 
void save(string, string[], int[], int);
// Preconditions: 

// Function:      encrypt
// Parameters:
// Functionality: 
void encrypt(string[], int[], int);
// Preconditions: 

// Function:      isEmpty
// Parameters:
// Functionality: 
bool isFull(int);
// Preconditions: 

// Function:      addMessage
// Parameters:
// Functionality: 
void addMessage(string[], int[], int, string, int);
// Preconditions: 


int main() // Begin of main program function
{
	int choice;
	string messages[MAX_SIZE], filename;
	int keys[MAX_SIZE];
	int numElements = 0;

	//these are the given variable declarations
		
	do
	{
		cout << "*************** MAIN MENU ***************" << endl;
		cout << "(1) \"Load\" File" << endl;
		cout << "(2) Save File" << endl;
		cout << "(3) Encrypt" << endl;
		cout << "(4) Print Messages to the Screen" << endl;
		cout << "(5) Quit" << endl;
		cout << "Please enter a selection from the menu: ";
		cin >> choice;
		
		switch(choice)
		{
			case 1: // Read user input into the arrays provided
				load(messages, keys, numElements);
				break;
		
			case 2: // This function is given: write encrypted file
				cout << "Please enter the name of the file you want to save to: ";
				cin >> filename;
				save(filename, messages, keys, numElements);
				break;
		
			case 3:
				cout << " ***** ENCRYPTING ***** " << endl;
				encrypt(messages, keys, numElements);
				cout << " ***** ENCRYPTION DONE ***** " << endl;
				break;
		
			case 4:
				cout << " ***** MESSAGES IN THE ARRAY ***** " << endl;
				print(messages, numElements);
				break;
		
			case 5:
				cout << " GOODBYE! " << endl;
				break;		

			default:
				cout << choice << " is not a valid selection ... Try again." << endl;
		}
	} while(choice != 5);

	return 0;
} // End of main function

/**** Function definitions ****/
// TODO:  Place the definitions to your functions here.

// This function is given, because we havent covered it yet: save
void save(string file_in, string messages_in[], int keys_in[], int numElements_in)
{
	// Declaring a new file, and opening it on the disk
	ofstream out_stream;
	out_stream.open(file_in.c_str()); // file_in must be converted to a C-string

	// Write every single key/message pair to the file, up to numElements_in
	for (int message_idx = 0; message_idx < numElements_in; message_idx ++)
	{
		out_stream << keys_in[message_idx] << " " << messages_in[message_idx] << endl;
	}

	// Very important: close the file again. Otherwise there may be data loss
	out_stream.close();
}

// This is the function definition for the function: print
void print(string[], int)
{
	

}

// This is the function definition for the function: load
void load(string messages[], int keys[], int& numElements)
{
	int key;
	string message;
	int index = 0;
	while((key >= 0) && (index < MAX_SIZE))
	{
		cout << "Please enter a key number: \n";
		keys[index] = key;
		cin >> key;
		cout << "Please enter a message: \n";
		messages[index] = message;
		getline(cin, message);		
		index++;
		
	}	
	numElements = index;
		
}

// This is the function definition for the function: encrypt
void encrypt(string[], int[], int)
{
	
	
}

// TESTING FOR IS FULL FUNCTION

bool isFull(int numElements)
{
	
	if(numElements < MAX_SIZE)
	{
		return false;
	}
	else
	{
		return true; 
	}
}


// Testing for ADDMESSAGE function

//string[]=array of messages
//int[]=the array of keys
//int = numElements
//string= the text of the new message to be submitted in array
//int= the corresponding key to go with the message, to go into the array of keys

void addMessage(string messages[], int keys[], int numElements, string message, int key)
{
//this function takes in a message and one key, adds them to an array of message and keys
//and adds to keep track of how many elements are currently being stored. if it is full it
//outputs an error message. USES ISFULL FUNCTION

	if(!isFull)
	{
		messages[numElements]+=message;
		keys[numElements]+=key;
		numElements++;
	}
	else
	{
		cout << "error, not enough space\n";
	}

	

}

Recommended Answers

All 2 Replies

>if you find errors in the isFull and addMessage functions please let me know.
I'll focus on those two, but be aware that there are problems in the rest of your code.

>if(!isFull)
You forgot that isFull is a function. It needs an argument list.

>messages[numElements]+=message;
>keys[numElements]+=key;
Most likely you meant to use = instead of +=:

void addMessage(string messages[], int keys[], int numElements, string message, int key)
{
  if(!isFull(numElements))
  {
    messages[numElements]=message;
    keys[numElements]=key;
    numElements++;
  }
  else
  {
    cout << "error, not enough space\n";
  }
}

It's hard to get isFull wrong, but you can make it more concise:

bool isFull(int numElements)
{
  return numElements >= MAX_SIZE;
}

thank you for your help. i had the feeling that i was making it more complicated then it actually was.

my issue with the load function is that when i run the program it prompts the user for a key, and when you hit enter it shows the line "please enter a message" but doesn't allow you to write text, it will only accept integers. otherwise the program will keep repeating its output message.

i'm also supposed to call the "addMessage" function somewhere within the load function and i'm completely lost on how i'm supposed to do that. any ideas/tips?

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.