String gets messed up for unknown reason

时间:2019-04-16 22:20:01

标签: c++ string class

I was trying to make a program to convert Roman numerals to arabic numeral, but for some reason when he gets to the function setNumeri, the input text gets messed up super badly even before it can even get to the conversion part.

The problem is the function setNumeri, you can ignore everything else, because I pasted everything to be sure not to leave anything out.

I apologize for the fact that some variables are in Italian, but it should be pretty straightforward.

Here's the code:

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;


class IX{
 public:
  void setNumeri(string rom);
  void setArabo();
  int getArabo();
 private:
  int length;
  int Arabo;
  int Numeri[];
};

void IX::setNumeri(string rom){

 length = rom.length();
 for(int i = length - 1; i >= 0; i--) {
  cout << rom.at(i) << endl;
 switch (rom.at(i)){
  case 'I':
   Numeri[i] = 1;
   break;
  case 'V':
   Numeri[i] = 5;
   break;
  case 'X':
   Numeri[i] = 10;
   break;
  case 'L':
   Numeri[i] = 50;
   break;
  case 'C':
   Numeri[i] = 100;
   break;
  case 'D':
   Numeri[i] = 500;
   break;
  case 'M':
   Numeri[i] = 1000;
   break;
  }
 }
}

void IX::setArabo(){
 int counter = Numeri[length];
 for(int i = length - 1; i >= 0; i--){
  if(Numeri[i] == 0) {
   counter = counter + Numeri[i];
  } else if(Numeri[i] > Numeri[i-1]) {
   counter = counter - Numeri[i-1];
   i--;
  } else if(Numeri[i] <= Numeri[i-1]) {
   counter = counter + Numeri[i-1];
  }
 }
 Arabo = counter;
}

int IX::getArabo(){
 return Arabo;
}

int main(){
 IX lol;
 string hellothere;
 cout << "Roman numeral:" << endl << endl;
 cin >> hellothere;
 lol.setNumeri(hellothere);
 cout << lol.getArabo() << endl;
 return 0;
}

When I input XIII, the output is:

I
I

□
-107362937

That last ominous number is the result of the conversion, while the first 3 characters (one is missing in action, and one is not recognised at all) are the output by string.at() that, as you can see, did a wonderful job getting the string characters right. Tried using string[] instead but no success.

What's even weirder is the fact that once I deleted the whole switch case string.at() gets the string right, it looks like the string gets somehow messed up during the switch part. The same happens using an if statement instead of the switch.

Thanks in advance.

1 个答案:

答案 0 :(得分:0)

尝试以下代码,即可正常运行。

#include <cstdlib>
#include <string>
using namespace std;


class IX{
 public:
  void setNumeri(string rom);
  int getArabo();
 private:
  int length;
  int Numeri[100];
};

void IX::setNumeri(string rom){

 length = rom.length();
 for (int i = 0; i < 100; i++)
 {
   Numeri[i]=-1;
 }

 for(int i = length - 1; i >= 0; i--) {
  cout << rom.at(i) << endl;
 switch (rom.at(i)){
  case 'I':
   Numeri[i] = 1;
   break;
  case 'V':
   Numeri[i] = 5;
   break;
  case 'X':
   Numeri[i] = 10;
   break;
  case 'L':
   Numeri[i] = 50;
   break;
  case 'C':
   Numeri[i] = 100;
   break;
  case 'D':
   Numeri[i] = 500;
   break;
  case 'M':
   Numeri[i] = 1000;
   break;
  }
 }
}

int IX::getArabo(){
 int sum=0;
 for (int i = 0; i < 100; i++)
 {
   if (Numeri[i]==-1)
   {
     return sum;
   }
   else
   {
     sum+=Numeri[i];
   }


 }

}

int main(){
 IX lol;
 string hellothere;
 cout << "Roman numeral:" << endl << endl;
 cin >> hellothere;
 lol.setNumeri(hellothere);
 cout << lol.getArabo() << endl;
 return 0;
}