₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,330,984 members, 8,448,101 topics. Date: Sunday, 19 July 2026 at 06:41 PM

Toggle theme

C++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingC++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! (1423 Views)

1 Reply (Go Down)

C++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! by Sirnuel(op): 12:04pm On Oct 21, 2015
I can't seem to find the bug in this my program


====================================================================
roman_to_dec.h

#include <string>
using namespace std;

int M, D, C, L, X, V, I;

class romanType{
public:
romanType();
void set_roman_type(string);
int get_roman_type()const;
int convert_rom_to_dec();
int get_answer_in_decimal()const;
~romanType();

private:
int answer_in_decimal;
string roman;
};


-----------------------------------------------------------------------------------------------------------------------------
roman_to_dec.cpp

#include <iostream>
#include <string>
#include <cstring>
#include "roman_to_dec.h"

int display_menu();
void switch_stat(int, romanType);

using namespace std;

int input;

romanType::romanType(){

answer_in_decimal = 0;

roman = " ";
}


void romanType::set_roman_type(string x){

for(int i = 0; i < 10; i++ )
x[i] = roman[i];
}

int romanType::convert_rom_to_dec(){

int answer;


int roman1, roman2;

string::size_type i = 0;

while (i < roman.length()){

switch(toupper(roman.at(i))){

case 'M': roman1 = 1000;
break;

case 'D': roman1 = 500;
break;

case 'C': roman1 = 100;
break;

case 'L': roman1 = 50;
break;

case 'X': roman1 = 10;
break;

case 'V': roman1 = 5;
break;

case 'I': roman1 = 1;
break;

default: roman1 = -1;
}

switch(toupper(roman.at(i + 1))){

case 'M': roman2 = 1000;
break;

case 'D': roman2 = 500;
break;

case 'C': roman2 = 100;
break;

case 'L': roman2 = 50;
break;

case 'X': roman2 = 10;
break;

case 'V': roman2 = 5;
break;

case 'I': roman2 = 1;
break;

default: roman2 = -1;
}

if ((roman1 == -1) || (roman2 == -1)){

cout <<"\n\n\tError incorrect input";
return -1;
}

else if (roman1 < roman2){

answer = roman2 - roman1;
i +=2;
}

else if (roman1 >= roman2){

answer += roman[i];
i +=1;
}

}

answer_in_decimal = answer;

return answer;
}


int romanType::get_answer_in_decimal()const{

return answer_in_decimal;
}
romanType::~romanType(){

cout << "calling destructor";
}


int display_menu(){

cout << "\n\t---------------------------------------------------------------------";
cout << "\n\t---------This program converts Roman Numerals to Decimals -----------";
cout << "\n\t| |";
cout << "\n\t| |";
cout << "\n\t| Press 1 to perform a new converstion |";
cout << "\n\t| |";
cout << "\n\t| |";
cout << "\n\t| Press 2 to view last result |";
cout << "\n\t| |";
cout << "\n\t| |";
cout << "\n\t| Or press 0 to exit |";
cout << "\n\t| |";
cout << "\n\t| |";
cout << "\n\t---------------------------------------------------------------------\n\n\t";

cin >> input;


while ((input != 0) && (input > 2)){

cout << "error incorrect input\n try again";

cout << "\n\t---------------------------------------------------------------------";
cout << "\n\t---------This program converts Roman Numerals to Decimals -----------";
cout << "\n\t| |";
cout << "\n\t| |";
cout << "\n\t| Press 1 to perform a new converstion |";
cout << "\n\t| |";
cout << "\n\t| |";
cout << "\n\t| Press 2 to view last result |";
cout << "\n\t| |";
cout << "\n\t| |";
cout << "\n\t| Or press 0 to exit |";
cout << "\n\t| |";
cout << "\n\t| |";
cout << "\n\t---------------------------------------------------------------------\n\n\t";

cin >> input;

}

return input;

}



