I am practicing my C++ programming just making an
Health Care related program, I cant seem to access
my data that is stored on a .txt file,
All I get is that f is not declared in this scope
Heres the code:

#include <iostream>
    #include <stdlib.h>
    #include "structr.h"

    using namespace std;
void regsPat();
void database(FILE * f);
void saved(patient ab);

      void menu(){
        int opc;
        system("cls");

        cout<<"   ----<-<-<>--<-INOMO MEDICARE->--<>-->->----"<<endl;
        cout<<"        1. Register new Patient "<<endl;
        cout<<"        2. Database             "<<endl;
        cout<<"        3. Statistics           "<<endl;
        cout<<"        4. Corporate            "<<endl;
        cout<<"        5. Help          \n\n\n "<<endl;
        cin>>opc;

               switch (opc){

                 case 1:
                   regsPat();
                   break;

                 case 2:
                  database(f);//<------F WAS NOT DECLARED IN THIS SCOPE
                     break;

               }


      }

As you can see im trying to access a external function
its in another file heres the code that im using:

void database (FILE * f)
{int a;
     system("cls");
//float suma=0, promedio;
//holaa.codigo=0;
f = fopen("saved.txt", "r");

if( f == NULL)
{
printf("\nData Does not Exist! \nPlease Create it");
return;
}

do
{
    fread(&ab , sizeof(ab), 1, f);
    if (feof(f))break;

    printf("Name: %d\n",ab.name);
    printf("Age: %s\n",ab.age);
    printf("Body fat: %s\n",ab.bodFat);
    printf("Height: %.1f\n",ab.height);
    cout<<"=========================================================================="<<endl;
    }while(!feof(f));

    fread(&ab,sizeof(ab),1,f);
    fclose(f);
    fseek(f,ftell(f)-sizeof(ab),SEEK_END);
    getch();
    system("cls");
    return;
}

Why does the database function needs to be passed anything? From the looks of the function, all it does it use the "f" variable as a local variable, and it doesn't use its passed-value for anything. Just make it a local variable and make the database function a function without parameters.

The error is pretty obviously because "f" was never declared in the menu() function, just like the compiler says. You could declare a variable "f" in menu() and pass it to database(), but I see no point in that, because database() doesn't use that value.

Finally, if you want to practice your C++ programming, maybe you should try not to use C functions (like fopen, printf, etc.).

This article has been dead for over six months. Start a new discussion instead.