pnsrp's blog

By pnsrp, 13 years ago, In English
Anybody can help me, I got runtime error for the this problem, 1B - spread sheet, it works fine in my computer.

#include<iostream>
#include<string>
#include<cmath>
#include<cstdlib>
#include<stack>

using namespace std;

char convert(int cno){
     return cno - 1 + 'A';
     }
    
int convert2n (char ch){
    return ch - 'A' + 1;
    }

int main()
{
    int n, index, cno, total=0, multiplicand=1;
    string input, row, col;
    stack<char>ch_stack;
    cin >> n;
    while(n--){
               cin >> input;
               if(input[0]=='R' && (input[1]>=48 && input[1]<=57)){
                                for(int i=2; i<input.size(); i++){
                                        if(input[i]=='C') {index=i; break;}
                                        }
                                row = input.substr(1,index-1);
                                col = input.substr(index+1);
                                cno = atoi(col.c_str());
                                if(cno<=26){
                                            cout << convert(cno) << row << endl;
                                            }
                                else {
                                     while(cno>26){
                                                   ch_stack.push(convert(cno%26));
                                                   cno /= 26;
                                                   }
                                     ch_stack.push(convert(cno));
                                     while (!ch_stack.empty()){
                                           cout << ch_stack.top();
                                           ch_stack.pop();
                                           }
                                     cout << row << endl;
                                     }
                                }
               else {
                    for(int i=1; i<input.size(); i++){
                            if(input[i]>=48 && input[i]<=57) {index=i; break;}
                            }
                    row = input.substr(index);
                    col = input.substr(0, index);
                    for(int i=col.size()-1; i>=0; i--){
                            char ch = col[i];
                            total += convert2n(ch)*multiplicand;
                            multiplicand *= 26;
                            }
                    cout << "R" << row << "C" << total << endl;
                    multiplicand = 1;
                    total = 0;
                    }
             
               }
    //system("pause");
    return 0;
}
I did it in a stupid way, but i wanted to know why i got that runtime error, Please help me.
Thank you.
  • Vote: I like it
  • -26
  • Vote: I do not like it

| Write comment?
13 years ago, # |
  Vote: I like it +12 Vote: I do not like it
next time, please, submit your code to pastebin and write there a link to it - it will be better to see ans understand it
13 years ago, # |
  Vote: I like it +9 Vote: I do not like it
Your 'index' variable used before initialisation.
13 years ago, # |
  Vote: I like it 0 Vote: I do not like it
Thanks a lot, freopen.
  • 13 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    You can reply to user, below of his comment, by pressing button 'Reply'