6 Replies - 942 Views - Last Post: 11 December 2008 - 10:53 AM Rate Topic: -----

#1 gamejust4u   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 214
  • Joined: 19-November 08

Trouble calling functions

Post icon  Posted 10 December 2008 - 12:36 PM

ok- In my menu() function depending on which you choose it is suppose to call so another function. What my program does is go straight to my default- "invalid choice" I scrambled this around and finally just need to be pointed into the right direction



#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;
using std::ifstream;
using std::ofstream;
using std::ios;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);

const char FileName[] = "c:/TestAddress.txt";
string database;
ifstream myAddress(FileName);

	string name = " ";
	string address = " ";
	string street= " ";
	string city = " ";
	string state = " ";
	string zipCode = " ";

int main () 
{
		
	menu();
	return 0;
} //end main

void menu(void) 
{
	//allow user to choose to append records, display records or exit the program
	int userChoice = 0;

	//Display Menu
	cout << "\n\n(A)ppend record, (S)how Record, (E)xit:"; 
		cin >> userChoice; 
		//Users Choice

	switch (userChoice)
	{
		case 'a':
		case 'A'://Append Record
			myAddress.open ("TestAddress.txt", ios::app);
			if (myAddress.is_open()) 
				{writeData(); }
		break;

		case 's':
		case 'S'://Show record
			readData();
			
		break;

		case 'e':
		case 'E'://Exit
			myAddress.close();
		break;

		default:
			cout<<"Invalid choice"<<endl;
			cout << endl << endl << endl;
			break;
			
	}

	return;
	
}//end menu

void writeData(void)
{	//Write the Address Info to a file\
	
	char answer = ' ';

	cout<<" Write name and address \n"<<endl;
	cout<< " Enter another record?"<<endl;
	cin>>answer;
	myAddress>>database;

	while (answer != 'n' || answer != 'N')
	{
		cout<< "Enter name";
		cin>>name;
		cout<< "Enter Street";
		cin>>street;
		cout<<" Enter City";
		cin>>city;
		cout<<" Enter State";
		cin>>state;
		cout<<"Enter Zip Code";
		cin>>zipCode;
		cout<<name<<address<<endl;
		cout<<endl<<endl<<endl<<endl;}

	return;
	
}//end write data

void readData(void)
{ 	
	cout<<"Reading a File"<<endl;

	//read data from a file
	getline (myAddress, database, '\n');
	cin>>database;
	//use the split function to break a deliminated line of text into fields
	cout<<"Name"<<"....."<<name<<endl;
	cout<<"Street"<<"....."<<street<<endl;
	cout<<"City"<<"....."<<city<<endl;
	cout<<"State"<<"....."<<state<<endl;
	cout<<"Zip Code"<<"....."<<zipCode<<endl;

	return;
}//end read data

string * split(string theLine, char theDeliminator){
	//Break theline into fields and save the fields to an array.
	//Each field will occupy one element in a character array.
	//theLine is a string with fields separated with theDeliminator character.
	//Assumes the last field in the string is terminated with a newline.
	//Useage: string *theFields = split(lineBuffer, ',');

	//determine how many splits there will be so we can size our array
	int splitCount = 0;
	for(int i = 0; i < theLine.size(); i++){
	if (theLine[i] == theDeliminator)
	splitCount++;
	return 0;
}

splitCount++; //add one more to the count because there is not an ending comma 
	//create an array to hold the fields
	string* theFieldArray;
	theFieldArray = new string[splitCount];
	//split the string into seperate fields
	string theField = "";
	int commaCount = 0;

	for(int i = 0; i < theLine.size(); i++){ //read each character and look for the deliminator
	if (theLine[i] != theDeliminator) {
	theField += theLine[i]; //build the field
	}
	else { //the deliminator was hit so save to the field to the array
	theFieldArray[commaCount] = theField; //save the field to the array
	theField = "";
	commaCount++;
	}
	}
	theFieldArray[commaCount] = theField; //the last field is not marked with a comma...

	return theFieldArray;
} //end split
  


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble calling functions

#2 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Trouble calling functions

Posted 10 December 2008 - 12:43 PM

cin treats int differently to char (even though both are actually just numeric integer types, 'char' is considered by cin to be a special case).

If you type an alpha character when cin expects an int. then cin will fail, not knowing how to handle it - because alpha characters are considered to be human readable text - you won't automatically retrieve its character value instead.

One quick way to fix this would be to change int userChoice = 0; to char userChoice = 0;
Was This Post Helpful? 1
  • +
  • -

#3 gamejust4u   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 214
  • Joined: 19-November 08

Re: Trouble calling functions

Posted 10 December 2008 - 08:38 PM

That did the trick. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 sleeplessgamer   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 09-December 08

Re: Trouble calling functions

Posted 10 December 2008 - 09:21 PM

It seems im having problems with the same kind of code. here is what i have im just having problems with the way it outputs it seems to combine them all into one when I append more than one name and address. Not really sure what im doing wrong.

//Specification: Append and display records in a address database 
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void menu(void);
void writeData(void);
void readData(void);
string * split(string, char);

const char FileName[] = "c:/TestAddress.txt";
string database;
ifstream myAddress(FileName);

int main () {
		menu();
		return 0;
} //end main

