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
  #21  
Old 05-May-2005, 08:36
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
I have a doubt...

How ...
when having a file made by the user, how do i make a for that reads everyline and at the end tells me how many lines are in the file.

Example:

NAME LASTNAME AREACODE PHONENUMBER
1111 222222 333 4444444
5555 66666 777 8888888

and reads 'tils the newline that i says that i have.. in this case, two ..(is objects the word?)
  #22  
Old 05-May-2005, 08:54
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by marita
How ...
when having a file made by the user, how do i make a for that reads everyline and at the end tells me how many lines are in the file.
I don't suppose you can use a std::vector? Then you wouldn't need to.

You could open the file and read all the lines -- then you'll know. Then close the file, allocate some space, and reread the file.

CPP / C++ / C Code:
#include <iostream>
#include <fstream>

int main()
{
   std::ifstream file("file.txt");
   char line[80];
   int  count = 0;
   while ( file.getline(line, sizeof line) )
   {
      std::cout << line << std::endl;
      ++count;
   }
   std::cout << "count = " << count << std::endl;
}

/* my output
NAME LASTNAME AREACODE PHONENUMBER
1111 222222 333 4444444
5555 66666 777 8888888
count = 3
*/
  #23  
Old 05-May-2005, 09:04
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Thanks, i think that'll help.

Let's keep working it....
would you be a dear and give me a hand, (if you'll be here)
I think there's no one else reading my post...
  #24  
Old 05-May-2005, 09:19
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
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()
{
	ifstream file;

   ifstream file ("file.txt");
   char line[80];
   int  count = 0;
   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;
}


and the errors:
Code:
NewPhoneBook.cpp C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(41) : error C2065: 'ifstream' : undeclared identifier C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(41) : error C2146: syntax error : missing ';' before identifier 'file' C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(41) : error C2065: 'file' : undeclared identifier C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(43) : error C2146: syntax error : missing ';' before identifier 'file' C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(46) : error C2228: left of '.getline' must have class/struct/union type C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(46) : fatal error C1903: unable to recover from previous error(s); stopping compilation Error executing cl.exe. NewPhoneBook.obj - 6 error(s), 0 warning(s)
Last edited by LuciWiz : 05-May-2005 at 11:38. Reason: Please insert your C code between [c] & [/c] tags
  #25  
Old 05-May-2005, 09:40
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
There are generally three ways to go about using stuff from the std namespace.
  1. Grab the whole namespace. Generally acceptable for toy programs and homework, later on it's not a habit to keep.

    CPP / C++ / C Code:
    using namespace std;
    // ...
    cout << "something" << endl;
  2. Explicitly qualify each item from the std namespace. Sometimes this gets a little too wordy.

    CPP / C++ / C Code:
    std::cout << "something" << std::endl;
  3. Just grab the things you use.

    CPP / C++ / C Code:
    using std::cout;
    using std::endl;
    // ...
    cout << "something" << endl;
  #26  
Old 05-May-2005, 09:41
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
This is just something else to look at.

CPP / C++ / C Code:
#include <iostream>
#include <fstream>

struct Entry
{
   char first    [20];
   char last     [30];
   char areacode [6];
   char number   [10];
};

class PhoneBook
{
   Entry *entry;
public:
   PhoneBook(int size = 0) : entry(new Entry[size])
   {
   }
   ~PhoneBook()
   {
      delete[] entry;
   }
   bool Add(int index, std::ifstream &ifs)
   {
      return ifs >> entry[index].first
                 >> entry[index].last
                 >> entry[index].areacode
                 >> entry[index].number;
   }
   void Show(int index, std::ostream &o)
   {
      o << "index    = " << index                 << '\n'
        << "first    = " << entry[index].first    << '\n'
        << "last     = " << entry[index].last     << '\n'
        << "areacode = " << entry[index].areacode << '\n'
        << "number   = " << entry[index].number   << '\n'
        << std::endl;
   }
};

int main()
{
   static const char filename[] = "phonebook.txt";

   // Count the number of entries (lines) in the file.
   std::ifstream file(filename);
   char line[80];
   int  count = 0;
   while ( file.getline(line, sizeof line) )
   {
      ++count;
   }
   file.close();

   // Construct the phone book.
   PhoneBook phonebook(count);

   // Populate the phone book.
   file.open(filename);
   for ( int i = 0; i < count; ++i )
   {
      if ( phonebook.Add(i, file) )
      {
         phonebook.Show(i, std::cout);
      }
   }
}

/* my output
index    = 0
first    = Marie
last     = Nieves
areacode = (555)
number   = 555-5555

index    = 1
first    = Juanita
last     = Rivera
areacode = (777)
number   = 777-7777

index    = 2
first    = Pepito
last     = Conde
areacode = (88
number   = 888-8888

index    = 3
first    = Panchita
last     = Cruz
areacode = (999)
number   = 999-9999
*/
  #27  
Old 05-May-2005, 09:46
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
i took off the std's of your code and added just #include <iostream.h> and i still have 5 errors...

i can do that right? 'cause we have been taught to use jsut cout not std::cout...
Let me know...

Code:
NewPhoneBook.cpp C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(49) : error C2065: 'fstream' : undeclared identifier C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(49) : error C2146: syntax error : missing ';' before identifier 'file' C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(49) : error C2065: 'file' : undeclared identifier C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(50) : error C2228: left of '.getline' must have class/struct/union type C:\Documents and Settings\ee181\Desktop\NewPhoneBook.cpp(50) : fatal error C1903: unable to recover from previous error(s); stopping compilation Error executing cl.exe. NewPhoneBook.obj - 5 error(s), 0 warning(s)


Thanks,
  #28  
Old 05-May-2005, 09:47
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by marita
i took off the std's of your code and added just #include <iostream.h> and i still have 5 errors...
Don't go backwards in time! The standard made <iostream.h> obsolete in 1998.
  #29  
Old 05-May-2005, 09:51
marita marita is offline
New Member
 
Join Date: Apr 2005
Posts: 25
marita is on a distinguished road
Quote:
Originally Posted by Dave Sinkula
Don't go backwards in time! The standard made <iostream.h> obsolete in 1998.

REALLY?!

*haha*
Well, then my professors are backwards, because they have always told us to do that...


by the way, the last code you gave me, is not giving me that output.... ??:
  #30  
Old 05-May-2005, 09:59
Dave Sinkula Dave Sinkula is offline
Member
 
Join Date: Apr 2005
Posts: 199
Dave Sinkula will become famous soon enough
Quote:
Originally Posted by marita
REALLY?!

*haha*
Well, then my professors are backwards, because they have always told us to do that...
All too many are.

http://groups-beta.google.com/group/...925ca0462d2081

Quote:
Originally Posted by marita
by the way, the last code you gave me, is not giving me that output.... ??:
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
 
 

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 15:40.


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