Hey guys. So I'm doing this project for my end-of-year exam and it's running. But I can't seem to go further beyond a point. This program asks the user as series of questions and then analyses the symptoms to give the result ie, if the user is diabetic or not. When i run the program, after the first question is answered, an error message comes up on the screen which says that the program has encountered a problem and has to terminate.
I think something's wrong with the main function. I am also very confused about the variables m and n in the program.

Here's the program, if any of you could point out the error in the logic of the program, it would really be great!

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>


void clrscr()
{
system("cls");
}


class diabetes
{
private:
char name[40];
float age;
int wt;
float ht;
char sex;
char *s;
public:
void welcome_screen(void);
void getvalue(void);
void getlevel1_symptoms(void);
void getlevel2_symptoms(void);
void getlevel3_symptoms(void);
int analyse_symptoms(int);
char display_message(int,int);
};
void main()
{
char ch,choice,cho;
//int m,n=2;
float m;
int n=1;
void diagnosis(void);
diabetes dts;
dts.welcome_screen();
dts.getvalue();
diagnosis();
dts.getlevel1_symptoms();
m=dts.analyse_symptoms(n);
choice=dts.display_message(m,n);
choice=toupper(choice);
if(choice=='Y')
{
++n;
dts.getlevel2_symptoms();
m=dts.analyse_symptoms(n);
choice=dts.display_message(m,n);
choice=toupper(choice);
if(choice=='Y')
{
++n;
dts.getlevel3_symptoms();
m=dts.analyse_symptoms(n);
choice=dts.display_message(m,n);
choice=toupper(choice);
if(cho=='Y')
{
++n;
dts.getlevel3_symptoms();
m=dts.analyse_symptoms(n);
cho=dts.display_message(m,n);
}
}
}
}



void diabetes::welcome_screen()
{
cout<<"********* W E L C O M E ********* "<<endl<<endl<<endl ;
cout<<"I N T E R N A T I O N A L   I N D I A N   S C H O O L "<<endl<<endl  ;
cout<<" M E D I C A L   D I A G N O S I S   S O F T W A R E  "<<endl<<endl;
cout<<"D O N E  B Y  : "<<endl<<endl;
cout<<"ANONYMOUS "<<endl<<endl;
cout<<"******** PRESS ANY KEY TO CONTINUE ********* "<<endl;
getch();
return;
}



void diabetes::getvalue()
{
clrscr();
cout<<" P E R S O N A L   I N F O R M A T I O N"<<endl<<endl<<endl;
cout<<"N A M E        :"<<endl<<endl;
gets(name);
cout<<"A G E          :"<<endl<<endl;
cin>>age;
cout<<"W E I G H T    :"<<endl<<endl;
cin>>wt;
cout<<"H E I G H T    :"<<endl<<endl;
cin>>ht;
cout<<"S E X (M/F)    :"<<endl<<endl;
cin>>sex;
getch();
return;
}



void diagnosis(void)
{
clrscr();
cout<<" **  D I A G N O S I S   W I N D O W **   ";
cout<<"\n\n\n";
cout<<" Let's have a look at the symptoms.........";
cout<<"\n\n\n";
cout<<" Please enter the form in the next page .";
cout<<"\n\n\n\n\n\n\t\t\t ";
cout<<"***** PRESS ANY KEY ***** "<<endl<<endl;
getch();
}


void diabetes::getlevel1_symptoms(void)
{
clrscr();
cout<< " *** MEDICAL DIAGONOSIS FORM *** "<<endl<<endl;
for(int i=0;i<9;i++)
{



cout<<"FREQUENCY OF THIRST(H(HIGH),/L(LOW)/N(NORMAL):"<<endl<<endl;


cin>>s;


cout<<"FREQUENCY OF URINATION(H(HIGH),/L(LOW),/N(NORMAL):";


cin>>s;


cout<<"VISION (I(IMPAIRMENT),/N(NORMAL)";



cin>>s;


cout<<"URINE SUGAR(P(PASSIVE)/A(ACTIVE);";


cin>>s;


cout<<"KETONUREA(P(PASSIVE)/A(ACTIVE)";


cin>>s;


cout<<"FASTING BLOOD SUGAR(H(HIGH)/L(LOW)/N(NOMAL)";


cin>>s;


cout<<"R B S (H(HIGH)/L(LOW)/N(NORMAL)";


cin>>s;


cout<<"FAMILY HISTORY OF DIABETES(P(PASSIVE)/A(ACTIVE)";


cin>>s;


cout<<"OGTT(D/N)";


cin>>s;



}
}


