/*course: CS215 - 001 *Project: Lab 6 (As part of Project 1) *Purpose: Display the year of Super Bowl in Roman Numeral Representation * The first Super Bowl was hold in 1967 (at Los Angeles Memorial Coliseum) * For testing purpose, this program displays the Super Bowls in two-group of 50 years * (1) from year 2000 to year 2049; * (2) from year 5916 to year 5965. *Author: Christian Brewer */ #include <iostream> #include <string> using namespace std; //Turns a digit into a Roman numeral string roman_digit(int digit, string one, string five, string ten); //Returns a string form of a Roman Numeral. //(n must be between 1 and 3999) string roman_numeral(int n); int main() { const int START_SUPERBOWL = 1967; // The first Super Bowl was hold in 1967 (at Los Angeles Memorial Coliseum) const int FIRST_START = 2000; // The first group displays Super Bowl starting in year 2000 const int FIRST_END = 2049; // The first group displays Super Bowl ending in year 2099 const int SECOND_START = 5916; // The second group displays Super Bowl starting in year 5616 const int SECOND_END = 5965; // The second group displays Super Bowl ending in year 5965 // define the correct range for Roman Numerals: [MIN_ROMAN, MAX_ROMAN] const int MIN_ROMAN = 1; const int MAX_ROMAN = 3999; // The last Super Bowl which can be represented by Roman Numerals would be: START_SUPERBOWL + MAX_ROMAN - 1; const int GROUP = 10; // Layout the years in a group of 10 while(true){ //creating varibles maintaining the loop bool repeat = true; bool success_input = true; int year; //print out the Super Bowl information cout << "***********************************************************" << endl; cout << "* The Super Bowl is the annual final playoff game *" << endl; cout << "* of the NFL to determine the league champion. *" << endl; cout << "* The first Super Bowl took place on January 15, 1967. *" << endl; cout << "* Super Bowl I (Los Angeles Memorial Coliseum) --> 1967 *" << endl; cout << "* This Roman Numerals Convertor is written by Yi Pike. *" << endl; cout << "* If you had a time machine, which year of Super Bowl *" << endl; cout << "* would you want to attend (1967 - 5965) ? *" << endl; cout << "***********************************************************" << endl; cout << "Please enter the year you want to attend (click Q or q to quit):" << endl; //allow user input a year cin >> year; //execute if the cin fail if (cin.fail()) { // clean and reset cin cin.clear(); string input_to_check; cin >> input_to_check; cin.ignore(256,'\n'); //run if user input q or Q if (input_to_check == "q" || input_to_check == "Q") { cout << "Back to 2024, and have a great day!" << endl; repeat = false; } else { cout << "Please use four-digit number to represent a year (1967-5965)!"<< endl; success_input = false; } } //execute if the cin did not fail else { //clear the left over from cin cin.ignore(256,'\n'); } // if repeat is set to false, then break out of the loop if (repeat == false) { break; } //Run if the cin did not fail if(success_input){ cout << "The time machine will bring you to the year of " << year <<":" << endl; //Run if year is smaller than the START_SUPERBOWL if ( year < START_SUPERBOWL){ cout << "Wait!!! The year you enter is TOO EARLIER than the first Super Bowl!" << endl; } //Run if year is bigger than the largest year value able to be represent by roman numbers else if (year > SECOND_END){ cout << "Hold on!!! The year you enter is TOO BIG for Roman Numerals!" << endl; } //Run if the year is valid within the range else{ int roman_year = year - START_SUPERBOWL + 1; cout << "It is Super Bowl " << roman_numeral(roman_year) << endl; cout << "We will help you find out the result and other interesting information...next time:)" << endl; } } //give it a line space before next loop runs cout << endl; } } /* Purpose: convert the integer n to its corresponding Roman Numeral n must be between 1 and 3999 it returns string form of the Roman Numeral @param n int: representing the number to convert into Roman Numeral @return string: representing the corresponding Roman Numeral for n */ string roman_numeral(int n){ //Abstract each digit int first_digit = n % 10; int second_digit = n / 10 % 10; int third_digit = n / 100 % 10; int fourth_digit = n / 1000 % 10; //pass call roman_digit to get the corresponding roman number for each digit string unitRoman = roman_digit(first_digit, "I", "V", "X"); string tenthRoman = roman_digit(second_digit, "X", "L", "C"); string hundredRoman = roman_digit(third_digit, "C", "D", "M"); string thousandthRoman; for (int i = 0; i < fourth_digit; i++){ thousandthRoman += "M"; } //concatenate the string together string roman = thousandthRoman+hundredRoman + tenthRoman + unitRoman; // return the roman number return roman; } /* Turns a digit into a Roman numeral. @param digit: the digit to convert into Roman @param one: string representing the Roman numeral for ones @param five: string representing the Roman numeral for fives @param ten: string representing the Roman numeral for tens @return string representing the Roman number for digit */ string roman_digit(int digit, string one, string five, string ten){ string roman_num; //arrange the digit in pattern according to the value of the digit switch (digit){ case 1: roman_num = one; break; case 2: roman_num = one + one; break; case 3: roman_num = one + one + one; break; case 4: roman_num = one + five; break; case 5: roman_num = five; break; case 6: roman_num = five + one; break; case 7: roman_num = five + one + one; break; case 8: roman_num = five + one + one + one; break; case 9: roman_num = one + ten; break; default: break; } //return the roman number return roman_num; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}