so i'm trying to fix my "load" function, but i can't seem to get it to work correctly.

here's the load function i have currently:

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

so what needs to happen is, i'm supposed to prompt the user for a key and message (which will be stored in their corresponding array). the program is supposed to keep asking for a key and message until a negative key value is entered. within the load function i'm supposed to call the addMessage function to add the key/message pairs to their arrays.

here's the addMessage function:

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. 

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

	

}

when i run the program the load function asks for a key, but won't let me enter a message. i can't figure out what the issue is. i also think i'm having a hard time understanding how i'm supposed to call the addMessage function from the load function.

any help will be appreciated!

i'm confused, are you saying i need to make numElements a call by reference parameter, or i'm supposed to say index = numElements? i'm sorry i'm really confused. i'm making this more difficult then it probably is.

>>are you saying i need to make numElements a call by reference parameter
yes -- see below

void addMessage(string messages[], int keys[], int& numElements, string message, int key)
{

you don't have to change load() at all.

i'm still having issues when i try to add input when using the program:

#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 index = 0;
	int key;
	string message;
	while(index < MAX_SIZE && key > 0)
	{
		cout << "Please enter a key for number " << (index + 1) << ":\n";
		cin >> key;
		cout << "Please enter a message for number " << (index + 1) << ":\n";
		getline(cin, message);		
		if(key > 0)
		{
			addMessage(messages, keys, numElements, message, key);
			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)
{
	return numElements >= MAX_SIZE;	
}


// 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(numElements))
	{
		messages[numElements]=message;
		keys[numElements]=key;
		numElements++;

	}
	else
	{
		cout << "error, not enough space\n";
	}

	

}

i think i have my load function set up incorrectly or something. i've tried several different approaches, but it still wont let me input a message

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

after adding the second "cin >> message" it appears to be working correctly, but i dont understand why that would be.

unless there are spaces in the message! i dont know how to fix that.

What compile erros and warnings are you getting ? Did you correct them ? You can not just ignore warnings and expect the program to work right. For example: while(index < MAX_SIZE && key > 0) generates warning that key is used before initialized. Change it to 1.

in load() you need to flush the '\n' after entering the number cin.ignore(); after cin >> key;

when you say "change it to 1" do you just mean that i should just used one of the parameters? like only key>0, or only index<MAX_SIZE?

Hi rem0404,

I have tried the overloaded function of istream& getline and it works.
At line 143. You have this code

143 getline(cin, message);

and what I did is just used the overload getline which has the delim parameter. The code is something like this.

143 getline(cin, message,'.');


But the problem is you need to put the delimiter '.' everytime you are inputing a message.

Hope this will help you!

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

This is what I mean.

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

>>But the problem is you need to put the delimiter '.' everytime you are inputing a message.
NO, that is not the problem and you don't have to do anything like that.

this is the code i have so far. my program is supposed to show a menu with 5 options. so far i have all the functions completed except the encrypt function. everything is working fine. except the only thing that's slightly off is that when the user enters messages and then prints them out they dont appear in the order that they were entered. i'm not sure if that's significant but that seems to be the only thing that isn't working so far.

in the encrypt function i'm supposed to take the messages entered into the arrays and encrypt them (when they are encrypted the corresponding key should be added to the ASCII code for each character in the string, to decrypt it should be subtracted). They are also supposed to be encrypted into printable ASCII characters (32-126).

#include <iostream> // For input and output to the monitor
#include <fstream> // For file input and output (given)
#include <string> // For text data

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 messages[], int numElements)
{
	for(int i=0; i < numElements; i++)
	{
		cout << messages[i];
		cout << endl;
	}
}

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

			cout << "Please enter a message for number " << (index + 1) << ":\n";
			getline(cin, message);	
			messages[index+1]=message;
			addMessage(messages, keys, numElements, message, key);
			index++;
		}
	}
	numElements = index;
}


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

// TESTING FOR IS FULL FUNCTION

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


// 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(numElements))
	{
		messages[numElements]=message;
		keys[numElements]=key;
		numElements++;

	}
	else
	{
		cout << "error, not enough space\n";
	}

	

}
This question has already been answered. Start a new discussion instead.