gautiergator
March 29th, 2008, 09:36 PM
For some reason the for loop never executes. Any idea why?
The program is supposed to convert a user entered roman numeral to decimal. I can place cout<<"Test"; in the loop or after it and neither will ever execute.
I have all of the code below.
romantype.h
#ifndef ROMANTYPE_H
#define ROMANTYPE_H
#include <cstring>
/*
* No description
*/
class romanType
{
public:
// class constructor
romanType();
// class destructor
~romanType();
//printDecimal
void printDecimal() const;
//printDecimal
void printRoman() const;
//convertRoman
void convertRoman();
char myRoman[];
int myDecimal;
};
#endif // ROMANTYPE_H
romantype.cpp
#include "romantype.h" // class's header file
#include <iostream>
#include <stdio.h>
#include <cstring>
//const static int M = 1000;
//const static int D = 500;
//const static int C = 100;
//const static int L = 50;
//const static int X = 10;
//const static int V = 5;
//const static int I = 1;
using namespace std;
// class constructor
romanType::romanType()
{
char myRoman[] = "";
myDecimal = 0;
}
// class destructor
romanType::~romanType()
{
// code here
}
//Print Decimal
void romanType::printDecimal() const
{
cout<<"The Decimal value is "<<myDecimal<<endl;
}
//Print Roman
void romanType::printRoman() const
{
cout<<"The Roman numeral is "<<myRoman<<endl;
}
//Convert Roman
void romanType::convertRoman()
{
int counter;
int length;
char charNow[1] = "";
char charPrev[1] = "";
int valueNow = 0;
int valuePrev = 0;
length = strlen(myRoman);
for(counter=length; counter<1; counter--)
{
charNow[1] = myRoman[counter];
cout<<myRoman[counter]<<endl;
if (charNow == "M")
{valueNow = 1000;}
else
if (charNow == "D")
{valueNow = 500;}
else
if (charNow == "C")
{valueNow = 100;}
else
if (charNow == "L")
{valueNow = 50;}
else
if (charNow == "X")
{valueNow = 10;}
else
if (charNow == "V")
{valueNow = 5;}
else
if (charNow == "I")
{valueNow = 1;}
if (valueNow < valuePrev)
{myDecimal = myDecimal - valueNow;}
else
{myDecimal = myDecimal + valueNow;}
valuePrev = valueNow;
};//end for
}
and finally main.cpp
#include <cstdlib>
#include <iostream>
#include "romantype.h"
using namespace std;
char userIn;
romanType romanConverter;
int main(int argc, char *argv[])
{
cout<<"Please enter a Roman Numeral: ";
cin>>romanConverter.myRoman;
romanConverter.convertRoman();
romanConverter.printDecimal();
romanConverter.printRoman();
system("PAUSE");
return EXIT_SUCCESS;
}
The program is supposed to convert a user entered roman numeral to decimal. I can place cout<<"Test"; in the loop or after it and neither will ever execute.
I have all of the code below.
romantype.h
#ifndef ROMANTYPE_H
#define ROMANTYPE_H
#include <cstring>
/*
* No description
*/
class romanType
{
public:
// class constructor
romanType();
// class destructor
~romanType();
//printDecimal
void printDecimal() const;
//printDecimal
void printRoman() const;
//convertRoman
void convertRoman();
char myRoman[];
int myDecimal;
};
#endif // ROMANTYPE_H
romantype.cpp
#include "romantype.h" // class's header file
#include <iostream>
#include <stdio.h>
#include <cstring>
//const static int M = 1000;
//const static int D = 500;
//const static int C = 100;
//const static int L = 50;
//const static int X = 10;
//const static int V = 5;
//const static int I = 1;
using namespace std;
// class constructor
romanType::romanType()
{
char myRoman[] = "";
myDecimal = 0;
}
// class destructor
romanType::~romanType()
{
// code here
}
//Print Decimal
void romanType::printDecimal() const
{
cout<<"The Decimal value is "<<myDecimal<<endl;
}
//Print Roman
void romanType::printRoman() const
{
cout<<"The Roman numeral is "<<myRoman<<endl;
}
//Convert Roman
void romanType::convertRoman()
{
int counter;
int length;
char charNow[1] = "";
char charPrev[1] = "";
int valueNow = 0;
int valuePrev = 0;
length = strlen(myRoman);
for(counter=length; counter<1; counter--)
{
charNow[1] = myRoman[counter];
cout<<myRoman[counter]<<endl;
if (charNow == "M")
{valueNow = 1000;}
else
if (charNow == "D")
{valueNow = 500;}
else
if (charNow == "C")
{valueNow = 100;}
else
if (charNow == "L")
{valueNow = 50;}
else
if (charNow == "X")
{valueNow = 10;}
else
if (charNow == "V")
{valueNow = 5;}
else
if (charNow == "I")
{valueNow = 1;}
if (valueNow < valuePrev)
{myDecimal = myDecimal - valueNow;}
else
{myDecimal = myDecimal + valueNow;}
valuePrev = valueNow;
};//end for
}
and finally main.cpp
#include <cstdlib>
#include <iostream>
#include "romantype.h"
using namespace std;
char userIn;
romanType romanConverter;
int main(int argc, char *argv[])
{
cout<<"Please enter a Roman Numeral: ";
cin>>romanConverter.myRoman;
romanConverter.convertRoman();
romanConverter.printDecimal();
romanConverter.printRoman();
system("PAUSE");
return EXIT_SUCCESS;
}