Write a program that will prompt a user for a number between 1 and 3999 and prin
ID: 3741315 • Letter: W
Question
Write a program that will prompt a user for a number between 1 and 3999 and prints the Roman numeral equivalent. In other words, if the user enters 500, your program needs to print line number 500 of the roman.txt file. You must do input validation to prevent the user from entering a value less than 1 or greater than 3999. The file roman.txt contains Roman numerals from 1 to 3999, one on each line. In other words, line 1 contains the Roman numeral equivalent of 1. Line 2 contains the Roman numeral equivalent of 2. Line 388 contains the Roman numeral equivalent of 388, etc.: Contents of roman.txt : I ? line 1 II III IV V VI VII : MMMCMXCVIII MMMCMXCIX ? line 3999
Explanation / Answer
/**** This program works perfectly fine. Just place the roman.txt file and given the full path of this roman.txt ****/
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main(){
int number = 0;
cout<<"Please Enter a number between 1 and 3999"<<endl;
bool valid = false;
do{
cin>>number;
if(number >=1 && number <=3999){
valid = true;
}
if(!valid){
cout<<"Invalid Number. Please Enter the number between 1 to 3999"<<endl;
}
}while(!valid);
// read a file roman.txt make sure you have placed it at correct location. And provide the complete path for //the roman.txt
ifstream infile("E: oman.txt");
string line;
if(infile.is_open()){
for(int lineNo=1; getline(infile, line) && lineNo <= number; lineNo++){
if(lineNo==number){
cout<<"Roman number is"<<line<<endl;
}
}
}else{
cout<<"roman.txt File doesn't exist"<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.