이 프로그램의 기능은 다음과 같습니다. 사용자가 이것을 실행하면 3 개의 입력을 요구받습니다. 날짜 (DDMMYY) 신체 훈련 (예 : 가슴) 및 시간 (예 : 100) 조건에 대한 사용자 입력에 따라 다릅니다. 사용자는 새로운 데이터를 저장할 수 있습니다. 또는 저장된 데이터를 볼 수 있습니다.csv 파일 읽기 및 쓰기
사용자가 데이터를 저장하면 데이터가 정상적으로 저장됩니다. 그들이이 데이터를 볼 때 그 데이터를 보여줍니다. 이것이 올바른 의도 된 기능입니다.
프로그램을 다시 시작하고 다른 정보 집합을 입력 할 때 문제가 발생합니다. 이 정보 집합은 마지막 세트를 덮어 씁니다. 따라서 최신 데이터 입력 만 표시됩니다.
1. 프로그램을 다시 시작하고 새 데이터를 입력하면 어떻게 데이터를 덮어 쓰지 않고 파일에 추가합니까? 2. 내 다른 질문은 어떻게 프로그램의 동일한 실행에서 기능을 반복 할 수 있습니까? 추기 꽤 때문이다 은 내가 파일을 열기
workoutlogger.cpp
#include "workoutlogger.h"
#include <iostream>
#include <fstream>
#include <string>
#include "mainclient.h"
using namespace std;
workoutlogger::workoutlogger()
{
int choices;
cout << " (1) Do you want to log a new workout\n (2) Track previous workouts?\n Enter the number of your choice\n";
cin >> choices;
switch (choices) {
case 1:
log();
break;
case 2:
viewinfo();
break;
default:
cout << "invalid option..";
workoutlogger();
}
}
workoutlogger::~workoutlogger()
{
}
int workoutlogger::log()
{
ofstream theFile("workinfo.txt");
cout << "enter date (DDMMYY), bodypart trained (eg. Chest), time trained (mins)" << endl;
cout << "press ctrl + z to quit\n";
int date;
string bodypart;
int minutes;
while (cin >> date >> bodypart >> minutes)
{
theFile << date << ' ' << bodypart << ' ' << minutes << endl;
}
system("pause");
return 0;
}
int workoutlogger::viewinfo() {
ifstream theFile("workinfo.txt");
int date;
string bodypart;
int minutes;
while (theFile >> date >> bodypart >> minutes) { //stores infomation in these variables
//file pointer starts at first piece of info, then onto next info to store in variables
cout << date << ", " << bodypart << ", " << minutes << endl;
}
system("pause");
return 0;
}
workoutlogger.h
#pragma once
class workoutlogger
{
public:
workoutlogger();
~workoutlogger();
int viewinfo();
int log();
};
파일은 공간이 아닌 쉼표로 구분되며, 당신은 파일에 추가하는 방법을 이해하는 fstream 설명서를 확인해야합니다. 'main' 함수를 보지 않거나 다른 함수를 호출한다고해서 두번째 질문을 짐작할 수 있습니다. 짧은 대답, 함수를 다시 호출하십시오. –
이데올로기 적 포인트 :이 클래스에는 상태가 없습니다 (데이터 멤버가 없음). 자신에게 물어보십시오, "왜 그것이 수업입니까?" – user4581301