| | |
Need some guidance/pointers/tips/etc regarding codes.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 2
Hi,
I'm doing a project regarding converting roman numerals to decimals and vice versa. With some reference from the internet, I was able to come up with one. However, it isn't working very well. Can someone look at my codes and point out what/where's the mistakes?
Thanks in advance.
The answers are suppose to be those on the textfile, instead I get some extra characters for some numbers.
I went through and followed the numbers but can't figure out why.
http://i126.photobucket.com/albums/p...untitled-2.jpg
So I tried to limit the codes and location of the mistakes.http://i126.photobucket.com/albums/p.../untitled2.jpg
Weird... The first few numbers and once in awhile it works fine.
I'm doing a project regarding converting roman numerals to decimals and vice versa. With some reference from the internet, I was able to come up with one. However, it isn't working very well. Can someone look at my codes and point out what/where's the mistakes?
Thanks in advance.
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cstring> using namespace std; int checkStatus(string); string toRoman(string); int main() { ifstream fin; string filename, data, roman; int status; cout << "Please enter filename: "; getline(cin, filename); fin.open(filename.c_str()); if (!fin.good()) { cout << "File not found" << endl; system("pause"); return 1; } while (!fin.eof()) { getline(fin, data); status = checkStatus(data); if (status == 0) cout << data << "\t\t" << "Invalid" << endl; else if (status == 1) { roman = toRoman(data); cout << data << "\t\t"; for (int c = 0; (isalpha(roman[c])) ; c++) cout << roman[c]; cout << endl; } else if (status == 2) cout << data << "\t\t" << "Decimal" << endl; } fin.close(); system("pause"); return 0; } int checkStatus(string data) { char temp; if (!isdigit(data[0])) { for (int b = 0; b < data.length(); b++) { temp = toupper(data[b]); if ((temp != 'I') && (temp != 'V') && (temp != 'X') && (temp != 'L') && (temp != 'C') && (temp != 'D') && (temp != 'M')) return 0; } return 2; } else { for (int b = 0; b < data.length(); b++) { if (!isdigit(data[b])) return 0; } return 1; } } string toRoman(string data) { int a; int i = 0; string romanF; a = atoi(data.c_str()); while(a > 0) { if((a >= 1000) && (a < 4000)) { romanF[i] = 'M'; a = a-1000; i ++; } else if((a >= 900) && (a < 1000)) { romanF[i] = 'C'; romanF[i+1] = 'M'; a = a-900; i += 2; } else if((a >= 500) && (a < 900)) { romanF[i] = 'D'; a = a-500; i ++; } else if((a >= 400) && (a < 500)) { romanF[i] = 'C'; romanF[i+1] = 'D'; a = a-400; i += 2; } else if((a >= 100) && (a < 400)) { romanF[i] = 'C'; a = a-100; i ++; } else if((a >= 90) && (a < 100)) { romanF[i] = 'X'; romanF[i+1] = 'C'; a = a-90; i += 2; } else if((a >= 50) && (a < 90)) { romanF[i] = 'L'; a = a-50; i ++; } else if((a >= 40) && (a < 50)) { romanF[i] = 'X'; romanF[i+1] = 'L'; a = a-40; i += 2; } else if((a >= 10) && (a < 40)) { romanF[i] = 'X'; a = a-10; i ++; } else if((a >= 9) && (a < 10)) { romanF[i] = 'I'; romanF[i+1] = 'X'; a = a-9; i += 2; } else if ((a >= 5) && (a < 9)) { romanF[i] = 'V'; a = a-5; i ++; } else if ((a >= 4) && (a < 5)) { romanF[i] = 'I'; romanF[i+1] = 'V'; a = a-4; i += 2; } else if ((a >= 1) && (a < 4)) { romanF[i] = 'I'; a = a-1; i ++; } } return romanF; }
The answers are suppose to be those on the textfile, instead I get some extra characters for some numbers.
I went through and followed the numbers but can't figure out why.
http://i126.photobucket.com/albums/p...untitled-2.jpg
So I tried to limit the codes and location of the mistakes.http://i126.photobucket.com/albums/p.../untitled2.jpg
Weird... The first few numbers and once in awhile it works fine.
Just had a quick look but something im gonna point out.
could become
This has made it cleaner, but also it has reduded the time complexity by only having to perform 1 comparison.
Chris
C++ Syntax (Toggle Plain Text)
else if ((a >= 4) && (a < 5)) { romanF[i] = 'I'; romanF[i+1] = 'V'; a = a-4; i += 2; }
C++ Syntax (Toggle Plain Text)
else if (a==4) { romanF[i++] = 'I'; romanF[i++] = 'V'; a -= 4; }
This has made it cleaner, but also it has reduded the time complexity by only having to perform 1 comparison.
Chris
Last edited by Freaky_Chris; Oct 10th, 2008 at 5:08 pm.
Knowledge is power -- But experience is everything
•
•
Join Date: Jan 2008
Posts: 49
Reputation:
Solved Threads: 2
Woot! I just solved it.
else if (status == 1)
{
roman = toRoman(data);
cout << data << "\t\t";
for (int c = 0; isalpha(roman[c]) ; c++)
{
cout << roman[c];
roman[c] = ' ';
}
But it's weird on how I have to clear the string. Shouldn't it be a new string everytime?
I tried using string.clear() but it doesn't work. Hmmm
else if (status == 1)
{
roman = toRoman(data);
cout << data << "\t\t";
for (int c = 0; isalpha(roman[c]) ; c++)
{
cout << roman[c];
roman[c] = ' ';
}
But it's weird on how I have to clear the string. Shouldn't it be a new string everytime?
I tried using string.clear() but it doesn't work. Hmmm
![]() |
Other Threads in the C++ Forum
- Previous Thread: do my homework for me because I'm too lazy
- Next Thread: number of ways to..ehh, i hate counting ways
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





