Splitting a function into 3 more functions.

I am trying to understand functions, or how to make one that calls another function, and i am trying to use as many functions as possible in this code.
I want to learn how i can split into 3 more functions and call from main the 3 functions. Any suggestions?

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
//Pointers to structures
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>

using namespace std;

//void func1 ();
//void func2 ();
//void func3 ();
struct movies_t {
       string title;
       int year;
};


int main () {
    string mystr;
    
    movies_t amovie;
    movies_t * pmovie;
    pmovie = &amovie;
 // split 1  
 //  func1 (); 
    cout << "Enter title: ";
    getline (cin, pmovie ->title);     
 // split 2  
 //  func2 ();                           
    cout << "Enter year: ";
    getline (cin,mystr);
    (stringstream) mystr >>pmovie ->year;
 // split 3 
 // func3 ();
    cout << "\nYou have entered:\n";
    cout << pmovie->title;
    cout <<" (" << pmovie->year << ")\n";
 //    
    getch();
    return 0;
}

//void func1 () {
//}
//void func2 () {
//}
//void func3 () {
//}
// 


Last edited on
What's holding you back? Do you know how to use functions? If not, study the tutorial and then have a go: http://www.cplusplus.com/doc/tutorial/functions/
This is how i think it should be.

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
//Pointers to structures
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>

using namespace std;

void func1();
void func2();
void func3();

struct movies_t {
       string title;
       int year;
};


int main () {
    string mystr;
    
    movies_t amovie;
    movies_t * pmovie;
    pmovie = &amovie;
 //
   func1(); 
 //                                
    func2();
 //  
    func3();
 //    
    getch();
    return 0;
}
    
void func1(){
      cout << "Enter title: ";
      getline (cin, pmovie ->title);
      return 0;
}
void func2(){
     cout << "Enter year: ";
     getline (cin,mystr);
     (stringstream) mystr >>pmovie ->year;
     return 0;
}
void func3(){
      cout << "\nYou have entered:\n";
      cout << pmovie->title;
      cout <<" (" << pmovie->year << ")\n";
      return 0;
}
Last edited on
I've not studied that in depth, Does it compile and run ok?

One thing, you can't return anything from a function declared as void, so return 0; is incorrect. Sometimes a plain return; might be useful, for example in an if statement.
Last edited on
It does not compile, in my functions i think i must pass something in the ()
func1 has a string, func2 has a int and func3 has both a string and an int. I think i have to edit that to reflect that data.
Ya, you'll have to pass the pointer to the functions then use the format pointer->member to access the structure members, either to input data to them or to read data out of them. Then to call the function you just use func1(pmovie)
Last edited on
How should it be?

