string line;
fstream search;
short loop = 0;
const int SIZE = 6;
string weight[SIZE];
int index[6] = { 1, 2, 3, 4, 5 };
int i;
int choice;
string weightChange;
search.open("name.txt"); //Opens the text file in which the user details are stored
if (search.is_open())
{
cout << endl;
while (getline(search, line)) { //While the program searches for the lines in the text file
if (line.find("Current Weight(KG): ") != string::npos) { //If the string "Name" isnt the last word on the line
weight[loop] = line; //Assings the strings read from the text file to the array called weight
cout << index[loop] << ". " << weight[loop] << endl; //Outputs the index array which loops through the numbers in the array and outputs the weight variable which loops through the strings in the array
loop++; //Iterates the loop
}
}
cout << "Which index would you like to change?" << endl;
cout << "1, 2, 3, 4 or 5" << endl;
cin >> choice;
cout << "Weight being changed is " << weight[choice - 1] << endl;
switch (choice) //Uses switch to allow the user to enter a number from the menu and then performs that function based on the user's input
{
case 1: //If the user enters choice 1, then the first weight index will be changed
cout << "What would you like to change the user's weight to?" << endl;
cin >> weightChange;
weightChange = weight[1];
search >> weight[1];
cout << endl;
break;
}
}
因此,我有一个程序可以使用循环从文本文件读取用户当前的重量。然后,我必须获得用户输入他们想要更改的体重的信息。当输入他们想改变其体重的用户时,询问他们他们想改变的体重。该程序运行正常,但是当他们输入新的权重时,它不会更改txt文件中的任何内容。