int diabetes::analyse_symptoms(int n)


{
int i=0;
int count=0;
int result=0;
switch(n)
{
case 1:    if(s[9]=='D' )
result=-1;
else
if(s[5]=='P'&& s[6]=='H' && s[7]=='H')
result=-1;
else
{
for(i=0;i&result;i++)
{
if(s=='H'||s=='P'||s=='D'||s=='I')
count++;
}
if(count>5)
result=-1;
}
break;


case 2: if((s[0]=='P')||(s[1]=='P')||(s[2]=='P')||(s[3]=='H')||(s[4]=='P')||(s[5]=='P')||(s[6]=='P'))
result=-1;
else
result=0;
break;
case 3: if((s[0]=='Y')&&(s[1]=='N')&&(s[2]=='W')&&(s[3]=='P')&&(s[4]=='P')||


(s[0]=='Y')&&(s[1]=='B')&&(s[2]=='W')&&(s[3]=='P')&&(s[4]=='P')||


(s[0]=='Y')&&(s[1]=='N')&&(s[2]=='M')&&(s[3]=='P')&&(s[4]=='P')||


(s[0]=='Y')&&(s[1]=='N')&&(s[2]=='Y')&&(s[3]=='P')&&(s[4]=='P'))
result=0;
else
result=-1;
break;
default:break;
}
return(result);
}


void diabetes::getlevel2_symptoms()
{


int j=0;


cout<<"PANCREATITIS(P/A)  :";


cin>>s[j];
s[j]=toupper(s[j]);
j++;


cout<<"CARCINOMA(P/A)  :";


cin>>s[j];
s[j]=toupper(s[j]);
++j;


cout<<"CIRHHOSIS(P/A)     :";


cin>>s[j];
s[j]=toupper(s[j]);
++j;


cout<<" HCTS  (H/L/N)     :";


cin>>s[j];
s[j]=toupper(s[j]);
++j;


cout<<"HEPATITIS(P/A)    :";


cin>>s[j];
s[j]=toupper(s[j]);
++j;


cout<<" HORMONAL DISORDER(P/A):";



cin>>s[j];
s[j]=toupper(s[j]);
++j;


cout<<" PANCREATECTOMY(P/A) :";


cin>>s[j];
s[j]=toupper(s[j]);
++j;


}


void diabetes::getlevel3_symptoms()
{
int k=0;



cout<<" AGE(young(Y)/Middle aged(M)/Elderly(E))";


cin>>s[k];
s[k]= toupper (s[k]);
++k;


cout<<"BODY WEIGHT(normal(N)/Above normal(A)/Below normal(B)/obese)";


cin>>s[k];
s[k]= toupper(s[k]);
++k;


cout<<" DURATION (weeks(W)/Months(M)/Years(Y))";


cin>>s[k];
s[k]= toupper(s[k]);
++k;



cout<<"KETONUREA(P/A)";


cin>>s[k];
s[k]= toupper(s[k]);
++k;


cout<<"AUTO ANTIBODIES(P/A)";


cin>>s[k];
s[k]= toupper(s[k]);
++k;


}


char diabetes::display_message(int n,int m)


{
char ch;


switch(n)
{
case 1:
switch(m);
{
case 0:
cout<<"THE PERSON IS NOT DIABETIC";


cout<<"PRESS ANY KEY TO QUIT." ;


cin>>ch;
break;
exit(0);


case -1:



cout<<"THE PERSON IS DIABETIC ";


cout<<"PROCEED (Y/N)  ?";


cin>>ch;
break;


default: break;
}
break;



case 2:
switch(m)
{
case 0:
cout<<"IT IS PRIMARY DIABETES.";


cout<<"Proceed(Y/N)?";


cin>>ch;
break;


case -1:
cout<<"IT IS SECONDARY DIABETES";


cout<<"PRESS ANY KEY TO QUIT";
getch();
break;


}
break;


case 3:switch(m)
{
case 0:
cout<<" IT IS INSULIN DEPENDENT DIABETES";


cout<<"press any key to quit";
getch();
break;



case -1:
cout<<"IT IS NON INSULIN DEPENDENT DIABETES";


cout<<"PRESS ANY  KEY TO QUIT .";
getch();
break;


default: break;


}


break ;


}
return (ch);
}

