structure and arguement

I have to write a program that takes a person's name as arguement and prints out all of the information about said person.
I wrote a code to do this but it's not working with the name passed as arguement and I can't really understand why. This is the code:

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
  struct car {
    ...
  } 

  struct person {
  ... 
  Car car
  } 

  void InfoPerson(Car car, Person person) // i think this is where i'm doing something wrong)
  { 
  ... 
  }

  int main {
  Car namecar:
  ... 

  Person guy1;
  ...

  Person guy2;
  ...

  string person = argv[1] 

  InfoPerson(namecar, person)

I know that there's surely something wrong with namecar as it always prints out the same thing even if i have the person has a different car, but i don't know how else to define InfoPerson.
I also don't understand why I cannot use person in InfoPerson.
http://sscce.org/
Rather than random single lines, make a short example.
Each class only needs 1 variable and 1 method to demonstrate the issue.

Then a few lines in main to finish the job.

The end result is something which either clearly demonstrates the issue, or the very exercise of doing it makes you realise what your mistake was.
Either way, it's a win for everyone.
Hello irene 127,

As salem c said a complete is better than code that will not compile and leaves everyone guessing at what you did.

Line 10 looks OK for now, but it is what is missing between the {}s that is needed. The same applies to all the other "..."s.

Line 27 would be more of a problem because the 2nd parameter is not what is expected be the function. "person" is a type not an object of the struct.

Andy

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
#include <iostream>
#include <string>
using namespace std;
  
struct Car                      // UPPER case C
{
   string maker;
   string type;
}; 

struct Person                   // UPPER case P
{
   string name;
   Car car;
}; 

void InfoPerson( Person person )          // In a future life: void InfoPerson( const Person &person )
{ 
   cout << "Person's name: " << person.name << '\n';
   cout << "Drives a " << person.car.maker << " " << person.car.type << '\n';
}

int main()
{
   Car namecar{ "Vauxhall", "Astra" };
   Person guy1{ "Joe Bloggs", namecar };
   InfoPerson( guy1 );
}




Or you could do something like
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
#include <iostream>
#include <string>
using namespace std;
  
struct Car
{
   string maker;
   string type;
   
   friend ostream &operator << ( ostream &out, const Car &car ){ return out << car.maker << " " << car.type; }
}; 

struct Person
{
   string name;
   Car car;
   
   friend ostream &operator << ( ostream &out, const Person &person ){ return out << person.name; }
}; 

int main()
{
   Car namecar{ "Vauxhall", "Astra" };
   Person guy1{ "Joe Bloggs", namecar };
   cout << guy1 << " drives a " << guy1.car << '\n';
}
Last edited on
Topic archived. No new replies allowed.