Help with simple code!

Hello, I am currently learning C++ and the topic of this week is functions, the task is to make cout and cin variables in one function and print it out in another. In this code I get several errors, and can someone please look into it and explain what I have done wrong. Sorry about bad english, and lack of experience, this is not for a test or something btw. Thanks!

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
#include <iostream>

using namespace std;



// We got an error in the first funcion!
// Someone look into it! And tell me what was wrong, and how you fixed it!



string FirstQuestions(string FavorFood, string FavorAnimal, string FavorGame, string FavorHobby) {


      cout << "What is your favor food?" << endl ;
        cin >> FavorFood ;


      cout << "What is your favor animal?" << endl ;
        cin >> FavorAnimal ;

      cout << "What is your favor game?" << endl ;
        cin >> FavorGame;

      cout << "What is your favor Hobby?" << endl ;
        cin >> FavorHobby;

           cout << "Thank you, I will now look into your anwsers \n\n\n\n" ;


   return FavorAnimal, FavorFood, FavorGame, FavorHobby ;


}






int main (string FavorAnimal, string FavorFood, string FavorGame, string FavorHobby )
{

string FirstQuestions()

   cout << "Since your favor food is " << FavorFood << " you like it!" << endl << endl;

   cout << "Since your favor animal is " << FavorAnimal << " It's probebly fluffy. :3 " << endl << endl ;


           if (FavorHobby == "gaming") {

           cout << "My favor hobby is gaming as well!" << endl << endl ;

               } else {

           cout << "Since your favor hobby is " << FavorHobby << ", I beth it is fun." << endl << endl ;


           }



   cout << "Since your favor game is " << FavorGame << ", I beth it is awesome!" << endl << endl ;



    return 0;
}
#include <string>


You cannot return multiple values as you attempt in FirstQuestions. When the prototype is

string FirstQuestions(...)

the return type is string. You may return one string, and one string only.


return FavorAnimal, FavorFood, FavorGame, FavorHobby ;

The way the comma operator works, that is equivalent to: return FavorHobby ;

You can't decide main takes a bunch of strings as parameters. that just doesn't work.

The line string FirstQuestions() in main will be seen by the compiler as a function declaration.

You never actually call the function.

The function's parameters don't make sense unless they are references. If they are references, the return type doesn't make sense.

I would suggest taking a simpler approach. Functions should usually be more general in nature:

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

// A function taking a prompt to display
// and return a line of input obtained
// from the user.
string get_input( string prompt ) 
{
	string user_input ;
	cout << prompt << endl ;
	getline(cin, user_input) ;

	return user_input ;
}

int main()
{
	string fav_animal = get_input( "What is your favorite animal?" ) ;
	string fav_hobby  = get_input( "What is your favorite hobby?" ) ;

	// do stuff w/ fav_animal/hobby.

}




Thank you! I got it to work! :D
if the question is anwered and everything works, mark this as solved, so people don't go here to try to help, read the whole topic, and then realise it's alredy answered!
Topic archived. No new replies allowed.