struct

Aslam u alikum ,

this code has logical error , i declared two structures name st1 st2 i copy st1 to st2 after writing st1=st2 i tried to print values using st2 but no result was shown on the console can any one help

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
 #include <iostream>
#include<string>
using namespace std;

    struct student{

    string name;
    int age ;
    string fathername;
    int fathersage;

    };
int main()
{
    student st1,st2;
 st1.fathername="fathernameone";
 st1.fathersage=34;
 st1.name="nameone";
 st1.age=18;


cout<<st1.fathername<<endl;
st1=st2;
cout<<st2.fathersage<<endl;
cout<<st2.age<<endl;
cout<<st2.name<<endl;
cout<<st2.fathername<<endl;
    return 0;
}
Last edited on
Perhaps your assignment is in the wrong order?
st1=st2;
At this point st2 has not been initialized. Remember the value on the right side of the assignment is being assigned the values of the left side of the assignment.

By the way you should probably get values for the age and fathersage, although the values will be undefined, the strings will be at their default values (empty).

thanks jib
Topic archived. No new replies allowed.