ARTICLE AD BOX
To read name with space you may need getline(cin, yourcar);
instead of getline(cin, yourcar, ' '); and instead of cin >> yourcar;
By the way: to make code more readable I use _ in your_cars.
Because name yourcar is very similar to yourcars so I used new_car.
To work with two arrays/vectors you may need nested for-loops.
for(int my_idx = 0; my_idx < size(my_cars); my_idx++) { for(int your_idx = 0; your_idx < your_cars.size(); your_idx++) { if(your_cars.at(your_idx) == my_cars[my_idx]) { cout << "we both share a dream car! it is: " << your_cars.at(your_idx) << "\n"; } } }of course you may also nested loop with my_cars inside loop with your_cars
for(int your_idx = 0; your_idx < your_cars.size(); your_idx++) { for(int my_idx = 0; my_idx < size(my_cars); my_idx++) { if(your_cars.at(your_idx) == my_cars[my_idx]) { cout << "we both share a dream car! it is: " << your_cars.at(your_idx) << "\n"; } } }You may also use break inside if to skip rest of loop when you find matching cars.
And to check if some car doesn't match then you may need extra variable before inner loop - e.g. bool matching = false - and set it true in if, and check if it still false after leaving inner loop.
With first version of nested loop you may check which of my cars is not your dream (but not what of your cars is not my dream).
With second version of nested loop you may check which of your cars is not my dream (but not what of my cars is not your dream).
To check both it may need two versions or it may need more complex code with extra vector with true/false values.
Full code used for tests:
#include <iostream> #include <vector> using namespace std; string my_cars[] = { "MX5", "560sec", "560sl", "sl65", "lp500", "f40", "demon", "6*6", "g550 cab", "u5000", "slk500", "sl600", "lc79", "sls", "slr", "valkyrie", "cls", "911 targa", "firebird" }; vector<string> your_cars = {}; int main() { int n; string drcar; // ------------------------------------ cout << "\nmy dream cars are: \n"; for(int i = 0; i < size(my_cars); i++) { cout << my_cars[i] << "\n"; } // ------------------------------------ cout << "\nhow many dream cars do you have\n"; cin >> n; cin.ignore(); // remove "\n" before `getline` for(int i = 0; i < n; i++) { string new_car; cout << "\nenter your dream cars please\n"; //cin.ignore(); // remove "\n" before `getline` getline(cin, new_car); //getline(cin, new_car, ' '); //cin >> new_car; //cout << "\n"; cout << "adding car: " << new_car << "\n"; // to check if it get name with spaces your_cars.push_back(new_car); } // ------------------------------------ for(int my_idx = 0; my_idx < size(my_cars); my_idx++) { for(int your_idx = 0; your_idx < your_cars.size(); your_idx++) { if(your_cars.at(your_idx) == my_cars[my_idx]) { cout << "we both share a dream car! it is: " << your_cars.at(your_idx) << "\n"; break; } } } // ------------------------------------ for(int my_idx = 0; my_idx < size(my_cars); my_idx++) { bool matching = false; for(int your_idx = 0; your_idx < your_cars.size(); your_idx++) { if(your_cars.at(your_idx) == my_cars[my_idx]) { matching = true; break; } } if(!matching) { cout << "not your dream: " << my_cars[my_idx] << "\n"; } } // ------------------------------------ for(int your_idx = 0; your_idx < your_cars.size(); your_idx++) { bool matching = false; for(int my_idx = 0; my_idx < size(my_cars); my_idx++) { if(your_cars.at(your_idx) == my_cars[my_idx]) { matching = true; break; } } if(!matching) { cout << "not my dream: " << your_cars[your_idx] << "\n"; } } // ------------------------------------ return 0; }Result:
my dream cars are: MX5 560sec 560sl sl65 lp500 f40 demon 6*6 g550 cab u5000 slk500 sl600 lc79 sls slr valkyrie cls 911 targa firebird how many dream cars do you have 3 enter your dream cars please fiat uno adding car: fiat uno enter your dream cars please MX3 adding car: MX3 enter your dream cars please demon adding car: demon we both share a dream car! it is: demon not your dream: MX5 not your dream: 560sec not your dream: 560sl not your dream: sl65 not your dream: lp500 not your dream: f40 not your dream: 6*6 not your dream: g550 cab not your dream: u5000 not your dream: slk500 not your dream: sl600 not your dream: lc79 not your dream: sls not your dream: slr not your dream: valkyrie not your dream: cls not your dream: 911 targa not your dream: firebird not my dream: fiat uno not my dream: MX3