void menu(void) {	 
	char choice = 'S';	
	while (choice!='E') {		  
		cout << "What would you like to do with these records: " << endl << endl;		
		cout << "(A)ppend records, (S)how records, or (E)xit"  << endl;		  
		cin >> choice;		 
		cin.ignore(1, '\n');				
		if (choice=='A') {			 
			writeData();		  } 
		else if (choice=='S') {			  
			readData();		  } 
		else if (choice!='E') {			  
			cout << "'" << choice << "' is not an option."  << endl;		
		}	 
	}
}
void writeData(void){
//Write the Address Info to a file
   char choice = 'Y';
   string name = "";
   string street = "";
   string city = "";
   string state = "";
   int zipCode = 0;
   ofstream outMyStream (FileName, ios::app);
	
		do {
			cout << "\nEnter The Name: ";
			getline(cin, name);
			cout << "\nEnter The Street: ";
			getline(cin, street);
			cout << "\nEnter The City: ";
			getline(cin, city);
			cout << "\nEnter The State: ";
			getline(cin, state);
			cout << "\nEnter The Code: ";
			cin >> zipCode;

			//write to the file
			outMyStream << name << "," << street << "," << city << "," << state << "," << zipCode;

			cout << "\nEnter another Record? (Y/N) "; 
			cin >> choice;
			//discard any newline character in keyboard buffer
			cin.ignore(1, '\n');
   }
	while (choice == 'Y');
		outMyStream.close();  
}//end write data

void readData(void){
//read data from a file
   ifstream inMyStream (FileName);

   //read the first record
   string lineBuffer;


   while (!inMyStream.eof() ){
	  getline (inMyStream, lineBuffer, '\n');
	  //divide the line into seperate fields
	  string *theFields = split(lineBuffer, ',');
	  cout << "Name...... " << theFields[0] << endl;
	  cout << "Street.... " << theFields[1] << endl;
	  cout << "City...... " << theFields[2] << endl;
	  cout << "State..... " << theFields[3] << endl;
	  cout << "Zip code.. " << theFields[4] << endl;
}

inMyStream.close();
}//end read data

string * split(string theLine, char theDeliminator){
		//Break the line into fields and save the fields to an array.
		//Each field will occupy one element in a character array.
		//theLine is a string with fields separated with theDeliminator character.
		//Assumes the last field in terminated with a newline.
		//Useage: string *theFields = split(lineBuffer, ',');
		//determine how many splits there will be so we can size our array

		int splitCount = 0;
		for(unsigned int i = 0; i < theLine.size(); i++){
				if (theLine[i] == ',')
				splitCount++;
		}
		splitCount++; 
	   //add one more to the count because there is not an ending comma

		//create an array to hold the fields
		string* theFieldArray;
		theFieldArray = new string[splitCount];

		//split the string into seperate fields
		string theField = "";
		int commaCount = 0;

			  for( unsigned int i = 0; i < theLine.size(); i++ ){
			  //read each character and look the deliminator
			   if (theLine[i] != theDeliminator) {
					   theField += theLine[i]; //build the field
			   }
			  else { //the deliminator was hit so save to the field to the array
					   theFieldArray[commaCount] = theField;
					   //save the field to the array
					   theField = "";
					   commaCount++;
			  }
		}
		theFieldArray[commaCount] = theField; 
		 //the last field is not marked with a comma...

		return theFieldArray;
} //end split  



Im supposed to be able to enter atleast two fields where its supposed too look like this

The file output should appear similar to:

Show Records
__________________________________________
Record #1
Name...........John Smith
Street..........902 Union Ave
City.............Any Town
State...........TX
Zip Code......78552
__________________________________________
Record #2
Name...........Eric Jones
Street..........345 State Way
City.............Fresno
State...........CA
Zip Code.......93432
__________________________________________

(A)ppend Records, (S)how Records, (E)xit



and
The program input should appear similar to this:


Append Records

Name..........John Smith
Street.........902 Union Ave
City............Any Town
State...........TX
Zip Code......78552

"Enter another Record? (Y/N) "

The file structure look like this:

John Smith, 902 Union Ave, Any Town, TX, 79552
Eric Jones, 345 State Way, Fresno, CA, 93432
...

But it just putts the second entrys name and puts it on the first zipcode.
Im not really sure what Its doing.

This post has been edited by sleeplessgamer: 10 December 2008 - 10:02 PM

Was This Post Helpful? 0
  • +
  • -

#5 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Trouble calling functions

Posted 10 December 2008 - 09:24 PM

@sleeplessgamer
1 - Please edit your posting to wrap your code in code tags
:code:
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Trouble calling functions

Posted 10 December 2008 - 10:58 PM

You have 2 errors related to each other. The first is you do not add a comma after the zipCode string. This causes the name field of the next record, if it exists, to be output with the preceding zipCode. All the records you add are added to the end of the preceding record, resulting in one long line.
//write to the file
outMyStream << name << "," << street << "," << city << "," << state << "," << zipCode;


The second error depends on the first. You read the file using
while (!inMyStream.eof() ){
	 getline (inMyStream, lineBuffer, '\n');
...


Since you create the file as one long line, only the first record gets output.
Change
outMyStream << name << "," << street << "," << city << "," << state << "," << zipCode;


to
outMyStream << name << "," << street << "," << city << "," << state << "," << zipCode << endl;


This post has been edited by n8wxs: 10 December 2008 - 11:00 PM

Was This Post Helpful? 0
  • +
  • -

#7 gamejust4u   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 214
  • Joined: 19-November 08

Re: Trouble calling functions

Posted 11 December 2008 - 10:53 AM

Also for your write loop- make sure you put both Y and y because not everyone puts capital letters.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1