Trouble making a table (repeats itself)

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi guys, I need help with my code. Ive got it so it gets input from an external .txt file and spits out the table with the input on the given intervals. however, with each new input the program makes a whole new table. I need to change something so it just
adds the input to the existing table, but I honestly have no idea how I would do that. Thanks in advance for the help!


<div style="color:Black;background-color:White; <pre>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#include <span style="color:#A31515; "medicationTimes.h"

<span style="color:Blue; using <span style="color:Blue; namespace std;


<span style="color:Blue; int main()
{
ifstream inFile, inFile2;
ofstream outFile;
inFile.open(<span style="color:#A31515; "meds_input.txt");
inFile2.open(<span style="color:#A31515; "Han_Seul_Choi_output.txt");
outFile.open(<span style="color:#A31515; "Han_Seul_Choi_output.txt");

<span style="color:Blue; int medTime = 1;
<span style="color:Blue; int hour = 1;
<span style="color:Blue; int time = 0;
string med;
string food=<span style="color:#A31515; " ";
string take;
<span style="color:Blue; const <span style="color:Blue; char NWLN = n;
<span style="color:Blue; int nummed;
<span style="color:Blue; char answer;

menu();

cin >> answer;<span style="color:Green; //again the user is asked to initiate the program

<span style="color:Blue; if (answer == t || answer == T)
{

medication_times();
cout<<n<<<span style="color:#A31515; "Press M to go back to the menu or Q to quit the program."<<endl;
cin>>answer;
<span style="color:Blue; if (answer == m || answer == M)
{
<span style="color:Blue; return main();
}
<span style="color:Blue; if (answer == q || answer == Q)
{
cout<< <span style="color:#A31515; "Exiting Program."<<endl;
system (<span style="color:#A31515; "PAUSE");
<span style="color:Blue; return 0;
}

}

<span style="color:Blue; else <span style="color:Blue; if (answer == a || answer == A)

{
cout<<<span style="color:#A31515; "Please enter the medication in the following format: (medication_name) (frequency) (conditions for pill consumption); for only one medication"<<endl;

cin>>med>>medTime;
getline(cin,food);

outFile.ate;
outFile<<med<<medTime<<food<<endl;

nummed++;

<span style="color:Blue; return main();
}

<span style="color:Blue; else <span style="color:Blue; if (answer == q || answer == Q)

{
cout<< <span style="color:#A31515; "Exiting Program."<<endl;
system (<span style="color:#A31515; "PAUSE");
<span style="color:Blue; return 0;
}

<span style="color:Blue; else
{
cout<<<span style="color:#A31515; "Invalid input, try again."<<endl<<endl;
<span style="color:Blue; return main();
}
}

<span style="color:Blue; void menu()
{
cout << <span style="color:#A31515; "Press T to show the medicine schedule." << endl;
cout << <span style="color:#A31515; "Press A to add to the medicine schedule." << endl;
cout << <span style="color:#A31515; "Press Q to exit the program." << endl;
}

<span style="color:Blue; void medication_times ()
{
ifstream inFile, inFile2;
ofstream outFile;
inFile.open(<span style="color:#A31515; "meds_input.txt");
inFile2.open(<span style="color:#A31515; "Han_Seul_Choi_output.txt");
outFile.open(<span style="color:#A31515; "Han_Seul_Choi_output.txt");

<span style="color:Blue; int medTime = 1;
<span style="color:Blue; int hour = 1;
<span style="color:Blue; int time = 0;
string med;
string food=<span style="color:#A31515; " ";
string take;
<span style="color:Blue; const <span style="color:Blue; char NWLN = n;
<span style="color:Blue; int nummed;

inFile>>nummed;
getline(inFile, food);
outFile<<endl<<endl<<<span style="color:#A31515; "There are currently "<<nummed<<<span style="color:#A31515; " medications in the schedule.n"<<endl;
cout<<<span style="color:#A31515; "There are currently "<<nummed<<<span style="color:#A31515; " medications in the schedule.n"<<endl;

inFile>>med>>medTime;
getline(inFile, food);


<span style="color:Blue; for (<span style="color:Blue; int x = 0; x<=nummed; x++)
{

hour = 1;

<span style="color:Blue; do
{

time = hour % medTime;

<span style="color:Blue; if (hour<10 && time !=0)
{
outFile<<<span style="color:#A31515; "0"<<hour<<<span style="color:#A31515; " : "<<<span style="color:#A31515; "00"<<<span style="color:#A31515; " "<<n;
cout<<<span style="color:#A31515; "0"<<hour<<<span style="color:#A31515; " : "<<<span style="color:#A31515; "00"<<<span style="color:#A31515; " "<<n;
}

<span style="color:Blue; if (hour>=10 && time !=0)
{
outFile<<hour<<<span style="color:#A31515; " : "<<<span style="color:#A31515; "00"<<<span style="color:#A31515; " "<<n;
cout<<hour<<<span style="color:#A31515; " : "<<<span style="color:#A31515; "00"<<<span style="color:#A31515; " "<<n;
}

<span style="color:Blue; else <span style="color:Blue; if (hour<10 && time ==0)
{
outFile<<<span style="color:#A31515; "0"<<hour<<<span style="color:#A31515; " : "<<<span style="color:#A31515; "00"<<<span style="color:#A31515; " "<<med<<<span style="color:#A31515; " ";
cout<<<span style="color:#A31515; "0"<<hour<<<span style="color:#A31515; " : "<<<span style="color:#A31515; "00"<<<span style="color:#A31515; " "<<med<<<span style="color:#A31515; " ";

<span style="color:Blue; if(!food.empty())
{
outFile<<food<<endl;
cout<<food<<endl;
}
<span style="color:Blue; if(food.empty())
{
outFile<<endl;
cout<<endl;
}

}
<span style="color:Blue; else <span style="color:Blue; if (hour>=10 && time ==0)
{
outFile<<hour<<<span style="color:#A31515; " : "<<<span style="color:#A31515; "00"<<<span style="color:#A31515; " "<<med<<<span style="color:#A31515; " ";
cout<<hour<<<span style="color:#A31515; " : "<<<span style="color:#A31515; "00"<<<span style="color:#A31515; " "<<med<<<span style="color:#A31515; " ";

<span style="color:Blue; if(!food.empty())
{
outFile<<food<<endl;
cout<<food<<endl;
}
<span style="color:Blue; if(food.empty())
{
outFile<<endl;
cout<<endl;
}

}
hour++;

}<span style="color:Blue; while(hour<=24);

outFile.seekp(0);
inFile2.seekg(0);
inFile>>med>>medTime;
getline(inFile, food);

}


}

[/code]
<br/>
<br/>


View the full article
 
Back
Top