floating point (invalid)

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? :(




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#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;
}



anyone? :(
You have to read up on how cin works, and also on strings.

char name; makes a variable named "name" that can hold exactly one character.
You want it to hold an arbitrary number. Read up on std::string for that.

Also, keep in mind that newlines don't magically get consumed by cin.

cin >> m; where m is a float will read a float, but if the user pressed enter
after typing that one number, then what is left in the cin input buffer is the
newline, since newline is not a valid part of a float. If you then go to read
a second float immediately without consuming the newline, nothing will be
read.
Are you sure that this does not work?
1
2
3
4
5
6
7
8
#include <iostream>

int main() {
    float a,b;
    std::cin>>a>>b;
    std::cout<<a<<' '<<b<<std::endl;
    return 0;
}
I think cin just ignore leading spaces and new lines when reading. getline is the one who causes troubles with strings.
Last edited on
Topic archived. No new replies allowed.