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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

801 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