Error in compiling program

Hello, i'm new in c+..
I have to do a project in my college so please help me in correcting the problem...

#include <iostream.h>
#include <iomanip.h>
#include <math.h>
#include <string>
using namespace std;

void formula();
void result();

int age, weight, height;
string catname[20];
double bmi;
char name[20];





void main()
{
cout<<"\n\nWelcome to Body Mass Index Calculator"<<endl;
cout<<"Please fill up your identity information below."<<endl;
cout<<"Name :";
cin.getline(name,20);
cout<<"Age :";
cin>>age;
cout<<"Height(cm) :";
cin>>height;
cout<<"Weight(kg) :";
cin>>weight;
formula();
result();

}

void formula()
{
bmi=weight/pow(height/100,2);

if (bmi <= 18.5)
char catname = "Underweight";
else if (bmi <= 24.9)
char catname = "Ideal";
else if (bmi <= 29.9)
char catname = "Overweight";
else
char catname = "Obese";
}

void result()
{
cout<<setfill('-')<<endl;
cout<<"\n\nInformation: Name : "<<name<<endl;
cout<<" Age : "<<age<<endl;
cout<<" Height : "<<height<<" cm"<<endl;
cout<<" Weight : "<<weight<<" kg"<<endl;
cout<<" Bmi : "<<bmi<<endl;
cout<<" Category : "<<catname<<endl;
cout<<"\n\nHave a nice day!"<<endl;
cout<<setfill('-')<<endl;
}


and this is the error...

--------------------Configuration: Text2 - Win32 Debug--------------------
Compiling...
Text1.cpp
C:\Users\User\Desktop\Text1.cpp(44) : error C2440: 'initializing' : cannot convert from 'char [12]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Users\User\Desktop\Text1.cpp(46) : error C2440: 'initializing' : cannot convert from 'char [6]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Users\User\Desktop\Text1.cpp(48) : error C2440: 'initializing' : cannot convert from 'char [11]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Users\User\Desktop\Text1.cpp(50) : error C2440: 'initializing' : cannot convert from 'char [6]' to 'char'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.

Text1.obj - 4 error(s), 0 warning(s)
For example this statement is invalid

char catname = "Underweight";

the left object has type char that is it can contain a single character. The right expression is a const pointer to string literal "Underweight" that is has type const char *. You may not initialize an object of type char by an expression of type const char *

So you should decide what is catname and how it should be declared. For example it could be declared either as const char *, or char [] or even as std::string
looks like a horrible mix of C and C++.

i'd use std::string. Also get used to passing your input parameters from the user into your methods, rather than declaring them as global variables.

I think:
bmi=weight/pow(height/100,2);

will need to be:
bmi=weight/pow(height/100.0,2);
as well.
Topic archived. No new replies allowed.