//
void func1(pmovie);
//
int main () {
//
func1(*pmovie);
//
void func1(pmovie){
}
Last edited on
This is what i have so far but it is not compiling.
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
//Pointers to structures
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>

using namespace std;

void func1(pmovie);
void func2(pmovie);
void func3(pmovie);

struct movies_t {
       string title;
       int year;
};

int main () {
    string mystr;
    
    movies_t amovie;
    movies_t * pmovie;
    pmovie = &amovie;
//  
    func1(*pmovie); 
//                                
    func2(*pmovie);
//  
    func3(*pmovie);
//    
    getch();
    return 0;
}
    
void func1(pmovie){
      cout << "Enter title: ";
      getline (cin, pmovie ->title);
      return ;
}
void func2(pmovie){
     cout << "Enter year: ";
     getline (cin,mystr);
     (stringstream) mystr >> pmovie ->year;
     return ;
}
void func3(pmovie){
      cout << "\nYou have entered:\n";
      cout << pmovie->title;
      cout <<" (" << pmovie->year << ")\n";
      return ;
}
'pmovie' in the function parameter list needs to have a type. Just add 'movies_t*' in front of it.
I am still getting a lot of errors.



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
//Pointers to structures
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>

using namespace std;

void func1(movies_t * pmovie);
void func2(movies_t * pmovie);
void func3(movies_t * pmovie);

struct movies_t {
       string title;
       int year;
};

int main () {
    string mystr;
    
    movies_t amovie;
    movies_t * pmovie;
    pmovie = &amovie;
//  
    func1( pmovie); 
//                                
    func2( pmovie);
//  
    func3( pmovie);
//    
    getch();
    return 0;
}
    
void func1(movies_t * pmovie){
      cout << "Enter title: ";
      getline (cin, pmovie ->title);
      return ;
}
void func2(movies_t * pmovie){
     cout << "Enter year: ";
     getline (cin,mystr);
     (stringstream) mystr >> pmovie ->year;
     return ;
}
void func3(movies_t * pmovie){
      cout << "\nYou have entered:\n";
      cout << pmovie->title;
      cout <<" (" << pmovie->year << ")\n";
      return ;
}
And the errors are?
Edited the code a little.
Now the errors are

https://dl.dropbox.com/u/8616586/error.PNG


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
//Pointers to structures
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>

using namespace std;

struct movies_t {
       string title;
       int year;
};

void func1(movies_t * pmovie);
void func2(movies_t * pmovie);
void func3(movies_t * pmovie);

int main () {
    string mystr;
    
    movies_t amovie;
    movies_t * pmovie;
    pmovie = &amovie;
//  
    func1( pmovie); 
//                                
    func2( pmovie);
//  
    func3( pmovie);
//    
    getch();
    return 0;
}
    
void func1(movies_t * pmovie){
      cout << "Enter title: ";
      getline (cin, pmovie ->title);
      return ;
}
void func2(movies_t * pmovie){

     cout << "Enter year: ";
     getline (cin,mystr);
     (stringstream) mystr >> pmovie ->year;
     return ;
}
void func3(movies_t * pmovie){
      cout << "\nYou have entered:\n";
      cout << pmovie->title;
      cout <<" (" << pmovie->year << ")\n";
      return ;
}
Last edited on
func2() is using something called mystr, but that isn't defined inside func2()'s scope.
Yes but i do not know how to add that so it can be used by func2
You just need a random temporary string to store some input data, so why not just declare a local variable inside func2()?
I have a string mystr inside main, cant i use that?
Last edited on
This concludes this exercise, i have pasted the final code for further reference. The initial code was taken from the cplusplus tutorial, pdf, page 80.



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
//Pointers to structures
#include <iostream>
#include <string>
#include <sstream>
#include <conio.h>

using namespace std;

struct movies_t {
       string title;
       int year;
};

void func1(movies_t * pmovie);
void func2(movies_t * pmovie);
void func3(movies_t * pmovie);
                              //prototype must be under struct
int main () {
//  string mystr;       //*remove here
    
    movies_t amovie;
    movies_t * pmovie;
    pmovie = &amovie;
//  
//      cout << "Enter title: ";
//      getline (cin, pmovie ->title);
    func1( pmovie); 
// 
//      cout << "Enter year: ";
//      getline (cin,mystr);
//     (stringstream) mystr >>pmovie ->year;                               
    func2( pmovie);
//  
//    cout << "\nYou have entered:\n";
//    cout << pmovie->title;
//    cout <<" (" << pmovie->year << ")\n";
    func3( pmovie);

//    
    getch();
    return 0;
}
    
void func1(movies_t * pmovie){
      cout << "Enter title: ";
      getline (cin, pmovie ->title);
      return ;   //added return
}
void func2(movies_t * pmovie){
     string mystr;           //*added here
     cout << "Enter year: ";
     getline (cin,mystr);
     (stringstream) mystr >> pmovie ->year;
     return ;   //added return
}
void func3(movies_t * pmovie){
      cout << "\nYou have entered:\n";
      cout << pmovie->title;
      cout <<" (" << pmovie->year << ")\n";
      return ;   //added return
}


Thanks to those who helped.
Last edited on
Topic archived. No new replies allowed.