+ 3

Write a program to accept the student detail such as name and 3 different marks by get_data() method and display the name?hlp

#include <iostream> using namespace std; class student { private : char name ; int Eng,Urdu,Maths; public : void getdata() { cout<<"\nEnter name "; cin>>name ; cout<<"\nEnter eng marks"; cin>>Eng; cout <<"\nEnter urdu marks"; cin>>Urdu; cout <<"\nEnter maths marks"; cin>>Maths; } void display() { cout <<"\nName:"<<name; cout <<"\nEng marks:"<<Eng; cout <<"\nUrdu marks:"<<Urdu; cout <<"\nMaths marks"<<Maths; } }; void main() { student obj; obj.getdata(); obj.display( ); return 0; };

24th Aug 2020, 2:41 PM
Kaamila Salahuddin
Kaamila Salahuddin - avatar
10 Antworten
+ 4
you did little mistakes first thing you have not mentioned char size in name Second thing in main function after last curly bracket you put semicolon . your main function is void type but u used return statement. now its working fine. #include <iostream> using namespace std; class student { private : char name[20] ; int Eng,Urdu,Maths; public : void getdata() { cout<<"\nEnter name "; cin>>name ; cout<<"\nEnter eng marks"; cin>>Eng; cout <<"\nEnter urdu marks"; cin>>Urdu; cout <<"\nEnter maths marks"; cin>>Maths; } void display() { cout <<"\nName:"<<name; cout <<"\nEng marks:"<<Eng; cout <<"\nUrdu marks:"<<Urdu; cout <<"\nMaths marks"<<Maths; } }; int main() { student obj; obj.getdata(); obj.display( ); return 0; } ........ Thankyou........
24th Aug 2020, 2:59 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 4
Coz char name can store a single character only and char name[20] is an array of characters and each character will be stored in a continuous manner. For example , taking a word "world". w will be stored at name[0], o at name[1] and so on...
24th Aug 2020, 3:43 PM
shreya sinha
shreya sinha - avatar
+ 3
Use `string` as type for the <name> field instead of `char`. And set main function return value to `int` because you return something there.
24th Aug 2020, 2:56 PM
Ipang
+ 3
Kindly encorrect the mistake in this code by mention the line
24th Aug 2020, 2:58 PM
Kaamila Salahuddin
Kaamila Salahuddin - avatar
+ 3
Problem resolve by Ada Lovelace...... but I want to know .... how is it resolved what is the logic of ..... char name [20] ?
24th Aug 2020, 3:06 PM
Kaamila Salahuddin
Kaamila Salahuddin - avatar
+ 2
C++ code help? Any one ?
24th Aug 2020, 2:42 PM
Kaamila Salahuddin
Kaamila Salahuddin - avatar
+ 2
Kindly guide me ..... I can't understand my this mistake ....
24th Aug 2020, 3:02 PM
Kaamila Salahuddin
Kaamila Salahuddin - avatar
+ 2
But why integer value could not be placed ?
24th Aug 2020, 4:37 PM
Kaamila Salahuddin
Kaamila Salahuddin - avatar
+ 2
Could you explain your question(integer value)?
24th Aug 2020, 6:07 PM
shreya sinha
shreya sinha - avatar
+ 1
Kaamila Salahuddin In the class definition, inside private: section; Change this line 👇 char name; -> string name; And in the main function declaration; Change this line 👇 void main() -> int main() That's it I guess ...
24th Aug 2020, 3:03 PM
Ipang