program problem, please HELP!!! 
Author Message
 program problem, please HELP!!!

I am trying to make this program for my friend and just started to quickly set
it up. He is a trainer so I am making him a program to track his client's
results and data.
basically it is comprised of 2 structure type , each with a respective
counter designated as ctr1 and ctr2  to keep track of the record enterd and
what position is the next record.
I can save a new file no problem, the ctrs are first and then the structs are
saved underneath in the file. when I go back to open it to append the data or
add more data, the ctr that is read is completely wrong.
for example, i save one record for 1 structure and 0 for the other, so the
counters should read 2 for the 1 and 1 for the other to signify the next
starting point for the record. However, the ctrs that are read are -10 & 0.
please help me if you can.  the code is below. I write in DOS using turbo c/c++
3.0.

This started out as favor and now is taking forever.
I appreciate any help and thank you in advance.

Mark

here is the program below:

_____________________________________________-
//this program is for the daily  and weekly
//tracking of workout data for a Trainer's clients
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdio.h>
#include<ctype.h>

istream & read(char*,int length);
ostream & write(char*,int length);

ifstream ip;
ofstream fp;

struct MEAS
{
float wt;
float fatpercnt;
float neck; float chest;
float arm; float forearm;
float waist; float thigh;
float calf;
float meastime;

Quote:
} week[52];

struct DAILY
{
float calories;
float carbs;
float fat;
float protien;
float aeromins;         //mins of aerobic exercise
char aerotype[10];      //type of aerobic exercise
char sore[10];          //which body part or none
char sleep;             //yes or no
char workout[30];
float worklen;

Quote:
} days[365];

void enterdata();
struct MEAS entermeas(MEAS week);
DAILY enterdays(DAILY days);
void menu();

void main()
{
 int reply,ans2;
 do{
   do{ menu();
       cin>>reply;}
   while((reply<1)||(reply>4));
   switch(reply)
   {case(1):{
             Break2:
             enterdata();
             cout<<"\nWould you like to:\n\n";
             cout<<"\t\t1.   open another file?\n";
             cout<<"\t\t2.   go back to the main menu?\n";
            do
            { cin>>ans2;
              }
            while((ans2<1)||(ans2)>2);
           if(ans2==1)
           {goto Break2;}
           else{break;}
            }

    case(2):{break;}
    case(3):{break;}
    default:{break;}
   }
   }
while(reply!=4);
return;

Quote:
}

void menu()
{
cout<<"\nPlease Select from the following:\n\n";
cout<<"\t1.  Append / Enter Data.\n\n";
cout<<"\t2.  Analysis.\n\n";
cout<<"\t3.  Print Data Records\n\n";
cout<<"\t4.  Exit\n\n";
cout<<"Please make your choice now.  ";
return;

Quote:
}

void enterdata()
{
Break:
int ans=0;
int ctr1;
int ctr2;
char filename[30]="\0";
char an;

cout<<"Please enter the filename. Please include path and add '.REC' to the end
of the filename. ";
cout<<"\nie...A:\\dir\\filename..etc..(DO NOT INCLUDE SPACES) ";
cout<<"\nEnter the last name of the client up to 8 letters only.  ";
cout<<"so filename is smith in directory a:, type 'A:\\smith.REC'   ";
   cin>>filename;
   ip.open(filename,ios::in);

if(!ip)
{cout<<"\nFile does not exist, would you like to create a new file or";
cout<<"\nre-enter another filename?(1=CREATE, 0=RE-ENTER)  ";

  cin>>ans;
        if(ans==1)
                  { fp.open(filename,ios::out);
                    if(!fp)
                    {cout<<"\nError opening file "<<filename<<"\n";}
                    ctr1=1;
                    ctr2=1;
                    cout<<"\nFilename="<<filename<<"\n";
                    cout<<"ctr1="<<ctr1<<" and ctr2="<<ctr2<<"\n";
                    goto  Break4;
                    }
                else
           {goto Break;}

Quote:
}

fp.open(filename,ios::out);
ip>>ctr1;                                                 //PROBLEM IS
HERE!!
ip >>ctr2;                                                        //AND HERE.
cout<<"\nFilename="<<filename<<"\n";
cout<<"ctr1="<<ctr1<<" and ctr2="<<ctr2<<"\n";
ip.read((char*)week,(ctr1-1)*sizeof(week));
ip.read((char*)days,(ctr2-1)*sizeof(days));
ip.close();

Break4:

cout<<"\n\t "<<filename<<" HAS BEEN OPENED\n";
ans=0;
cout<<"Would you like to enter data for:\n\n";
cout<<"\t1.   Weekly Measurements\n\n";
cout<<"\t2.   Daily Input\n\n";
cout<<"\t3.   Both\n\n";
cout<<"\t4.   Exit\n\n";
cout<<"Please make your choice now.          ";
do
{cin>>ans;}
while(ans<=0||ans>4);
if (ans==1)
{
do{   cout<<"ctr1="<<ctr1<<" and ctr2="<<ctr2<<"\n";
week[ctr1]= entermeas(week[ctr1]);
cout<<"\nyour cuurent weight is  "<<week[ctr1].wt;
ctr1++;
cout<<"\nWould you like to enter another?(Y or N)";
cin>>an;}
while(toupper(an)=='Y');

Quote:
}

else{
if(ans==2)
{
do{
days[ctr2]= enterdays(days[ctr2]);
ctr2++;
cout<<"\nWould you like to enter another?(Y or N)";
cin>>an;}
while(toupper(an)=='Y');

Quote:
}

else{ if(ans==3)
        {
        do{
        week[ctr1]= entermeas(week[ctr1]);

        ctr1++;
        days[ctr2]= enterdays(days[ctr2]);
        ctr2++;
        cout<<"\nWould you like to enter another?(Y or N)";
        cin>>an;
          }
while(toupper(an)=='Y');
        }
        else
        {goto Break3;}
       }
  }  //1st else
  Break3:
  cout<<"\n\nFile are closing   ";
  fp<<ctr1<<"\n"<<ctr2<<"\n";
  cout<<"\nctr1="<<ctr1<<" & ctr2="<<ctr2<<"\n";
  fp.write( (char *)week,(ctr1-1)*sizeof(week));
  cout<<"sizeof week is "<<sizeof(week)<<"\n";
  fp.write((char *) days,(ctr2-1)*sizeof(days));
  fp.close();

  return;

Quote:
}

