3 Replies - 2130 Views - Last Post: 01 September 2019 - 03:30 PM Rate Topic: -----

#1 Vic45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 16

Reading a variable value from text file and incrementing it

Posted 15 August 2019 - 10:53 AM

Hello, so I am trying to get more experience with c++ and am expanding on a challenge program in my c++ book. Right now I am trying to get a function to work that if picked in a switch statement writes some user entered information to a text file. If the second case is picked it appends to the first file. This will be able to be appending infinitely. The last switch case just displays the current text file in command line interface. - I want to add a counter so at the beginning of every entry it states which record it was. So like record #1 then #2 etc. - Unfortunately I cannot think of a way of doing this without reading the previous record # value from the text file.

To make myself clear, when the file gets opened for reading there is no way of getting the previous record # value. So when I try to increment it beyond 2 it never gets to 3. - I hope that makes sense.

Basically I want it to go #1, #2, #3 etc. and I can't think of a way other than somehow reading the current record# value from the actual file. - I also have no idea how to get the current record # read from the file.

So the parts with the count variable are what I am working with for this problem.

void Personal::setInfo(int a)
{
	int count = 0;
      ofstream myFile;
      if (a==1) {
      myFile.open("myInfo.txt");
      count = 0;
	  }
      else
      {
	  myFile.open("myInfo.txt", ios_base::app);
	  }
	  
      if (myFile.is_open()) {
		  ++count;
		  myFile << "Record #: " << count << endl;
        myFile << "Name: " << getName() << endl;
        myFile << "Address: " << getAddress() << endl;
        myFile << "Age: " << getAge() << endl;
        myFile << "Phone Number: " << getPhone() << endl;
        myFile << endl;
        myFile.close();
      } else {
        cout << "Unable to open file";
      }
  
	}



Here is my full code in case it is needed:

#include <iostream>

#include <string>

#include <fstream>

#include <sstream>

#include <cassert>

using namespace std;


class Personal {
  public:
  void getInfo();
    void setInfo(int);
  string getName() {
    return name;
  }
  string getAddress() {
    return address;
  }
  int getAge() {
    return age;
  }
  string getPhone() {

    return phoneNum;
  }
  private:
  string name;
  string address;
  int age;
  string phoneNum;

};

void Personal::getInfo() 
{	
	 cout << "Enter your name" << endl;
      cin.ignore();
      getline(cin, name);
   
      cout << "Enter your Address" << endl;
      cin.clear();
      getline(cin, address);
 
      cout << "Enter your age" << endl;
      cin >> age;
  
      cout << "Enter your Phone #" << endl;
      cin.ignore();
      getline(cin, phoneNum);
}

void Personal::setInfo(int a)
{
	int count = 0;
      ofstream myFile;
      if (a==1) {
      myFile.open("myInfo.txt");
      count = 0;
	  }
      else
      {
	  myFile.open("myInfo.txt", ios_base::app);
	  }
	  
      if (myFile.is_open()) {
		  ++count;
		  myFile << "Record #: " << count << endl;
        myFile << "Name: " << getName() << endl;
        myFile << "Address: " << getAddress() << endl;
        myFile << "Age: " << getAge() << endl;
        myFile << "Phone Number: " << getPhone() << endl;
        myFile << endl;
        myFile.close();
      } else {
        cout << "Unable to open file";
      }
  
	}
//~ void getInfo(Personal&);
int main() {
  Personal info;

  //~ string userName;
  //~ string userAddress;
  //~ int userAge;
  //~ string userPhone;
  char cont = 'Y';
  do {
  int choice = 1;
  cout << "Please choose an option: " << endl << "Enter 1 to create new file. Enter 2 to append the file. Enter 3 to read full file" << endl;
  cin >> choice;
  switch (choice) {

  case 1:
    {
     
		  info.getInfo();
		  info.setInfo(choice);
  
    break;
	}
  case 2:
    {
	  info.getInfo();
      info.setInfo(choice);
	break;
	}
  case 3:
    {
    string STRING;
	ifstream infile;
	infile.open ("myInfo.txt");
        while(getline(infile,STRING)) // To get you all the lines.
        {
	       // Saves the line in STRING.
	        cout<<STRING << endl; // Prints our STRING.
        }
	infile.close();
	//~ system ("pause");
break;
  }

  } 
    cout << "To quit type Q or q To continue type Y or y" << endl;
  cin >> cont; 
	
  } while  ((cont != 'Q') && (cont != 'q'));
  return 0;
}



Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Reading a variable value from text file and incrementing it

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,179
  • Joined: 12-June 08

Re: Reading a variable value from text file and incrementing it

Posted 15 August 2019 - 11:32 AM

That shouldn't be an issue. A text file is a pretty unaware data storage.. so at the start of hte program it's best to read all the data into a collection (say a list of 'record' (custom class you would write)), and be able to manipulate from there. Before it closes write all those back out again.
Was This Post Helpful? 0
  • +
  • -

#3 Vic45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 03-March 16

Re: Reading a variable value from text file and incrementing it

Posted 15 August 2019 - 03:01 PM

Thanks for the reply. I'm sorry that doesn't make sense to me. What I hear you saying is I need to make a count member variable/function in a class, assign it information, increment the counter, then write the new count variable to the text file. - But the program exits after I collect all the data and display it. I still think I need a way of storing that count variable for reuse the second or however many times it is run.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7056
  • View blog
  • Posts: 23,986
  • Joined: 05-May 12

Re: Reading a variable value from text file and incrementing it

Posted 01 September 2019 - 03:30 PM

None of this code is for the .NET Framework C++/CLI (CLI == Common Language Interface). This looks to be just standard C++, and the OP is thinking of CLI == Command Line Interface.

Moving the thread...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1