switch case movie age whats wrong with code?

#include <iostream>
#include <cmath>

using namespace std;

void displayRatings ()

{



cout<< "1. G Rating."<<endl;



cout<< "2. PG Rating."<<endl;



cout<< "3. PG-13."<<endl;



cout<< "4. R." <<endl;

}



void sellTicket (bool guardianPresent, int movieRating, int age)

{

if (movieRating==1)

{



cout<< "You can see the movie."<<endl;

return;

}

if (guardianPresent==1)

{



cout<< "You can see the movie."<<endl;

return;

}



cout<< "Please enter you're age: ";

cin>> age;

switch (movieRating)

{

case 2:

if (age>=10)

{



cout<< "You can see the movie."<<endl;

return;

}

else

{



cout<< "You can not see the movie."<<endl;

return;

}

case 3:

if (age>=13)

{



cout<< "You can see the movie."<<endl;

return;

}

else

{



cout<<"You can not see the movie."<<endl;

return;

}

case 4:

if(age>=17)

{



cout<< "You can see the movie."<<endl;

return;

}

else

{



cout<< "You can not see the movie."<<endl;

return;

}

}




 

void

outputMin (double num1, double num2, double num3)

{

double minNum;

if(num1<num2)

{

minNum=num1;

}

else

{

minNum=num2;

}

if(minNum>num3)

{

minNum=num3;

}



cout<< "The minimum number is " <<minNum<< " ."<<endl;

}

 

int

main()

{

displayRatings ();

bool guardianPresent;

int movieRating;

int age;

double num1;

double num2;

double num3;



cout<< "Which rating do you wish to watch? (Please enter correstponding " <<

"number 1-4): ";

cin >>movieRating;



cout<< "Is there a guardian present? (Enter 1 for yes, and 0 for no) ";

cin >>guardianPresent;

sellTicket(guardianPresent,movieRating,age);

outputMin (num1,num2,num3);

return 0;

}
Last edited on
Your code is difficult to read with no code tags, all the extra blank lines, and no indentation.

What problem are you having with the code? Please be specific.

 
    sellTicket(guardianPresent,movieRating,age);

You pass age, but age is an uninitialized variable. You do prompt for age within sellTicket, but it's a poor practice to pass uninitialized variables. age should be a local variable inside sellTicket and not passed as a parameter.

What do num1, num2 and num3 and outputMin() have to do with this problem? num1, num2, num3 are also uninitialized variables.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.







/*
* File: main.cpp
* Author: Zac
*
* Created on October 6, 2015, 5:54 PM
*/

#include <iostream>
#include <cmath>

using namespace std;

void displayRatings ()

{



cout << "1. G Rating."<<endl;



cout << "2. PG Rating."<<endl;



cout << "3. PG-13."<<endl;



cout<< "4. R." <<endl;

}



void sellTicket (bool guardianPresent, int movieRating, int age)

{

if (movieRating==1)

{



cout<< "You can see the movie."<<endl;

return;

}

if (guardianPresent==1)

{



cout<< "You can see the movie."<<endl;

return;

}



cout<< "Please enter you're age: ";

cin>> age;

switch (movieRating)

{

case 2:

if (age>=10)

{



cout<< "You can see the movie."<<endl;

return;

}

else

{



cout<< "You can not see the movie."<<endl;

return;

}

case 3:

if (age>=13)

{



cout<< "You can see the movie."<<endl;

return;

}

else

{



cout<<"You can not see the movie."<<endl;

return;

}

case 4:

if(age>=17)

{



cout<< "You can see the movie."<<endl;

return;

}

else

{



cout<< "You can not see the movie."<<endl;

return;



}


{


void

outputMin(double num1, double num2, double num3)

}

double minNum;

if(num1<num2)

{

minNum=num1;

}

else

{

minNum=num2;

}

if(minNum>num3)

{

minNum=num3;

}



cout<< "The minimum number is " <<minNum<< " ."<<endl;

}

 

int

main()

{

displayRatings ();

bool guardianPresent;

int movieRating;

int age;

double num1;

double num2;

double num3;



cout<< "Which rating do you wish to watch? (Please enter correstponding " <<

"number 1-4): ";

cin >>movieRating;



cout<< "Is there a guardian present? (Enter 1 for yes, and 0 for no) ";

cin >>guardianPresent;

sellTicket(guardianPresent,movieRating,age);

outputMin (num1,num2,num3);

return 0;

}
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/175481/
You have been asked to use code tags. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
If you're not going to make the slightest bit of effort to make your posts readable, why should we spend the slightest bit of effort helping you?
I will not respond further until you apply code tags.
I have to agree with the previous comment. Too many variables are not initialised, so the outputMin() function will make no sense at all. Validation would be needed around any inputs to check that the values being entered are not outside the allowable ranges.

Also, personally, I wouldn't use multiple returns from a switch statement. Just use breaks at the end of each select/case condition.

However, it's not clear in what way the function is not working. And it's not clear what purpose the outputMin() function is serving here.
Topic archived. No new replies allowed.