MEAS entermeas(MEAS week)
{
cout<<"\nPlease enter your weight in pounds  ";cin>>week.wt;
cout<<"Please enter your  fat percent as .2150 =21.5%  ";cin>>week.fatpercnt;
cout<<"Please enter your  neck in inches  ";cin>>week.neck;
cout<<"Please enter your  chest in inches  ";cin>>week.chest;
cout<<"Please enter your  arm in inches  ";cin>>week.arm;
cout<<"Please enter your  forearm in inches  ";cin>>week.forearm;
cout<<"Please enter your  waist in inches  ";cin>>week.waist;
cout<<"Please enter your  thigh in inches  ";cin>>week.thigh;
cout<<"Please enter your  calf in inches  ";cin>>week.calf;
cout<<"Please enter the date as YRMODY i.e.  sept 9, 1998 = 980909
";cin>>week.meastime;
return(week);

Quote:
}

DAILY enterdays(DAILY days)
{
cout<<"\nPlease enter today's calories  ";cin>>days.calories;
cout<<"Please enter today's carbohysrates ";cin>>days.carbs;
cout<<"Please enter today's fat grams ";cin>>days.fat;
cout<<"Please enter today's protien grams  ";cin>>days.protien;
cout<<"Please enter today's  minutes of aerobic exercise ";cin>>days.aeromins;
cout<<"Please enter type of aerobic exercise, i.e. .. run. walk, treadmill,
stairstep, etc...   ";gets(days.aerotype);
cout<<"Please enter which body part is sore, if there is many, please separate
using a '/' , DONT USE SPACES   ";gets(days.sore);
cout<<"Please enter if you slept well, Y=yes, N=no ";cin>>days.sleep;
cout<<"Please enter the body parts you worked out today, separate using
commas(,) ";gets(days.workout);
cout<<"How long did your workout take in minutes ";cin>>days.worklen;
return(days);
Quote:
}



Tue, 27 Feb 2001 03:00:00 GMT  
 program problem, please HELP!!!

   I am trying to make this program for my friend and just started to quickly set
   it up. He is a trainer so I am making him a program to track his client's
   results and data.

Don't send C++ source code to comp.lang.c.
--
(supporter of the campaign for grumpiness where grumpiness is due in c.l.c)

Please: do not email me copies of your posts to comp.lang.c
        do not ask me C questions via email; post them instead



Tue, 27 Feb 2001 03:00:00 GMT  
 program problem, please HELP!!!

Quote:

> I am trying to make this program for my friend and just started to quickly set
> it up. He is a trainer so I am making him a program to track his client's
> results and data.

Your program appears to be written in C++, so you if you are going to
post should post to comp.lang.c++ (or alt.comp.lang.learn.c-c++) not
to comp.lang.c.  My C++ compiler spotted the following errors (some of
which appear to be caused by word wrapping by your newsreader):

bar.cc:107: warning: string constant runs past end of line
bar.cc:49: warning: return type for `main' changed to `int'
bar.cc: In function `int main(...)':
bar.cc:77: warning: `return' with no value, in function returning non-void
bar.cc: In function `void enterdata()':
bar.cc:107: warning: ANSI C++ forbids newline in string constant
bar.cc:136: `HERE' undeclared (first use this function)
bar.cc:136: (Each undeclared identifier is reported only once
bar.cc:136: for each function it appears in.)
bar.cc:136: parse error before `!'
bar.cc:102: warning: `int ctr2' might be used uninitialized in this function
bar.cc: In function `struct MEAS entermeas(struct MEAS)':
bar.cc:227: warning: ANSI C++ forbids newline in string constant
bar.cc: In function `struct DAILY enterdays(struct DAILY)':
bar.cc:239: warning: ANSI C++ forbids newline in string constant
bar.cc:241: warning: ANSI C++ forbids newline in string constant
bar.cc:244: warning: ANSI C++ forbids newline in string constant

and I also noticed that you are using the gets() function, which
cannot be used safely.  You can replace it with fgets() or use the C++
I/O streams like you do in the rest of your program.

You may be able to eliminate the bugs by restructuring your program -
currently, you use an awful lot of gotos, and it could be that you are
getting confused somewhere in there.  

If you do need to post your code again you will probably get more
helpful responses if you reformat it to use a more regular indentation
style and reduce the size of the program.

It is customary to expect replies to newsgroup postings in the
newsgroup.  This is polite, and ensures that if someone spots an error
in an answer you will see it.

--

            http://www.tardis.ed.ac.uk/~broonie/
EUFS        http://www.eusa.ed.ac.uk/societies/filmsoc/



Wed, 28 Feb 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Need help on this programming problem ( PLEASE HELP!!!)

2. A C program problem, Please help!

3. A C Program Problem-Please Help!

4. Programming problems..please help

5. Programming Problem, please Help

6. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

7. Problem with Program!!Please Help (AGAIN).

8. Please help-two problems with C program

9. HELP w/ C Programming Problem...PLEASE!!

10. Please Beginner Help with C programming problem.

11. Please help beginner with programming problem

12. Problem in my program.....Please Help

 

 
Powered by phpBB® Forum Software