GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #31  
Old 05-May-2005, 10:02
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Quote:
Originally Posted by Dave Sinkula
All too many are.

http://groups-beta.google.com/group/comp.std.c++/msg/35925ca0462d2081


This is the input file "phonebook.txt" I used (I thought I'd grabbed it from some earlier post).

Code:
Marie Nieves (555) 555-5555 Juanita Rivera (777) 777-7777 Pepito Conde (88 888-8888 Panchita Cruz (999) 999-9999

Yeah i used that one too...
What might be the problem... i wonder...
  #32  
Old 05-May-2005, 10:08
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
WHy is this giving me those errors?
How can i fix my code?

CPP / C++ / C Code:
// Mar-Iam Nieves Torres
// Cecs 2223-22: Computer Programming Lab II
// Prof. Paul Nieves Torres
// Laboratory #3: Phonebook
// May 5, 2005

#include <iostream>
#include <fstream>
#include <string>


class PhoneBook
{
public:
	PhoneBook();
	fileCount();


private:
	char name;
	char lastname;
	int areacode;
	int phone;
	
};



/* abro el archivo y cuento
fp.open

for (i=0; i <  cantidad de objetos que conte cuando abri el archivo  ; i++)
f= a.getData(fp)
arreglo [i] = f;
*/



PhoneBook::fileCount()
{
   
   char line[80];
   int  count = 0;
   

   fstream file ("file.txt");
   while ( file.getline(line, sizeof line) )
   {
      cout << line << endl;
      ++count;
   }
   cout << "count = " << count << endl;
}


//FUNCION GETDATA

PhoneBook::getData()
{
	objeto a;

	fp >> a.name // lee el archivo, lo guardo en el campo y le doy rewind

		return a;
}


int main()
{
	char option;
	int i=0;
	PhoneBook p;

	p.fileCount();




//Menu
	do
	{  
		cout<<"           Menu           "<<endl;                      //print menu
		cout << "What would you like to do? " << endl;
		cout<<"(1) Change Area Code"<<endl;                  //print option (1) to change the Area Code
		cout<<"(2) Change Phone Number"<<endl;               //print option (2) to change the Phone Number
		cout<<"(3) Change Area Code & Phone Number"<<endl; //print option (3) to change Area Code & Phone Number
		cout<<"(4) Print"<<endl;              //print option (4) for printing
		cout<<"(5) Salir"<<endl;              //print option (5) to exit
	
		cin >> option;


			switch (option)                        // begins switch structure
		{
			case 1:  
				cout << "Enter the name and last name of the person you would like to edit: ";
				cin >> fname >> lname;

				for (i=0; i < cant; i++)
				{
						for (int i =0; i<10; i++)
						data [i] = toupper (data[i]);

						if (strcmp (name, array [i].name)) && (strcmp (lastname, array [i].lastname))
						{
							array[i].()
						}
				}
				break;


			case 2:  
				cout << "Enter the name and last name of the person you would like to edit: ";
				cin >> fname >> lname;

				for (i=0; i < cant; i++)
				{
						for (int i =0; i<10; i++)
						data [i] = toupper (data[i]);

						if (strcmp (name, array [i].name)) && (strcmp (lastname, array [i].lastname))
						{
							array[i].[]
						}
				}
				break;


				case 3:  
				cout << "Enter the name and last name of the person you would like to edit: ";
				cin >> fname >> lname;

				for (i=0; i < cant; i++)
				{
						for (int i =0; i<10; i++)
						data [i] = toupper (data[i]);

						if (strcmp (name, array [i].name)) && (strcmp (lastname, array [i].lastname))
						{
							array[i].=
						}
				}
				break;


				case 4:  
					// Imprime el archivo

				break;


				case 5:  
					cout<<"Exiting...."<<endl;  //exits
			
				break;


				default:
				cout<<"Select and option from 1 -> 5"<< endl;
				break;
		}                                        //end swicth structure
	}while(operation !=5);    //end do/while structure

	/* Falta:
			- tengo que buscar donde esta el cursor
			- escribir en el archivo
			- darle delete a la memoria dinamica
			- cerrar el archivo
	*/

return 0;
}


errors:
Code:
NewPhoneBook.cpp C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(46) : error C2065: 'fstream' : undeclared identifier C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(46) : error C2146: syntax error : missing ';' before identifier 'file' C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(46) : error C2065: 'file' : undeclared identifier C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(47) : error C2228: left of '.getline' must have class/struct/union type C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(47) : fatal error C1903: unable to recover from previous error(s); stopping compilation Error executing cl.exe. NewPhoneBook.exe - 5 error(s), 0 warning(s)
Last edited by LuciWiz : 05-May-2005 at 11:43. Reason: Please insert your C code between [c] & [/c] tags
  #33  
Old 05-May-2005, 10:34
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
For starters, add these lines right after the #includes.

CPP / C++ / C Code:
using std::cout;
using std::endl;
using std::ifstream;

And then this was like this:

CPP / C++ / C Code:
ifstream file ("file.txt");

There are a lot of little odds and ends to clean up. For example,

CPP / C++ / C Code:
char name;
char lastname;
These are only single characters -- there is not enough room for an name with more than one letter.

There are undeclared items such as objeto.


That's why I was trying to get you started (well, I know you've been at this for a while so it's not really "started") with code that worked. Apparently I'm not familiar with some of the differences between BC55 and MSVC6. It looks like the MSVC6 build has an issue with closing/reopening the file (???).
  #34  
Old 05-May-2005, 10:42
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Okay, apparently the MSVC6 version needs me to clear the stream after closing the file. Adding this produced the expected results.

CPP / C++ / C Code:
file.close();
file.clear(); // added this
I'm sure there's more to it -- and possibly correct behavior that I'm not used to doing -- but I'm just learning this now.

[edit]Hmmm.
http://groups-beta.google.com/group/...b16e6517f1141e
Quote:
When you reach the end of the file your file is in an error state. Even closing the file and reopening does not change the error state. When a file is in an error state nothing works until you clear the error state. Change your code to the following

CPP / C++ / C Code:
ifstream fin;
li.readBorrowers(fin); //when this line is commented
fin.clear();    // clear error state
fin.close();

You might think this is terrible design (and I'd agree) but it is what the C++ standard says.

Actually the correct place for fin.clear() would probably be at the end of the readBorrowers function.
  #35  
Old 05-May-2005, 12:22
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
thisis what i have so far:

Code:
// May 5, 2005 #include <iostream> // libraries #include <fstream> #include <string> using std::cout; using std::endl; using std::cerr; using std::ifstream; using std::ofstream; using std::ios; class PhoneBook // class definition { public: PhoneBook(); // default constructor fileCount(); // function supposed to count each row in the file void operator[] (); // operator overloading for [] - changes area code void operator() (); // operator overloading for () - changes phone number void operator = (); // operator overloading for = - changes area code & phone number private: char name[15]; char lastname[15]; int areacode; int phone; }; /* abro el archivo y cuento fp.open for (i=0; i < cantidad de objetos que conte cuando abri el archivo ; i++) f= a.getData(fp) arreglo [i] = f; */ //FUNCION FILECOUNT PhoneBook::fileCount() { char line[80]; int count = 0; fstream file; ifstream file; file.open("phonebook.txt", ios::in); if(!file) cerr <<"File phonebook.txt could not be opened"<<endl; while ( file.getline(line, sizeof line) ) { cout << line << endl; ++count; } cout << "count = " << count << endl; array = new PhoneBook [count]; for (int j=0; j <count; j++) { object.getData(file); array[j]=object; } file.clear(); } //FUNCION GETDATA /*PhoneBook::getData() { objeto a; fp >> a.name // lee el archivo, lo guardo en el campo y le doy rewind return a; } */ void PhoneBook::operator ()() { int acode; areacode = acode; cout << "Enter the new area code: "; cin >> acode; } void PhoneBook::operator[]() { int ph; phone = ph; cout << "Enter the new phone number: "; cin >> ph; } void PhoneBook::operator = (); { int ph, acode; areacode = acode; phone = ph; cout << "Enter new area code & phone number: "; cin >> acode >> ph; void main() { char option; int i=0; char name[15], lastname[15]; PhoneBook p; p.fileCount(); PhoneBook * PhBook = NULL; PhBook = new PhoneBook[]; //Menu do { cout << "***************************************"; //print menu cout << "****************************************" << endl; cout<<" MENU "<<endl; cout << "**************************************"; cout << "******************************************"; cout << "What would you like to do? " << endl; cout<<"(1) Change Area Code"<<endl; //print option (1) to change the Area Code cout<<"(2) Change Phone Number"<<endl; //print option (2) to change the Phone Number cout<<"(3) Change Area Code & Phone Number"<<endl; //print option (3) to change Area Code & Phone Number cout<<"(4) Print"<<endl; //print option (4) for printing cout<<"(5) Exit"<<endl; //print option (5) to exit cin >>option; switch (option) // begins switch structure { case 1: cout << "Enter the name and last name of the person you would like to edit: "; cin >> name >> lastname; /* for (i=0; i < count; i++) { for (int i =0; i<10; i++) data [i] = toupper (data[i]); if (strcmp (name, array [i].name)) && (strcmp (lastname, array [i].lastname)) { array[i].() } } break; case 2: cout << "Enter the name and last name of the person you would like to edit: "; cin >> fname >> lname; for (i=0; i < cant; i++) { for (int i =0; i<10; i++) data [i] = toupper (data[i]); if (strcmp (name, array [i].name)) && (strcmp (lastname, array [i].lastname)) { array[i].[] } } break; case 3: cout << "Enter the name and last name of the person you would like to edit: "; cin >> fname >> lname; for (i=0; i < cant; i++) { for (int i =0; i<10; i++) data [i] = toupper (data[i]); if (strcmp (name, array [i].name)) && (strcmp (lastname, array [i].lastname)) { array[i].= } } break; case 4: // Imprime el archivo break; case 5: cout<<"Exiting...."<<endl; //exits break; default: */ cout<<"Select and option from 1 -> 5"<< endl; break; } //end swicth structure }while(option !=5); //end do/while structure } /* Falta: - tengo que buscar donde esta el cursor - escribir en el archivo - darle delete a la memoria dinamica - cerrar el archivo */

errors:
Code:
NewPhoneBook1.cpp E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(23) : error C2805: binary 'operator [' has too few parameters E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(25) : error C2805: binary 'operator =' has too few parameters E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(55) : error C2065: 'fstream' : undeclared identifier E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(55) : error C2146: syntax error : missing ';' before identifier 'file' E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(55) : error C2065: 'file' : undeclared identifier E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(68) : error C2065: 'array' : undeclared identifier E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(68) : error C2440: '=' : cannot convert from 'class PhoneBook *' to 'int' This conversion requires a reinterpret_cast, a C-style cast or function-style cast E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(71) : error C2065: 'object' : undeclared identifier E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(71) : error C2228: left of '.getData' must have class/struct/union type E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(72) : error C2109: subscript requires array or pointer type E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(72) : error C2106: '=' : left operand must be l-value E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(99) : error C2065: 'cin' : undeclared identifier E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(99) : warning C4552: '>>' : operator has no effect; expected operator with side-effect E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(109) : warning C4552: '>>' : operator has no effect; expected operator with side-effect E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(115) : error C2447: missing function header (old-style formal list?) E:\Cecs2223\Lab #3 - Phonebook\NewPhoneBook1.cpp(245) : fatal error C1004: unexpected end of file found Error executing cl.exe. NewPhoneBook1.obj - 14 error(s), 2 warning(s)
  #36  
Old 05-May-2005, 13:04
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
First 2 diagnostics, different compiler...
Code:
Error E2080 testpp.cpp 17: 'PhoneBook::operator []()' must be declared with one parameter Error E2080 testpp.cpp 19: 'PhoneBook::operator =()' must be declared with one parameter
The next one...
Code:
fstream file; ifstream file;
Which do you want file to be (ifstream)? Delete the other one that you are not using.

Keep at it.
  #37  
Old 05-May-2005, 13:35
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Quote:
Originally Posted by Dave Sinkula
First 2 diagnostics, different compiler...
Code:
Error E2080 testpp.cpp 17: 'PhoneBook::operator []()' must be declared with one parameter Error E2080 testpp.cpp 19: 'PhoneBook::operator =()' must be declared with one parameter


What do you means with this?
  #38  
Old 05-May-2005, 13:48
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
I read it just like it says, "'PhoneBook:perator []()' must be declared with one parameter".

CPP / C++ / C Code:
   void operator[] (/* missing parameter */);

http://www.parashift.com/c++-faq-lit....html#faq-13.5
  #39  
Old 05-May-2005, 14:03
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Quote:
Originally Posted by Dave Sinkula
I read it just like it says, "'PhoneBook:perator []()' must be declared with one parameter".

CPP / C++ / C Code:
   void operator[] (/* missing parameter */);

http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.5


yes i know i need some parameters there even tho the professor said we didn't... but i have no clue what parameter i should put there...
  #40  
Old 05-May-2005, 14:20
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Well if it's being used to change the areacode, and areacode is and int, my guess would be an int value that you will assign to the areacode. But if you are going to take user input within the overloaded operator (to fit "what your professor says"), then you would probably merely need to dummy it up.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:51.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.