void switch_stat(int input, romanType roman_type){

string input_roman;

switch(input){

case 0: cout <<"exiting the program\n\n";
break;

case 1: cout << "input the numbers in roman figures:\t";
cin >> input_roman;
roman_type.set_roman_type(input_roman);
roman_type.convert_rom_to_dec();
cout << "\n\n\t" << input_roman << " = " << roman_type.get_answer_in_decimal()<< "in decimal";
cout << endl;
break;

case 2: cout << "testing case.......it works";
break;

default: cout << "\n\tError 505....Incorrect input!!!";
break;
}

}
--------------------------------------------------------------------------------------------------------------------------------

roman_to_dec_main.cpp

#include <iostream>
#include "roman_to_dec.cpp"



int main(){

romanType roman_type;

int input = display_menu();
switch_stat(input, roman_type);


return 0;
}






----------------------------------------------------------------------------------------------------------------------------------



this is the error i get whenever i try to run the program


termincate called after throwing an instance of 'std::out_of_range'
what(): basic_string::at _n (which is 1) >= this->size() (which is 1)

this application has requested the Runtime to terminate it in an unusual way.
Please contact he application's support team for more information.
Re: C++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! by codemarshal08(m):
hmm........your program structure is making it hard for me to scan through. Anyways, i tried to reproduce the error but your code kept giving me multiple errors different from the one you posted here
this is the error i get whenever i try to run the program
Please are you sure you were able to pass the COMPILE stage to the RUN stage ?
Re: C++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! by Sirnuel(op): 8:50pm On Oct 21, 2015
codemarshal08:
hmm........your program structure is making it hard for me to scan through. Anyways, i tried to reproduce the error but your code kept giving me multiple errors different from the one you posted here

Please are you sure you were able to pass the COMPILE stage to the RUN stage ?
.yes it compiled successfully
Re: C++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! by WhiZTiM(m): 9:21pm On Oct 21, 2015
I glanced over your code and can see a bug with the second switch statement.

You are testing for roman.at(i + 1) under the premise that i < roman.size().... That's wrong cause when you reach the last element, i will go overboard. If you are ever testing i + x, then your condition should be i - x....
What i am saying is...

Your while loop should be

.....
while (i < roman.length() - 1){
.....
Re: C++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! by liliian(f): 6:31pm On Oct 23, 2015
WhiZTiM:
I glanced over your code and can see a bug with the second switch statement.

You are testing for roman.at(i + 1) under the premise that i < roman.size().... That's wrong cause when you reach the last element, i will go overboard. If you are ever testing i + x, then your condition should be i - x....
What i am saying is...

Your while loop should be

.....
while (i < roman.length() - 1){

.....
Please can i get help with this:https://www.nairaland.com/2685865/need-assignment-java-programming#39282992
Re: C++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! by liliian(f): 6:31pm On Oct 23, 2015
codemarshal08:
hmm........your program structure is making it hard for me to scan through. Anyways, i tried to reproduce the error but your code kept giving me multiple errors different from the one you posted here

Please are you sure you were able to pass the COMPILE stage to the RUN stage ?
Please i need help:https://www.nairaland.com/2685865/need-assignment-java-programming#39282992
Re: C++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! by Sirnuel(op): 10:44pm On Oct 23, 2015
WhiZTiM:
I glanced over your code and can see a bug with the second switch statement.

You are testing for roman.at(i + 1) under the premise that i < roman.size().... That's wrong cause when you reach the last element, i will go overboard. If you are ever testing i + x, then your condition should be i - x....
What i am saying is...

Your while loop should be

.....
while (i < roman.length() - 1){
.....
Thanks.... I also found few bugs out on my own too. the program now runs smoothly
Re: C++ Programmers Help!!!!!!!!!!!!!!!!!!!!!!!! by MOM1(m): 6:50pm On Oct 27, 2015
Does anyone knows how to use the Oracle Virtual Machine?
1 Reply

Please Web Programmers Help Me On ThisPls Programmers Help With Similarities Of Pseudocode & AlgorithmPlease, I Need Help?.....programmers,help A Nairalander?234

Need To Open The File042 Programming GurusLaravel Vuejs Video Manager Application Tutorial Series