i need opinion foer data structure im really stupid in this

#include<iostream>
using namespace std;

struct computer
{
char brand[20];
char serial_no[30];
int year_produce;
};

struct customer
{
char name[20];
char email[20];
computer favouriteComputer;
}adam;

int main ()
{
cout<<"Enter name : ";
cin>>adam.name;
cout<<"Enter email : ";
cin>>adam.email;
cout<<favouriteComputer : " ;
cin>>adam.favouriteComputer;

cout<<"brand:"<<adam.brand<<"serial_no:"<<serial_no<<endl;
cout<<"year_produce:"<<year_produce<<"/"<<year_produce<<"/"<<year_produce<<endl;

return 0;
}
My opinion is that you're trying to inject C-style ways of doing things inside of a C++ program.

What happens when the brand, serial_no, name, or email entered is over 20 or 30 characters? There's no safety checks here, and no way to extend it. If this is just for a simple assignment, then it's fine. But if you're actually trying to write a robust program, it won't be acceptable.

Also, global variables like your adam variable are generally frowned upon. Declare adam in main instead.

But as far as actual errors, and not just opinions:

cin>>adam.favouriteComputer;
You'll also need a way to overload the >> operator to work with a computer object.

ex:
1
2
3
4
std::istream& operator>>(std::istream& is, computer& comp)
{
    return is >> comp.brand >> comp.serial_no >> comp.year_produce;
}


cout<<"year_produce:"<<year_produce
The compiler doesn't know what year_produce is by itself. You need to tell it you mean adam.favoriteComputer.year_produce. Same with the other variables.
Last edited on
1) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

2) The problem is that you're trying to use streaming operators << and >> to input and output your objects. However, the compiler can't magically know how to do that - you have to tell it how to do that.

Specifically, you have to override the << and >> operators to input and output whatever you want them to do.

If you don't know how to do this, search online for tutorials. Here's one I found from a simple Google search:

https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm
also tagging a variable on a class/struct is archaic and here it is also making a global with all those problems. just do

int main()
{
...
customer adam; //like a normal variable creation
..

customer has-a computer is fine.
Registered users can post here. Sign in or register to post.