functions issue

Im trying to run a program that lets the user enter a viewing code and title and then get back what they owe for that code and get the information they entered back. right now it only lets me enter title and code but terminates there. What can I do?

/*Program: prog2B.cpp
By: Irvin Florencia
Last Modified: October 20,2017
Purpose: Display price of viewings.
*/

//Directives
#include<iostream>
#include<string>

using namespace std;

void tv();
void brandNew();
int oldMovies(int year);
void displayResults();

int main()
{
char code;
string title, cost, viewing;
int year ;
void brandNew();
int oldMovies(int year);
void tv();

cout << "Enter Title: " ;
getline (cin, title);
cout << "Enter code: " ;
cin >> code ;

switch (code)
{
case 't': case 'T':
void tv();
break;

case 'n': case 'N':
void brandNew();
break;

case 'm': case 'M':
int oldMovies(int year);
break;
}
return 0;
}

void brandNew()
{
string cost, viewing;
cost = "$6.99" ;
viewing = "New Release"
void displayResults();
}
void tv()
{
string cost, viewing;
cost = "FREE";
viewing = "TV";
void displayResults();
}

int oldMovies (int year)
{
string cost, viewing;
cout << "Enter year: " ;
cin >> year ;

if (year < 1960)
{ cost ="$2.99";
viewing ="Non new release";
void displayResults();
}

else if (year <1979)
{ cost ="$3.99";
viewing = "Non new release";
void displayResults();
}

else if ( year < 1999)
{ cost = "$4.99" ;
viewing = "Non new release";
void displayResults();
}

else if ( year > 2000)
{ cost = "$5.99";
viewing = "Non new release";
void displayResults();
}

else
cout << "Error" << endl;

}

void displayResults(string)
{
string cost, title, viewing;

cout << "Title: " << title << endl;
cout << "Viewing: " << viewing << endl;
cout << "Amount due: " << cost << endl;
}

Alright here are a few things I noticed.

You defined your functions above your main() so you do not need to have them redfined in main again:

From this
1
2
3
4
5
6
7
8
9
int main()
{
	char code;
	string title, cost, viewing;
	int year;
	void brandNew();
	int oldMovies(int year);
	void tv();


To This
1
2
3
4
5
6
int main()
{
	char code;
	string title, cost, viewing;
	int year;


Next functions are made to do something so keep a function to that one scope which means dont be trying to call a function inside another function like this. Instead have a function return some value and pass that to another function.

So take this:

1
2
3
4
5
6
7
	if (year < 1960)
	{
		cost = "$2.99";
		viewing = "Non new release";
		void displayResults();
	}


and do something like:

1
2
3
4
5
6
7
8
9
10
11


	if (year < 1960)
	{
		cost = "$2.99";
		return cost;
	}
}

displayResults(cost); //this line would be in main()


Then change displayResults to:

1
2
3
4
void displayResults(data){

cout << "The total cost of this show is " << cost <<endl;
}


with your displayresults function the variables of cost,title,viewing will never be set because you are trying to define them inside a function that is outside of its scope you have to pass data back and fourth like I showed above (or use pointers). Anyway if you clean your code up a little with some of the things I mentioned it will be much easier to diagnose your issue!
Last edited on
What I have done here is created working code that is similar to what you are trying to do. I hope it helps you understand things a little more. What I wrote is not very efficient or practical but it is very basic and my goal is to help teach you so any questions feel free to ask.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83

//Directives
#include<iostream>
#include<string>

using namespace std;

//these functions require data be given to them and then return data
int pick(char);
string decideChoice(int);

//these functions dont require any data be given to them and they dont return anything
void displayMovie();
void displayTV();


int main() {

	//create a variable to store the users choice in
	char choice;
	//create a variable to store the information from pick function
	int picked;
	//create a variable to store the information from decide choice function!
	string concludedChoice;

	//Ask the user what they want to see
	cout << "Would you like to see the movie selection (M) or TV selection (T)";
	cin >> choice;
	//pass the users entry into our function and let it return its value to the variable picked
	picked = pick(choice);
	//pass the variable returned to the function and let it return its value to the variable concludedchoice
	concludedChoice = decideChoice(picked);

	if (concludedChoice == "movies") {
		displayMovie();
	} else if (concludedChoice == "TV Shows") {
		displayTV();
	}else {
		cout << "You selected an invalid option. " << endl;
	}
	
	return 0;
}

//return 0 for invalid choice 1 for movies and 2 for tv shows
int pick(char c) {
	if (c == 'M' || c == 'm') {
		return 1; //user selected movies
	}
	else if (c == 'T' || c == 't') {
		return 2; //user selected tv shows
	}
	else {
		return 0; //user selected an invalid choice
	}
}

//now decided what choice the user made
string decideChoice(int choice) {
	if (choice == 1){
		return "movies";
	}else if (choice == 2) {
		return "TV Shows";
	}
	else {
		return "Invalid";
	}
 }

void displayMovie() {

	cout << "1. Haunted Frieght!    ....  $6.99 " << endl;
	cout << "2. Knocked up!         ....  $8.99 " << endl;
	cout << "3. Pineapple Express!  ....  $10.00" << endl;
}

void displayTV() {

	cout << "1. Everybody Hates Raymond    ....  $6.99 " << endl;
	cout << "2. Family Guy                 ....  $8.99 " << endl;
	cout << "3. The Simpsons               ....  $10.00" << endl;
}
Last edited on
Topic archived. No new replies allowed.