Link to home
Start Free TrialLog in
Avatar of norbs101
norbs101Flag for United States of America

asked on

new to c++, with a weird error on runtime

Hi,  

I've just started to toy around with c++.  I wrote a program which takes a string and creates a text pyramid, using a random set of characters from the input.  Now, the thing compiles just fine and seems to run well under command prompt.  Except if my input is something which seems to be a  key word of somekind, i.e. "norbert test", the program goes nuts....printing funny characters for a while, if i type in "debug " ( space is needed to generate the error) then the program crashes, same with other string which seem like they might be system commands.

Here's the source code, which I cannot trace where I'd be getting this behavior from, any help would be appreciated.

#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main(){
      try{
            cout<<"What phrase to pyramid?" <<endl;
            string phrase;
            getline(cin,phrase);
            const int strsize = phrase.size();
            string *pyramid = new string[strsize];
            string newmainstr;
            int nOfBlanks = 0;
            string *outbuf = new string[strsize];
            for(int linect = strsize-1;linect >= 0; linect--)
            {
                  newmainstr.append(nOfBlanks, ' ');
                  for(int pos = 0;pos <= linect; pos++)
                  {
                              newmainstr.append(1, phrase[rand()%strsize]);
                              newmainstr.append(1, ' ');
                  }      
                  nOfBlanks++;
                  outbuf[linect] = newmainstr;
                  newmainstr.clear();}
            for(int i = 0;i <= strsize;i++)
            {
                  cout << outbuf[i] <<endl;
            }
      }
      catch(string e)
      {
            cout << e << endl;
      }
      return 0;
      }
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of norbs101

ASKER

You are absolutely correct!!!  It's the same old oversight of a +/-  1 in a loop.  Thank you.