User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 403,204 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,602 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 675 | Replies: 13 | Solved
Reply
Join Date: Apr 2008
Posts: 26
Reputation: rem0404 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rem0404 rem0404 is offline Offline
Light Poster

string arrays: storing input

  #1  
Apr 1st, 2008
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!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,736
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 884
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: string arrays: storing input

  #2  
Apr 1st, 2008
you need to pass numElements by reference in addMessage() function just like you did in load().
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Apr 2008
Posts: 26
Reputation: rem0404 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rem0404 rem0404 is offline Offline
Light Poster

Re: string arrays: storing input

  #3  
Apr 1st, 2008
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,736
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 884
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: string arrays: storing input

  #4  
Apr 1st, 2008
>>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.
Last edited by Ancient Dragon : Apr 1st, 2008 at 8:39 pm.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Apr 2008
Posts: 26
Reputation: rem0404 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rem0404 rem0404 is offline Offline
Light Poster

Re: string arrays: storing input

  #5  
Apr 1st, 2008
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
Reply With Quote  
Join Date: Apr 2008
Posts: 26
Reputation: rem0404 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rem0404 rem0404 is offline Offline
Light Poster

Re: string arrays: storing input

  #6  
Apr 1st, 2008
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.
Last edited by rem0404 : Apr 1st, 2008 at 8:59 pm.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,736
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 884
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: string arrays: storing input

  #7  
Apr 1st, 2008
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;
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,736
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 884
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: string arrays: storing input

  #8  
Apr 1st, 2008
>>unless there are spaces in the message! i dont know how to fix that.
Don't try to fix it because that is not the problem.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Apr 2008
Posts: 26
Reputation: rem0404 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
rem0404 rem0404 is offline Offline
Light Poster

Re: string arrays: storing input

  #9  
Apr 1st, 2008
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?
Reply With Quote  
Join Date: Jun 2006
Posts: 36
Reputation: ramiljoaquin is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 2
ramiljoaquin ramiljoaquin is offline Offline
Light Poster

Re: string arrays: storing input

  #10  
Apr 1st, 2008
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 );
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC