Help with structures
Oct 15, 2013 at 6:15am UTC
i need help with structures, i think my function call is not working and i dont know how to fix it. Basically i want it to output my getstudent function and once i get the information i want it to output with the displaystudent. any help and all help will be appreciated.
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <iostream>
#include <string>
using namespace std;
void displayStudent(Student x)
{
cout << x.first << '\t' << x.last << endl;
cout << x.id << endl;
cout << x.gpa << endl;
cout << "DOB" << x.dob.month << "/" << x.dob.day << "/" << x.dob.year << endl;
cout << x.location.streetName << endl;
cout << x.location.city << "\t" << x.location.state << " " << x.location.zip;
}
Student getStudent()
{
Student x;
cout << "Enter First Name" << endl;
cin >> x.first;
cout << "Enter Last name" << endl;
cin >> x.last;
cout << "Enter ID" << endl;
cin >> x.id;
cout << "Enter GPA" << endl;
cin >> x.gpa;
cout << "Enter the month of your Birthday" << endl;
cin >> x.dob.month;
cout << "Enter the day of your Birthday" << endl;
cin >> x.dob.day;
cout << "Enter the year of your Birthday" << endl;
cin >> x.dob.year;
cout << "Enter the street name you live in" << endl;
cin >> x.location.streetName;
cout << "Enter the city you live in" << endl;
cin >> x.location.city;
cout << "Enter the state you live in" << endl;
cin >> x.location.state;
cout << "Enter the zipcode" << endl;
cin >> x.location.zip;
return x;
}
struct Date
{
int month;
int day;
int year;
};
struct Address
{
string streetName;
string city;
string state;
string zip;
};
struct Student
{
string first;
string last;
string id;
double gpa;
Date dob;
Address location;
};
int main()
{
Student x;
Student getStudent();
void displayStudent();
cin.get();
cin.get();
return 0;
}
Oct 15, 2013 at 6:37am UTC
You are not calling your functions correctly. The function getStudent() returns a Student datatype, so you need to store the value returned by this function in a variable of type Student.
Similarly the function displayStudent() requires a Student datatype input so you need to supply that input variable to that function.
1 2 3 4 5 6 7 8 9 10 11 12 13
int main()
{
Student x;
Student myStudent = getStudent();
displayStudent( myStudent ); // do not put void here.
cin.get();
cin.get();
return 0;
}
Last edited on Oct 15, 2013 at 6:38am UTC
Oct 15, 2013 at 6:45am UTC
hm when i tried that it still wouldn't compile.
Oct 15, 2013 at 6:47am UTC
What is the error that it shows?
Oct 15, 2013 at 6:49am UTC
in the errorlist on visual studios it shows 46 errors
mostly with error c2228 and 2065
example
one of the errors is
error C2228: left of '.state' must have class/struct/union
error C2065 'x' is undeclared
Oct 15, 2013 at 7:00am UTC
Try moving the following struct definition to the beginning of your program, before the functions.
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
struct Date
{
int month;
int day;
int year;
};
struct Address
{
string streetName;
string city;
string state;
string zip;
};
struct Student
{
string first;
string last;
string id;
double gpa;
Date dob;
Address location;
};
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#include <iostream>
#include <string>
using namespace std;
struct Date
{
int month;
int day;
int year;
};
struct Address
{
string streetName;
string city;
string state;
string zip;
};
struct Student
{
string first;
string last;
string id;
double gpa;
Date dob;
Address location;
};
void displayStudent(Student x)
{
cout << x.first << '\t' << x.last << endl;
cout << x.id << endl;
cout << x.gpa << endl;
cout << "DOB" << x.dob.month << "/" << x.dob.day << "/" << x.dob.year << endl;
cout << x.location.streetName << endl;
cout << x.location.city << "\t" << x.location.state << " " << x.location.zip;
}
Student getStudent()
{
Student x;
cout << "Enter First Name" << endl;
cin >> x.first;
cout << "Enter Last name" << endl;
cin >> x.last;
cout << "Enter ID" << endl;
cin >> x.id;
cout << "Enter GPA" << endl;
cin >> x.gpa;
cout << "Enter the month of your Birthday" << endl;
cin >> x.dob.month;
cout << "Enter the day of your Birthday" << endl;
cin >> x.dob.day;
cout << "Enter the year of your Birthday" << endl;
cin >> x.dob.year;
cout << "Enter the street name you live in" << endl;
cin >> x.location.streetName;
cout << "Enter the city you live in" << endl;
cin >> x.location.city;
cout << "Enter the state you live in" << endl;
cin >> x.location.state;
cout << "Enter the zipcode" << endl;
cin >> x.location.zip;
return x;
}
int main()
{
Student x;
Student getStudent();
void displayStudent();
cin.get();
cin.get();
return 0;
}
Oct 15, 2013 at 7:05am UTC
ah thank you so much, that was the problem xD. it works now . thanks for all the help =)
Topic archived. No new replies allowed.