I am writing a C++ program that converts between Arabic and Roman numbering systems. I wrote one program that converts Arabic to Roman and have another program that converts Roman to Arabic.
The problem is that I can't figure out how to merge them into one single program so that the user can input an Arabic or Roman number and as a result, the program would convert said number to the other.
My question is HOW can I merge these two programs into one?
Code for Roman to Arabic
#include <iostream>
using namespace std;
int main()
{
char roman_Numeral;
int arabic_Numeral = 0;
cout << "Enter the Roman Numeral in Capital letters (e.g. CCXIX) : ";
while (cin.get(roman_Numeral))
{
if (arabic_Numeral > 100)
{
cout << "\nInvalid Value. Number must be between I and C" << endl;
return 0;
}
else if (roman_Numeral == 'C')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D')
{
arabic_Numeral = arabic_Numeral - 100;
}
else
{
arabic_Numeral = arabic_Numeral + 100;
}
}
else if (roman_Numeral == 'L')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C')
{
arabic_Numeral = arabic_Numeral - 50;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 50;
continue;
}
}
else if (roman_Numeral == 'X')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C' || roman_Numeral == 'L')
{
arabic_Numeral = arabic_Numeral - 10;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 10;
continue;
}
}
else if (roman_Numeral == 'V')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C' || roman_Numeral == 'L'
|| roman_Numeral == 'X')
{
arabic_Numeral = arabic_Numeral - 5;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 5;
continue;
}
}
else if (roman_Numeral == 'I')
{
roman_Numeral = cin.peek();
if (roman_Numeral == 'M' || roman_Numeral == 'D'
|| roman_Numeral == 'C' || roman_Numeral == 'L'
|| roman_Numeral == 'X' || roman_Numeral == 'V')
{
arabic_Numeral = arabic_Numeral - 1;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 1;
continue;
}
}
else
break;
}
cout << arabic_Numeral << endl;
return 0;
}
Code for Arabic to Roman
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
#define MAX_INPUT 100 // These constants hold high and low integer numbers,
#define MIN_INPUT 1
#define ARRAY_SIZE 4 // and the array size declarator.
string convert(int digit, string low, string mid, string high);
int main()
{
string answers[ARRAY_SIZE] = { "", "", "", "" }; //An array of string to hold the output from the convert function.
int accumulator = 0; // Variable to hold number of arabic numbers converted.
int userNum = 0;
string strUserNum;
do
{
cout << "";
cout << "Enter an arabic number between 1 and 100: ";
cin >> strUserNum;
userNum = std::stoi(strUserNum);
if (userNum == 0 || userNum > MAX_INPUT)
{
cout << "\nInvalid Value. Number must be between 1 and 100" << endl;
return 0;
}
else if (userNum == 'end')
{
cout << "Exiting program:";
break;
}
int thous = userNum / 1000;
int hund = userNum % 1000 / 100;
int tens = userNum % 100 / 10;
int ones = userNum % 10 / 1;
answers[0] = convert(thous, "M", "M", "M");
answers[1] = convert(hund, "C", "D", "M");
answers[2] = convert(tens, "X", "L", "C");
answers[3] = convert(ones, "I", "V", "X");
cout << answers[0] << answers[1] << answers[2];
cout << answers[3] << endl;
cout << endl;
break;
} while (userNum != 'end');
system("PAUSE");
return 0;
}
string convert(int digit, string low, string mid, string high)
{
if (digit == 1)
{
return low;
}
if (digit == 2)
{
return low + low;
}
if (digit == 3)
{
return low + low + low;
}
if (digit == 4)
{
return low + mid;
}
if (digit == 5)
{
return mid;
}
if (digit == 6)
{
return mid + low;
}
if (digit == 7)
{
return mid + low + low;
}
if (digit == 8)
{
return mid + low + low + low;
}
if (digit == 9)
{
return low + high;
}
if (digit == 0)
{
return "";
}
}
Put code (that inside the main block) for Roman to Arabic into void procedureRomanToArabic()
The same for Roman to Arabic. Put it into void procedureArabicToRoman().
Remove remove ArabicToRoman do-while and move it to main.
void procedureRomanToArabic(){
char roman_Numeral;
int arabic_Numeral = 0;
//...
cout << arabic_Numeral << endl;
}
void procedureArabicToRoman()
{
string answers[ARRAY_SIZE] = { "", "", "", "" }; //An array of string to hold the output from the convert function.
int accumulator = 0; // Variable to hold number of arabic numbers converted.
int userNum = 0;
string strUserNum;
//remove do while here
cout << "";
cout << "Enter an arabic number between 1 and 100: ";
cin >> strUserNum;
//...
answers[2] = convert(tens, "X", "L", "C");
answers[3] = convert(ones, "I", "V", "X");
cout << answers[0] << answers[1] << answers[2];
cout << answers[3] << endl;
cout << endl;
}
int main(){
char type;
do{
cout<<"2 for Arabic to Roman, 1 for Roman to Arabic, or anything else for exit";
cin>>type;
if(type=='1')
procedureArabicToRoman();
else if(type =='2')
procedureRomanToArabic();
else break; //end program
} while(true);
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
A few time ago I made a game similar to Gomoku in C++ that is taking between two players.
Now I want to make it Player vs Computer.
I tried to do it in simplest way, by making a function of computer to pick a random cell but I still haven't succeeded.
I understood that in order of getting a random number I can use rand() and for a letter something like this:
char letters[] = "abcdefghijklmnopqrstuvwxyz";
char x = letters[rand() % 26];
Can someone help me and describe how to implement a computer player?
This is my implementation so far:
#include <iostream>
#include <iomanip>
using namespace std;
void print_table(int x[][15]) {
system("cls");
for (int i = 0; i < 15; i++) {//the loop that use to print out the english character row
if (i == 0)
cout << setw(4) << "A";
else if (i == 1)
cout << " B";
else if (i == 2)
cout << " C";
else if (i == 3)
cout << " D";
else if (i == 4)
cout << " E";
else if (i == 5)
cout << " F";
else if (i == 6)
cout << " G";
else if (i == 7)
cout << " H";
else if (i == 8)
cout << " I";
else if (i == 9)
cout << " J";
else if (i == 10)
cout << " K";
else if (i == 11)
cout << " L";
else if (i == 12)
cout << " M";
else if (i == 13)
cout << " N";
else if (i == 14)
cout << " O";
else if (i == 15)
cout << " P";
}
cout << endl;
for (int i = 0; i < 15; i++) {
cout << setw(2) << i;//print out the row number
for (int j = 0; j < 15; j++) {//print out the board game.
if (x[i][j] == 0) {//the inital value is 0, so when the block is 0 then print out the '.'
cout << " .";
}
else if (x[i][j] == 1) {//when the player O input the block then the value will adding one then if check the block is one then output the 'o'
cout << " O";
}
else if (x[i][j] == 2) {//when the player X input the block then the value will adding two then if check the block is two then output the 'x'
cout << " X";
}
}
cout << endl;
}
}
int check_player(int p) {
if (p == 1) {//change the player everytime before the next loop compile
p++;
}
else {
p--;
}
return p;
}
void input_value(int &t, int &n, int p, int x[][15]) {
char eng;
int number;
do {//the loop that ask for the user input the location.
cout << "player ";
if (p == 1) {
cout << "O";
}
else {
cout << "X";
}
cout << ", make a move: ";
cin >> eng;//input the location
cin >> number;
if (eng == 'A')//change the character to different number
t = 0;
else if (eng == 'B')
t = 1;
else if (eng == 'C')
t = 2;
else if (eng == 'D')
t = 3;
else if (eng == 'E')
t = 4;
else if (eng == 'F')
t = 5;
else if (eng == 'G')
t = 6;
else if (eng == 'H')
t = 7;
else if (eng == 'I')
t = 8;
else if (eng == 'J')
t = 9;
else if (eng == 'K')
t = 10;
else if (eng == 'L')
t = 11;
else if (eng == 'M')
t = 12;
else if (eng == 'N')
t = 13;
else if (eng == 'O')
t = 14;
if (!(eng >= 'A'&&eng <= 'M') || !(number >= 0 && number <= 14) || x[number][t] != 0) {//when the input wrong, output the statement to ask anouther input and loop again.
cout << "Invaid input, Try again!" << endl;
continue;
}
else {//if no problem then this input loop is break and jump to the next statement
break;
}
} while (1);//Because it will break as well so the do-while loop is no any requirement
n = number;
}
int main() {
const int num = 15;//the number for constant the array row and column value
char check_e;//for the user input the column
int R[num][num] = { 0 }, check_n, player = 1, buger = 0, transfer, playerO_win = 0, playerX_win = 0, draw = 0, check_draw;//the variable that for user input or checking the game statment
do {//launch the loop for the user input again and again
check_draw = 0;//reset the checking of draw
print_table(R);
input_value(transfer, check_n, player, R);
R[check_n][transfer] += player;//change the value according the player's input and the player name.
for (int i = 0; i < num; i++) {
for (int j = 0; j < num; j++) {
if (i <= 8 && R[j][i] != 0 && (R[j][i] == R[j][i + 1] && R[j][i] == R[j][i + +2] && R[j][i] == R[j][i + 3] && R[j][i] == R[j][i + 4])) {//the checking for the row bingo
if (R[j][i] == 1) {
playerO_win++;
break;
}
else {
playerX_win++;
break;
}
}
else if (j <= 8 && R[j][i] != 0 && (R[j][i] == R[j + 1][i] && R[j][i] == R[j + 2][i] && R[j][i] == R[j + 3][i] && R[j][i] == R[j + 4][i])) {//the checking for the column bingo
if (R[j][i] == 1) {
playerO_win++;
break;
}
else {
playerX_win++;
break;
}
}
else if (j <= 8 && i <= 8 && R[j][i] != 0 && (R[j][i] == R[j + 1][i + 1] && R[j][i] == R[j + 2][i + 2] && R[j][i] == R[j + 3][i + 3] && R[j][i] == R[j + 4][i + 4])) {//the checking for the \ situation.
if (R[j][i] == 1) {
playerO_win++;
break;
}
else {
playerX_win++;
break;
}
}
else if ((j >= 4 || i >= 4 || i <= 8) && R[j][i] != 0 && (R[j][i] == R[j - 1][i + 1] && R[j][i] == R[j - 2][i + 2] && R[j][i] == R[j - 3][i + 3] && R[j][i] == R[j - 4][i + 4])) {//the checking for the / situation
if (R[j][i] == 1) {
playerO_win++;
break;
}
else {
playerX_win++;
break;
}
}
for (int i = 0; i < num; i++) {//the loop for checking the draw
for (int j = 0; j < num; j++) {//this loop will check for every time compilation.
if (R[j][i] == 0)//when there are any empty block then the check_draw will adding, the draw situation is the check_draw be 0
check_draw++;
}
}
if (check_draw == 0) {//when the check_draw equal to 0 which mean the situation is no empty block
draw++;
break;
}
}
if (playerO_win != 0 || playerX_win != 0 || draw == 1)//break the second loop
break;
}
if (playerO_win == 1 && playerX_win == 0) {// when the player win print the block game again and print out the win statement
print_table(R);
cout << "player O wins!" << endl;
break;
}
else if (playerX_win == 1 && playerO_win == 0) {//the other player win the game
print_table(R);
cout << "player X wins!" << endl;
break;
}
else if (draw == 1) {//the draw block game print
print_table(R);
cout << "Draw game!" << endl;
break;
}
player = check_player(player);
} while (1);//in fact it is no need for the loop statement, because most of the situation will have a break statement for out of the loop
return 0;
}
Here's how I would implement it probably based on your initial implementation:
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
#define SIZE 10
#define LINE_LENGTH 4
#define COMPUTER_O_PLAYER true
#define COMPUTER_X_PLAYER false
void print_board(unsigned char x[SIZE][SIZE]) {
cout << " ";
for (int i = 0; i < SIZE; i++) { // The loop that use to print out the english character row.
cout << (char) ('A' + i) << " ";
}
cout << endl;
for (int i = 0; i < SIZE; i++) {
cout << setw(2) << i; // Print out the row number.
for (int j = 0; j < SIZE; j++) {//print out the board game.
if (x[i][j] == 0) { // Unoccupied tile.
cout << " .";
} else if (x[i][j] == 1) { // The tile belongs to X.
cout << " X";
} else { // The tile belongs to O.
cout << " O";
}
}
cout << endl;
}
}
void get_position(int &x, int &y, bool player, unsigned char board[SIZE][SIZE]) {
char eng;
// The loop that ask for the user input the location.
do {
cout << "Player " << (player ? "X" : "O") << ", make a move: ";
cin >> eng;
y = toupper(eng) - 'A';
cin >> x;
if (!(x >= 0 && x < SIZE) || !(y >= 0 && y < SIZE) || board[x][y] != 0) {
// When the input wrong, output the statement to ask another input and loop again.
cout << "Invalid input, Try again!" << endl;
continue;
} else { // If no problem then this input loop is break and jump to the next statement.
break;
}
} while (true);
}
void get_random_position(int &x, int &y, bool player, unsigned char board[SIZE][SIZE]) {
do {
x = rand() % SIZE;
y = rand() % SIZE;
} while ((!(x >= 0 && x < SIZE) || !(y >= 0 && y < SIZE) || board[x][y] != 0));
cout << "Player " << (player ? "X" : "O") << " chose: " << (char) ('A' + y) << x << endl;
}
unsigned int
count_in_direction(unsigned char board[SIZE][SIZE], unsigned int x, unsigned int y, short int m_x, short int m_y) {
unsigned int count = 0;
unsigned char tile = board[x][y];
while (((x + m_x >= 0) && (x + m_x < SIZE)) && ((y + m_y >= 0) && (y + m_y < SIZE)) &&
(board[x + m_x][y + m_y] == tile)) {
x += m_x;
y += m_y;
count++;
}
return count;
}
bool full_line(unsigned char board[SIZE][SIZE], unsigned int x, unsigned int y) {
const short int directions[4][2] = {{1, 0},
{1, -1},
{0, 1},
{1, 1}};
for (const auto &direction : directions) {
if (LINE_LENGTH - 1 <= (count_in_direction(board, x, y, direction[0], direction[1]) +
count_in_direction(board, x, y, -direction[0], -direction[1])))
return true;
}
return false;
}
int main() {
// The variable that for user input or checking the game state.
unsigned char board[SIZE][SIZE] = {0};
int x, y;
bool player = true;
bool draw;
srand(time(nullptr));
// Run the game.
do {
print_board(board);
if ((player && COMPUTER_X_PLAYER) || (!player && COMPUTER_O_PLAYER)) {
get_random_position(x, y, player, board);
} else {
get_position(x, y, player, board);
}
board[x][y] = player ? 1 : 2;
if (full_line(board, x, y)) {
print_board(board);
cout << "player " << (player ? "X" : "O") << " wins!" << endl;
break;
}
draw = true;
for (int k = 0; (k < SIZE) && draw; k++) {
for (int l = 0; (l < SIZE) && draw; l++) {
if (board[k][l] == 0) draw = false;
}
}
if (draw) {
print_board(board);
cout << "Draw game!" << endl;
break;
}
player = !player;
} while (true);
return 0;
}
Please take a deep look in this implementation and internalize its approach, it also support both players as user, user and computer, and both computer.
Some insights:
Don't use long if ... else when there are simpler ways.
Use the correct data types for variables.
Use indicative variables naming.
Use loops where possible, again, not long if ... else.
Keep the code well formatted.
Use #define to define global constants to use, for easier changes later and readability.
You can indeed use rand, but you don't need to get a character, you can get an index straight away and avoid the conversion.
for class I have to create a code so that it could ask the user for an integer between 1-99 and be able to print that integer in roman numerals. Issue is after creating my code it would only print the numbers fully up to 39. Once it hits 40 it give's no Roman Numeral output and then from 41-99 it wont print the tenth place value(Ex. 45 will come out as V). Here's my code at the moment.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num;
int tenum;
int remnum;
string romnum;
string fromnum;
cout << "Enter a number between 1 and 99. \n";
cin >> num;
tenum = num / 10;
remnum = num % 10;
if (tenum == 9)
{
fromnum == "XC";
}
else if (tenum == 8)
{
fromnum == "LXXX";
}
else if (tenum == 7)
{
fromnum == "LXX";
}
else if (tenum == 6)
{
fromnum == "LX";
}
else if (tenum == 5)
{
fromnum == "L";
}
else if (tenum == 4)
{
fromnum == "XL";
}
else if (tenum == 3)
{
fromnum = "XXX";
}
else if (tenum == 2)
{
fromnum == "XX";
}
else if (tenum == 1)
{
fromnum == "X";
}
if (remnum == 9)
{
romnum = "IX";
}
else if (remnum == 8)
{
romnum = "VIII";
}
else if (remnum == 7)
{
romnum = "VII";
}
else if (remnum == 6)
{
romnum = "VI";
}
else if (remnum == 5)
{
romnum = "V";
}
else if (remnum == 4)
{
romnum = "IV";
}
else if (remnum == 3)
{
romnum = "III";
}
else if (remnum == 2)
{
romnum = "II";
}
else if (remnum == 1)
{
romnum = "I";
}
cout << tenum << endl;
cout << remnum << endl;
cout << fromnum;
cout << romnum << endl;
return 0;
}
You've mixed up == and =
change lines like
fromnum == "XL"
to
fromnum = "XL"
Demo
(I used a switch statement for brevity)
same answer with: Converting integer to Roman Numeral
#include <iostream>
#include <string>
std::string to_roman(int value)
{
struct romandata_t { int value; char const* numeral; };
static romandata_t const romandata[] =
{ 1000, "M",
900, "CM",
500, "D",
400, "CD",
100, "C",
90, "XC",
50, "L",
40, "XL",
10, "X",
9, "IX",
5, "V",
4, "IV",
1, "I",
0, NULL }; // end marker
std::string result;
for (romandata_t const* current = romandata; current->value > 0; ++current)
{
while (value >= current->value)
{
result += current->numeral;
value -= current->value;
}
}
return result;
}
int main()
{
for (int i = 1; i <= 4000; ++i)
{
std::cout << to_roman(i) << std::endl;
}
}
this code converts up to 1000. just take what you need and you are good to go!
from http://rosettacode.org/wiki/Roman_numerals/Encode#C.2B.2B
I am currently developing some code that is used to convert an inputted integer to ROMAN numerals. It is working how it should be except for the cases where I am using the character I, it isn't adding them up correctly. I have done the same syntax as the other characters which work so I am baffled as to why it doesn't work for the "I" character. Here is a snippet of what I have:
else if(roman_Numeral == 'V')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 2) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 5;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 5;
continue;
}
}
else if(roman_Numeral == 'I')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral), 2 != std::string::npos)
{
arabic_Numeral = arabic_Numeral -1;
break;
}
else
{
arabic_Numeral += 1 ;
continue;
}
}
else
break;
}
cout << arabic_Numeral << endl;
return 0;
The other characters, such as M, C, D etc are similar in layout, however with I obviously the desired output for something such as VI is 6 and it outputs 4. Any help would be appreciated.
The complete code is:
#include <string>
#include <iostream>
using namespace std;
std::string numerals = "IVXLCDM";
int main()
{
char roman_Numeral;
int arabic_Numeral = 0;
cout << "Enter the Roman Numeral in Capital letters (e.g. CCXIX) : ";
while(cin.get(roman_Numeral))
{
if(roman_Numeral == 'M')
arabic_Numeral = arabic_Numeral + 1000;
else if(roman_Numeral == 'D')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 5) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 500;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 500;
continue;
}
}
else if(roman_Numeral == 'C')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 4) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 100;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 100;
continue;
}
}
else if(roman_Numeral == 'L')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 3) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 50;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 50;
continue;
}
}
else if(roman_Numeral == 'X')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 2) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 10;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 10;
continue;
}
}
else if(roman_Numeral == 'V')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral, 2) != std::string::npos)
{
arabic_Numeral = arabic_Numeral - 5;
continue;
}
else
{
arabic_Numeral = arabic_Numeral + 5;
continue;
}
}
else if(roman_Numeral == 'I')
{
roman_Numeral = cin.peek();
if(numerals.find(roman_Numeral), 2 != std::string::npos)
{
arabic_Numeral = arabic_Numeral -1;
break;
}
else
{
arabic_Numeral += 1 ;
continue;
}
}
else
break;
}
cout << arabic_Numeral << endl;
return 0;
}
I initially found the code online and will be making additional changes later on. Currently the most of the test combinations of my code works. It's just implementing the I's so I can get test for singular cases of I such as III = 3, etc. III currently outputs -1 which is obviosuly not correct. I believe the issue is soemthing to do with the way it adds them together but not entirely sure. I am fairly new to coding after all.
Kind regards.
Your observation is correct. The code indeed has a bug for the 'I' section. Just read the code carefully and try to compare it with other working sections. Hint: missing parenthesis ;)
this is my first post in stackoverflow.
I write a program that should take a bmp file in input, and black and white it and then write it into the out.bmp.
when I started to write the code, i delete the bmp file format at the end of the name of input and then open it with text editor, then write the code, and the output style is like the input.
when I type ./a.out <in.bmp>out.bmp in terminal, I get an Abort error (Aborted (core dumped)) and when I give the ./a.out < in > out.bmp gimp say to me it is not a bmp file.
here is the code:
// In the Name of God
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <algorithm>
#include <math.h>
#include <sstream>
using namespace std;
bool TypeIsTrue (string type){
if (type == "424d")
return true;
return false;
}
int GrayIt (int b , int g , int r){
return (b + g + r) / 3 ;
}
int ConvertToDec(string hex){
long int dec = 0;
reverse(hex.begin(), hex.end());
for (int i = 0 ; i <hex.size();i++){
if (hex[i] == 'a')
dec = dec + 10 *(pow(16,i));
else if (hex[i] == 'b')
dec = dec + 11 *(pow(16,i));
else if (hex[i] == 'c')
dec = dec + 12 *(pow(16,i));
else if (hex[i] == 'd')
dec = dec + 13 *(pow(16,i));
else if (hex[i] == 'e')
dec = dec + 14 *(pow(16,i));
else if (hex[i] == 'f')
dec = dec + 15 *(pow(16,i));
else
dec = dec + ((hex[i] - '0')*(pow(16,i)));
}
return dec;
}
string ConvertToHex(int dec){
string hex;
int reminded,Divided;
reminded = dec % 16 ;
Divided = dec / 16;
if (Divided == 10){
hex = "a";
}
else if (Divided == 11){
hex = "b";
}
else if (Divided == 12){
hex = "c";
}
else if (Divided == 13){
hex = "d";
}
else if (Divided == 14){
hex = "e";
}
else if (Divided == 15){
hex = "f";
}
else if (Divided == 0){
hex = "0";
}
else if (Divided == 1){
hex = "1";
}
else if (Divided == 2){
hex = "2";
}
else if (Divided == 3){
hex = "3";
}
else if (Divided == 4){
hex = "4";
}
else if (Divided == 5){
hex = "5";
}
else if (Divided == 6){
hex = "6";
}
else if (Divided == 7){
hex = "7";
}
else if (Divided == 8){
hex = "8";
}
else if (Divided == 9){
hex = "9";
}
if (reminded == 10){
hex = hex+"a";
}
else if (reminded == 11){
hex = hex+"b";
}
else if (reminded == 12){
hex = hex+"c";
}
else if (reminded == 13){
hex = hex+"d";
}
else if (reminded == 14){
hex = hex+"e";
}
else if (reminded == 15){
hex = hex+"f";
}
else if (reminded == 0){
hex = hex+"0";
}
else if (reminded == 1){
hex = hex+"1";
}
else if (reminded == 2){
hex = hex+"2";
}
else if (reminded == 3){
hex = hex+"3";
}
else if (reminded == 4){
hex = hex+"4";
}
else if (reminded == 5){
hex = hex+"5";
}
else if (reminded == 6){
hex = hex+"6";
}
else if (reminded == 7){
hex = hex+"7";
}
else if (reminded == 8){
hex = hex+"8";
}
else if (reminded == 9){
hex = hex+"9";
}
return hex;
}
int main (){
vector <string> a;
vector <string> r;
vector <string> g;
vector <string> b;
vector <string> out;
string temp;
int red,green,blue;
while(cin >> temp){
a.push_back (temp);
}
if(!TypeIsTrue(a[0])){
cout<<"The file is not bmp\nRerun program"<<endl;
abort();
}
int phase = 1;
for (int i = 27 ; i < a.size(); i++){ //int i = 27
string first;
string last;
first = a[i].substr(0,2);
last = a[i].substr(2,3);
if(phase == 4)
phase = 1;
if(phase == 1){
b.push_back(first);
g.push_back(last);
phase ++;
// cout<<"push_backed"<<endl;
}
else if(phase == 2){
r.push_back(first);
b.push_back(last);
phase ++;
// cout<<"push_backed"<<endl;
}
else if(phase == 3){
g.push_back(first);
r.push_back(last);
phase ++;
// cout<<"push_backed"<<endl;
}
}
for (int i = 0 ; i <27 ; i++){
out.push_back(a[i]);
}
for(int i = 27 ; i<b.size() ; i++){
blue = ConvertToDec(b[i]);
green = ConvertToDec(g[i]);
red = ConvertToDec(r[i]);
out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
out.push_back ( ConvertToHex( GrayIt (blue , green , red)));
}
int j = 1 ;
for (int i = 0 ; i < 27 ; i++){
cout<< out[i] << " ";
if (j == 8){
cout<<endl;
j = 0;
}
j++;
}
j=1;
bool space = false;
for (int i = 27 ; i < out.size(); i++){
if( i == 27 + 10){
cout<<endl;
j = 1;
}
cout<<out[i];
if (space)
cout<<" ";
j++;
if(j == 17){
cout<<endl;
j = 1 ;
}
space=!space;
}
return 0;
}
You're getting an abort error because you asked for one.
if(!TypeIsTrue(a[0])){
cout<<"The file is not bmp\nRerun program"<<endl;
abort();
}
If your program is designed to have its output redirected, it is very important to send error messages to stderr (and std::cerr or std::clog) and not stdout.
BTW, the type test is failing because BMP files are not text, it makes no sense to read them using cin >> variable.
In addition, there's no guarantee that a[0] even exists. You need to test a.size() first.
if(a.size() < 1 || !TypeIsTrue(a[0])){
cerr << "The file is not bmp\nRerun program\n";
abort();
}
This program is a part of an exam I just took, that I had to write. I only got this far and couldn't get anywhere. Here is the prompt:"Write a Test Function toDecimal() that converts a roman numeral such as MMLXVII to it's decimal number representation. Use Main() to test the function. The toDecimal() function should have 2 arguments, the string array of roman numerals and a helper function. This helper function will return the numeric value of each of the letters used in roman numbers. Then convert the string arguments as so: Look at the first two characters,if the first is larger, convert the first and add it to the summation, then call the conversion function again with the second value and add both. IF the first character is lesser than the second subtract the first from the second, and add the result to the conversion of the string. without validation it will also convert strings like "IC". VAlidate the string arguement, if there is an error, call the error processing function. Provide at least two error processing functions and test toDecimal() with each. One could be adking the user to correct, the other may correct it."
I,X,C,M cannot be repeated more than 3 times in succession, D,L,V, can never be repeated in succession.I can only be subtracted from V and X,X can only be subtracted from L and C, C can only be subtracted from D and M. V, L, and D can never be subtracted.
I've lost about 2 days worth of sleep on this, tried writing it hundreds of different ways using and breaking the rules. This is the closest I've got on it.
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <cstring>
using namespace std;
bool checker(string roman);
// Adds each value of the roman numeral together
int toDecimal(string, bool* (*function)(string));
int convert(string roman, int i);
int main(){
string roman;
cout << "This program takes a roman numeral the user enters then converts it to decimal notation." << endl;
cout << "Enter a roman numeral: ";
cin >> roman;
transform(roman.begin(), roman.end(), roman.begin(), toupper);
cout << roman << " is equal to " << toDecimal(roman, *checker(roman)) << endl;
}
bool checker(string roman){
int length = roman.length();
for (int count = 0; count < length; count++){
string sub = roman.substr(count, count);
if(sub != "I" || sub != "V" || sub != "X" || sub != "L" || sub != "C" || sub != "D" || sub != "M"){
cout << "Error. Try Again"<< endl;
return false;
}
else if(convert(roman, count) == convert(roman, count-1) && convert(roman, count) == convert(roman, count+1)){
if (convert(roman,count) == 1 || convert(roman,count) == 10 || convert(roman,count) == 100 || convert(roman,count) == 1000)
if(convert(roman, count-1) == convert(roman, count-2) || convert(roman, count+1) == convert(roman, count+2)){
cout << "Error Try again" << endl;
return false;
}
else if (convert(roman,count) == 5 || convert(roman,count) == 50 || convert(roman,count) == 500){
cout << "Error Try again" << endl;
return false;
}
else return true;
}
}
return true;
}
int toDecimal(string s, bool*(checker) (string roman)){
/**map<char, int> roman;
roman['M'] = 1000;
roman['D'] = 500;
roman['C'] = 100;
roman['L'] = 50;
roman['X'] = 10;
roman['V'] = 5;
roman['I'] = 1;*/
checker(s);
int res = 0;
for (int i = 0; i < s.length() - 1; ++i){
int num = convert(s,i);
res += num;
/**if (roman[s[i]] < roman[s[i+1]])
res -= roman[s[i]];
else
res += roman[s[i]];
}
res += roman[s[s.size()-1]];*/}
return res;
}
int convert(string roman, int i){
enum romans {I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000};
int num = 0;
char c = roman[0];
switch(c){
case 'M':
num = M; break;
case 'D':
if(i + 1 != roman.size() && roman[i+1] == 'M'){
num = M - D;break;
}
else
num = D; break;
case 'C':
if(i + 1 != roman.size() && roman[i+1] == 'M' || roman[i+1] == 'D'){
if(roman[i+1] == 'M') num = M - C; break;
if(roman[i+1] == 'D') num = D - C; break;
}
else
num = C; break;
case 'L':
if(i + 1 != roman.size() && roman[i+1] == 'M' || roman[i+1] == 'D' || roman[i+1] == 'C'){
if(roman[i+1] == 'M') num = M - L; break;
if(roman[i+1] == 'D') num = D - L; break;
if(roman[i+1] == 'C') num = C - L; break;
}
else
num = L; break;
case 'X':
if(i + 1 != roman.size() && roman[i+1] == 'M' || roman[i+1] == 'D' || roman[i+1] == 'C'|| roman[i+1] == 'L'){
if(roman[i+1] == 'M') num = M - X; break;
if(roman[i+1] == 'D') num = D - X; break;
if(roman[i+1] == 'C') num = C - X; break;
if(roman[i+1] == 'L') num = C - X; break;
}
num = X; break;
case 'V':
if(i + 1 != roman.size() && roman[i+1] == 'M' || roman[i+1] == 'D' || roman[i+1] == 'C'|| roman[i+1] == 'L' || roman[i+1] == 'X'){
if(roman[i+1] == 'M') num = M - V; break;
if(roman[i+1] == 'D') num = D - V; break;
if(roman[i+1] == 'C') num = C - V; break;
if(roman[i+1] == 'L') num = L - V; break;
if(roman[i+1] == 'X') num = X - V; break;
}
num = V; break;
case 'I':
if ( i + 1 != roman.size() && roman[i + 1] != 'I'){
if(roman[i+1] == 'M') num = M - I; break;
if(roman[i+1] == 'D') num = D - I; break;
if(roman[i+1] == 'C') num = C - I; break;
if(roman[i+1] == 'L') num = L - I; break;
if(roman[i+1] == 'X') num = X - I; break;
}
num =1; break;
}
return num;
}
** I have added the help of people on here. This is an edit to show an progress/congress.
This is the code that I use to convert Roman (smaller than 3999) to Integer. You may check if it works for larger numbers.
int romanToInt(string s) {
map<char, int> roman;
roman['M'] = 1000;
roman['D'] = 500;
roman['C'] = 100;
roman['L'] = 50;
roman['X'] = 10;
roman['V'] = 5;
roman['I'] = 1;
int res = 0;
for (int i = 0; i < s.size() - 1; ++i)
{
if (roman[s[i]] < roman[s[i+1]])
res -= roman[s[i]];
else
res += roman[s[i]];
}
res += roman[s[s.size()-1]];
return res;
}
Hope this could help you.
The solution provided by Annie Kim works, but it uses a std::map, querying it several times for the same character, and I fail to see a reason for it.
int convert_roman_digit(char d)
{
switch (d)
{
case 'M': return 1000;
case 'D': return 500;
case 'C': return 100;
case 'L': return 50;
case 'X': return 10;
case 'V': return 5;
case 'I': return 1;
default: throw std::invalid_argument("Invalid digit");
}
}
int roman_to_int(const std::string& roman)
{
int result = 0, last_added = 0;
for (auto it = roman.rbegin(); it != roman.rend(); ++it)
{
const int value = convert_roman_digit(*it);
if (value >= last_added)
{
result += value;
last_added = value;
}
else
{
result -= value;
}
}
return result;
}
Caveat: the function happily accepts some invalid inputs (e.g. IMM) including "negative" numbers (e.g. IIIIIIIIIIIIIX), there are no overflow checks, and it throws. Feel free to improve it.
int romanToInt(string s)
{
unordered_map<char, int> roman;
roman['I'] = 1;
roman['V'] = 5;
roman['X'] = 10;
roman['L'] = 50;
roman['C'] = 100;
roman['D'] = 500;
roman['M'] = 1000;
int num = 0, prev = 0, curr;
for (int i = s.length() - 1; i >= 0; i--)
{
curr = roman[s[i]];
num += (curr >= prev ? 1 : -1) * curr;
prev = curr;
}
return num;
}