3 Replies - 1802 Views - Last Post: 03 October 2010 - 02:13 AM Rate Topic: -----

#1 Guest_fikri*


Reputation:

floating point (invalid)

Posted 03 October 2010 - 12:45 AM

hey guys, i need help with this question.

i need to write a program to get the output which is :

i) there will be two class which is info_student and mark_student. class mark_student will be friend to a class info_student.

ii) class info student will have name and ic with data type character as a variable. it also have function set_data that accept two variable above as a pointer in the parameter list.

iii) while mark_student will have mark1, mark2 and total with data type float. the function is setmark, calculateMark and display.

i already programmed and compiled the code, but i have the output show "floating point invalid" when i input the data. what have i done wrong here? :(

#include<iostream.h>
class info_mark;
class info_student
{
private:
char name,ic;
public:
int student()
{return name,ic;}
};

class info_mark
{
float mark1,mark2;
public:
int calculate_mark()
{return mark1 +mark2;}
};

void main()
{
char a,b;
float m,n;
cout<<"Name:";
cin>>a;
cout<<"Ic:";
cin>>b;

cout<<"mark 1:";
cin>>m;
cout<<"mark 2:";
cin>>n;

cout<<"total:"<<m+n;
}


Is This A Good Question/Topic? 0

Replies To: floating point (invalid)

#2 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: floating point (invalid)

Posted 03 October 2010 - 01:28 AM

Lots of issues here.

1) Name of a student should be stored in an array
char a[15]; //name can have a max of 14 characters + 1 null character
cin>>a;



2) You shouldn't use void main.Rather use int main and add 'return 0' to the end of main
int main() {
   ...
   ...
   return 0;
}


Modern compilers don't allow void main.
The reason why you shouldn't use void main has summarized here.

Quote

1. is wrong according to the standards.
2. is not guaranteed to work properly.
3. may cause problems for the operating system.


3) iostream.h is isn't used anymore.I guess you are using turbo/borland C IDE. I recommend you use Codeblocks.Download the codeblocks-10.05mingw-setup.exe and install. Pretty easy to use.

4)A side thing - Read the comments
char a,b;
cin>>a; //Say your input is MN..
cin>>b; //...then this cin is 'skipped'

cout<<"a="<<a<<" and "<<"b="<<b<<endl; //..but not quite...output is 'a=M and b=N'...why? read on!


Actually the cin>>b isn't skipped. Since you entered two characters - when you were supposed to only enter one - a='M' and b='N'.

To stop this 'skipping' of character and to take only the first character entered you can use a cin.ignore().
Read comments:
char a,b;

cin>>a; //Say that you entered MN...
cin.ignore(); //N is discarded..
cin>>b; //you will have to enter a character. say you enter O

cout<<"a="<<a<<" and "<<"b="<<b<<endl; //a=M and b=O


Hope that helped.

This post has been edited by Munawwar: 03 October 2010 - 01:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 162
  • View blog
  • Posts: 457
  • Joined: 20-January 10

Re: floating point (invalid)

Posted 03 October 2010 - 01:59 AM

Oops I forgot to expand on point 3).
#include <iostream>
using namespace std;


is used instead of iostream.h. Now if you are thinking what is a 'namespace std', you can read about namespace sometime later :)

Also familiarize yourself with the C++ string library. It is much easier to use than C-style character arrays
#include <iostream>
#include <string>
using namespace std;

int main() {
   string text;
   text="Hello ";
   text+="World!";

   cout<<text<<endl;
   return 0;
}


Read more here.
For quick reference check cplusplus.com
Was This Post Helpful? 0
  • +
  • -

#4 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: floating point (invalid)

Posted 03 October 2010 - 02:13 AM

int student()
{return name,ic;}

What is this??A function can return only one value.
Also:
int calculate_mark()
{return mark1 +mark2;}


As here mark1 and mark2 are of float type.So if you want floating point addition result the your return type should also be float type.
float calculate_mark()
{return (mark1+mark2);}

One last thing use parentheses on expressions its a good habit imo.

This post has been edited by aaa111: 03 October 2010 - 02:18 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1