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();
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!
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.
//Directives
#include<iostream>
#include<string>
usingnamespace 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();
} elseif (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
}
elseif (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";
}elseif (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;
}