Thread: ifstream with getline?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look carefully at the error message: "cannot convert parameter 3 from 'char [2]' to 'char'".

    Basically, you are supplying a string literal where you are supposed to supply a char, i.e., you should write '\n' instead of "\n".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    Ah ha! you are genius I love you! I was about to have serious words with my laptop screen

    SO just so I know for the future " " means? and ' ' means??
    whats the difference between the 2?

    thanks so much for your help

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    10
    hmm however now my find_free function seems to have stopped working properly, as the input function is having i returned as -1 all the time and saying all records are full :S odd
    seems to be ok?
    Any ideas why the load function would do that?
    Code:
    #include "stdafx.h"
    #include "iostream.h"
    #include "fstream.h"
    #include "stdlib.h" // includes standard libary header for clear screen function
    #include "string.h"
    struct record {                  /*struct called record*/
    			char name[30];
    	        char street[50];
                char streetii[50];
                char postcode[10];
    			};
    
    record entry[100];       /* delares variable entry with max 100 records*/
    
    //entry(input);
    void main(),menu(),options(int choice);
    void newrecord(),deleterecord(),findrecord(),displayall(),exit();
    void dofirst(), load(), input(),save(int i); 
    int find_free();
    
    void main ()
    {
    dofirst();
    menu();
    }
    void menu()
    {
         int choice; //declares the choice variable,
         do
         {
              cout << "Which option would you like? (1-5) \n\n";
              cout << "1: New Entry In Database\n";
              cout << "2: Delete Entry In Database\n";
              cout << "3: Find and Display A Single Record\n";
              cout << "4: Display All Records\n";
              cout << "5: Exit Database\n";
    		  cout << "\nPlease Enter Your Choice (1-5): ";//Display some options on screen.
                 
    		  cin  >> choice;
         
    		  options(choice); //opens options function + sends choice to options function.
    
         }while(choice!=5);//The program will continue to run until option 5 (exit) is chosen.
    }//end function
    
    void options(int choice)
    {
         switch (choice)//Goes Through options
         {
              case 1:  //If number 1 is picked, it will go to the function below same each case. 
                   newrecord();
                   break;
              case 2:
                   deleterecord();
                   break;
              case 3:
                   findrecord();
                   break;
              case 4:
                   displayall();
                   break;
              case 5:
                   exit();
                   break;
              default://THEY DIDNT READ THE MENU, Make Them Choose Correct choice
                  system("cls"); // Clear Screen
    			  cout << "\a\a\a\nYOU HAVE ENTERED AN INVALID CHOICE YOU FOOL!\n"
    				    << "Please enter a number from 1 to 5\n\n";
    			  // shown if an incorrect choice is shown.
    			  break;
         }
    }//end function
    
    //functions
    
    
    void dofirst() // this function prepares the data.dat file for first use
    {
    int i; // variable i declared
    	for ( i=0; i<100; i++ ) // loop until i=99
    	{
    		cout << "Preparing Record Line " << i << "\n" << endl; // shows progress of file prep
    		system("cls"); // clear screen
    		strcpy(entry[i].name, "*"); // make entry[i].name in struct/array = *
    	}
    load();
    } //end function
    
    void newrecord()
    {	
    	input(); // calls the input function 
    	system("cls"); // clear screen
    }//end function
    
    void input()
    {
    
    int i;	
    i=find_free();
    
    	if (i==-1) // if find_free fuction returns -1 do below
    	{	
    		system("cls");	
    		cout << "THE DATABASE IS FULL PLEASE DELETE SOME RECORDS\n\n";
    	}
    	else 
    	{
    	system("cls"); //clear screen
    	
    	cout <<"Enter Name:" << endl;
    	gets((entry[i].name));
    	cout <<"Enter House number and street address: " << endl;
    	gets((entry[i].street)); 
    	cout <<"Enter Town/City: " << endl;
    	gets((entry[i].streetii));
    	cout <<"Enter Post Code:" << endl; 
    	gets((entry[i].postcode));
    	save(i); //calls save
    	}
    }//end function
    
    int find_free()
    {
    int i;
    	for ( i=0; i<100; i++) //loops until i is = 99
    	{
    	if (!strcmp ((entry[i].name), "*")) //if String * is the same as, entry.name of number i return true
    	return i; // if strcmp statement is true return i to the function that called findfree()
    	}
    return -1; //if the above for statement finds nothing then return -1 to the calling funtion. 
    }//end function
    
    void exit()
    {
    	system("cls");
    	cout << "\nThankyou For Using Jays' Database. Goodbye :)\n\n";
    }//end function
     
    
    void load()
    {
    	int i;
    	ifstream fin("data.dat");
    	for (i=0; i<100; i++)
    	{
    		fin.getline((entry[i].name), 30, '\xba');
    		fin.getline((entry[i].street), 50, '\xba');
    		fin.getline((entry[i].streetii), 50, '\xba');
    		fin.getline((entry[i].postcode), 10, '\xba');
    	}
    }//end function
    
    void save(int i)
    {
    	ofstream fout("data.dat", ios::app); // opens file data.dat for output to the program in append mode.
    	fout << (entry[i].name) << "\xba"; // puts name variable of number [i] into a file then starts new line
    	fout << (entry[i].street) << "\xba";
    	fout << (entry[i].streetii) << "\xba";
    	fout << (entry[i].postcode) << "\xba\n";
    	fout.close();
    
    }//end function
    
    void findrecord()
    {
    //	int i;
    	system("cls");
    }//end function
    
    void displayall()
    {
    	system("cls");
    	cout << "free function\n\n";
    }//end function
    void deleterecord()
    {
    	system("cls");
    	cout << "free function 2\n\n";
    }//end function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. problem with string class, ifstream, and getline
    By deathbob in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2005, 11:20 AM
  3. ifstream getline
    By jimboob in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2004, 10:37 AM
  4. getline() and ifstream
    By quizkiwi in forum C++ Programming
    Replies: 5
    Last Post: 08-31-2004, 01:44 PM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM