IF statement for multiple choice question
Sep 10, 2012 at 2:14am UTC
I am a beginner, learning C++ on my own as part of an extra curricular activity.
I am writing this code that is intended to ask the user for a preferred movie genre, load and read through a list of movie titles in a text file, and display a random movie title at the end. However I am having trouble with my
second IF statement. When ran, it won't give me an error, it will simply follow with "Press any key to continue.." What is wrong? This is how my code looks like
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
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
{
int number;
cout << "\n" ;
cout << "Welcome!\n" ;
cout << "\n" ;
cout << "What kind of movie are you looking for? \n" ;
cout << "\n" ;
cout << "1)Romance \n2)Comedy \n3)Action \n4)Drama \n5)Classic \n6)Sci-Fi \n " ;
cout << "\n" ;
cout << "Pick a number: " ;
cin >> number;
cout << "\n" ;
if (number == 1)
{
string getcontent;
ifstream openfile ("romance.txt" );
if (openfile.is_open())
{
getline (openfile, getcontent);
cout << getcontent << endl;
}
if (number == 2)
{
string getcontent;
ifstream openfile ("comedy.txt" );
if (openfile.is_open())
{
getline (openfile, getcontent);
cout << getcontent << endl;
}
}
}
return 0;
}
Last edited on Sep 10, 2012 at 2:15am UTC
Sep 10, 2012 at 2:28am UTC
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
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main ()
{
int number;
cout << "\n" ;
cout << "Welcome!\n" ;
cout << "\n" ;
cout << "What kind of movie are you looking for? \n" ;
cout << "\n" ;
cout << "1)Romance \n2)Comedy \n3)Action \n4)Drama \n5)Classic \n6)Sci-Fi \n " ;
cout << "\n" ;
cout << "Pick a number: " ;
cin >> number;
cout << "\n" ;
if (number == 1)
{
}
if (number == 2)
{
}
}
return 0;
}
The ending bracket for the first if statement is after the second if statement. Was this intentional?
Last edited on Sep 10, 2012 at 2:31am UTC
Sep 10, 2012 at 2:31am UTC
Geez! I still have to learn to look at my code carefully!!! That's probably today's most important lesson. Thanks man!
Last edited on Sep 10, 2012 at 2:36am UTC
Topic archived. No new replies allowed.