Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2005-04-05
Medium Priority
Last Modified: 2010-04-01

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(){
            cout<<"What phrase to pyramid?" <<endl;
            string 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, ' ');
                  outbuf[linect] = newmainstr;
            for(int i = 0;i <= strsize;i++)
                  cout << outbuf[i] <<endl;
      catch(string e)
            cout << e << endl;
      return 0;
Question by:norbs101
LVL 86

Accepted Solution

jkr earned 200 total points
ID: 13709070
The problem is

          for(int i = 0;i <= strsize;i++)
              cout << outbuf[i] <<endl;

which should read

          for(int i = 0;i < strsize;i++)
              cout << outbuf[i] <<endl;

Using '<=', you are accessing an element that is not part of the array, thus the error.

Author Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question