So my program currently will read the first number in the text file, but if I accept, or replace the number. It will start randomly putting 0's in and it will shift around my file ex.
If my first number is 3 and I want to replace it with 2 I hit 'r'.
It then will replace that value, and it is suppose to get the next number on the next line, and ask to change that, but it gets all jumbled. Sorry for the long explanation but I wanted to be thorough.
#include <iostream>
#include <string>
#include <fstream>
usingnamespace std;
class myClass{
public:
myClass (string fname) //constructor
{
filename = fname;
selection = 0;
input = 0;
number = 0.0;
anothernumber = 0.0;
replacement = 0.0;
newfilename = "";
//choice = '';
answer = "";
}
string returnNewfile()
{
return newfilename;
}
string returnAnswer()
{
return answer;
}
int returnSelection()
{
return selection;
}
int returnInput()
{
return input;
}
double returnOtherNumber()
{
return anothernumber;
}
double returnNumber()
{
return number;
}
double returnReplace()
{
return replacement;
}
char returnChoice() //choice
{
return choice;
}
void Select()
{
cout << "Please enter 0 for read or 1 for write mode" << endl;
cin >> selection;
if (selection == 0)
{
ifstream filename ("example.txt");
if (filename.is_open())
{
while(getline(filename, line)) //Goes through the file and gets each line individually
{ //and prints out whats in it line by line.
cout << '\n' <<"This is what is in the file: " << line << endl;
}
filename.close();
}
}
elseif (selection == 1)
{
fstream filename("example.txt");
if (filename.is_open())
{
cout << "How many numbers do you want to enter? " << endl;
cin >> input;
for (int i = 1; i <= input; i++) //This will check how many numbers you want to enter
{ //then will keep asking for the numbers until it reaches what you wanted.
getline(filename, line);
cout << '\n' << "This is what is in the file: " << line << endl;
cout << '\n' << "Hit a to accept value, r to replace, d to delete it, or type x to accept rest of the values. " << endl;
cin >> choice;
if (choice == 'a' || choice == 'A')
{
filename << line << endl;
}
if (choice == 'r' || choice == 'R')
{
cout << "What number do you want to replace it with? " << endl;
cin >> number;
filename << number << " " << line << endl;
cout << "The number has been replaced." << endl;
}
if (choice == 'd' || choice == 'D')
{
number--;
cout << "Number deleted." << endl;
}
if (choice == 'x' || choice == 'X')
{
//exit();
}
cout <<'\n' << "Would you like to enter a new number?" << endl;
cin >> answer;
if (answer == "yes" || answer == "YES" || answer == "Yes")
{
cout << '\n' << "Please enter the number." << endl;
cin >> anothernumber;
filename << line << "";
filename << anothernumber << endl;
}
}// end of for loop
cout << "Would you like to save the changes of the file to a new one?" << endl;
cin >> newfilename;
if(newfilename == "yes" || newfilename == "Yes" || newfilename == "YES")
{
string thisfilenow;
cout << "Please enter new file name." << endl;
cin >> thisfilenow; //this will save the new data into another file leaving the old file intact.
filename << thisfilenow;
}
}// end of filename.open if statement
filename.close();
}// end of else if selectio == 1
}//End of void select
private:
string filename;
string line;
string answer;
string newfilename;
int selection;
double myarray[10][10];
int input;
double number, replacement,anothernumber;
char choice;
};
int main()
{
string fname = "example.txt";
myClass co(fname);
co.Select();
}
When you did getline(filename, line); on line 89, the internal file pointer of the file you are working with moves to the next line. So when you try to replace 160 with 125, you will actually be replacing the value on the next line. You need to make use of a peekline (if there is such a thing) kind of method so that you can instead "peek" at the contents of the next line and choose to replace it if need be
Thank you Smac89, I will try that when I get home and post if I have further issues. Does anything else look funky? It's better when another set of eyes looks at your code.