To begin with, please use [code] tags around your code samples. This will cause the forum software to retain the formatting for the code, making it far easier to read.

Second, using void main() is incorrect in C++. While the older standards allowed it, it was always non-portable and discouraged, and it is my understanding that the current standard forbids it outright. You should always use int main() instead.

Third, your headers are in the older format ( <iostream.h> rather than <iostream> , <stdio.h> rather than <cstdio> , etc.) This, plus the use of the <conio.h> header, implies that you are using an outdated compiler, probably Turbo C++ given where you are. I realize that this may not be something you have any control over, but you should be aware that the language has changed substantially in the twenty-two years since that compiler was released. If you have any choice in the matter, I recommend moving to a newer compiler and IDE such as Code::Blocks.

(Oh, and system() is normally found in <stdlib.h> , or <cstdlib> in modern C++, which you didn't include.)

Finally, I would strongly recommend using more descriptive variable names; most of the ones you are using don't tell us anything about what they are meant to represent.

That having been said, I have to ask, what is confusing you regarding the n and m variables? I am assuming that this is your own code, so you presumably had some specific idea in mind for them, so what is confusing about them?

Looking more carefully at the code, I am somewhat concerned about the way it represents the patient information and handles the screens. As it is now, you have all of the information about the patient in the class diabetes , combined with all of the handling for the input and output. Normally, you would want to separate these concerns. From an OOP perspective, this is not a very clean or object-oriented design, and it is typical of someone who hasn't really understood OO principles.

I would recommend significantly re-writing the code, with a Patient class handling the actual data and a separate set of functions for the user interface.

On a side note, I would strongly recommend using fgets instead of gets() , or better still, avoiding the C-style I/O entirely. Mixing C and C++ styles of I/O is problematic at best, and the C++ style is generally far easier to use than the C style is.

As for why the program is crashing, I think I've figured it out. You declare diabetes.s as type char* , but never assign an array to it. When you try to assign the symptom information to it, it gets a segmentation fault.

You can, I think, replace all three of the getevel*_symptoms() functions with a single table-driven function, something like this:

void get_symptoms(Patient* pat)
{
    char symptom;
    string queries[] = {
        "FREQUENCY OF THIRST(H(HIGH),/L(LOW)/N(NORMAL)",
        "FREQUENCY OF URINATION(H(HIGH),/L(LOW),/N(NORMAL)",
        "VISION (I(IMPAIRMENT),/N(NORMAL)",
        "URINE SUGAR(P(PASSIVE)/A(ACTIVE)",
        "KETONUREA(P(PASSIVE)/A(ACTIVE)",
        "FASTING BLOOD SUGAR(H(HIGH)/L(LOW)/N(NORMAL)",
        "R B S (H(HIGH)/L(LOW)/N(NORMAL)",
        "FAMILY HISTORY OF DIABETES(P(PASSIVE)/A(ACTIVE)",
        "OGTT(D/N)",
        "** Medical History **",
        "PANCREATITIS(P/A)",
        "CARCINOMA(P/A)",
        "CIRHHOSIS(P/A)",
        "HCTS(H/L/N)",
        "HEPATITIS(P/A)",
        "HORMONAL DISORDER(P/A)",
        " PANCREATECTOMY(P/A)",
        "** Personal Details **",
        "AGE(young(Y)/Middle aged(M)/Elderly(E))",
        "BODY WEIGHT(normal(N)/Above normal(A)/Below normal(B)/obese)",
        "DURATION (weeks(W)/Months(M)/Years(Y))",
        "AUTO ANTIBODIES(P/A)",
        ""
    };


    clrscr();
    cout<< " *** MEDICAL DIAGONOSIS FORM *** "<<endl<<endl;

    for (int i = 0; queries[i] != ""; i++)
    {
        if (queries[i][0] == '*')
        {
            clrscr();
            cout << queries[i] << endl << endl;
        }
        else
        {
            cout << queries[i] << ": ";
            cin >> symptom;
            cin.ignore();
            pat->setSymptom(i, toupper(symptom));
        }
    }
    clrscr();
}

I designed it to make it fairly easy to split the different screens up automagically; all you need is to insert a string between the end of one screen and the beginning of the next which contains an asterisk ('*') as the first character, and the program will treat it as a new screen header.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.