Nov 13, 2012 at 2:40am UTC
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 Nov 13, 2012 at 3:05am UTC
Nov 13, 2012 at 3:16am UTC
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 Nov 13, 2012 at 3:16am UTC
Nov 13, 2012 at 3:21am UTC
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 Nov 13, 2012 at 3:22am UTC
Nov 13, 2012 at 3:25am UTC
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.
Nov 13, 2012 at 3:30am UTC
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 Nov 13, 2012 at 3:31am UTC
Nov 13, 2012 at 3:40am UTC
How should it be?
//
void func1(pmovie);
//
int main () {
//
func1(*pmovie);
//
void func1(pmovie){
}
Last edited on Nov 13, 2012 at 4:00am UTC
Nov 13, 2012 at 4:09am UTC
'pmovie' in the function parameter list needs to have a type. Just add 'movies_t*' in front of it.
Nov 13, 2012 at 5:11am UTC
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 Nov 13, 2012 at 5:14am UTC
Nov 13, 2012 at 5:19am UTC
func2() is using something called mystr, but that isn't defined inside func2()'s scope.
Nov 13, 2012 at 5:20am UTC
Yes but i do not know how to add that so it can be used by func2
Nov 13, 2012 at 5:22am UTC
You just need a random temporary string to store some input data, so why not just declare a local variable inside func2()?
Nov 13, 2012 at 5:27am UTC
I have a string mystr inside main, cant i use that?
Last edited on Nov 13, 2012 at 5:27am UTC
Nov 13, 2012 at 5:42am UTC
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 Nov 13, 2012 at 5:46am UTC