0

I am new in C++. I generally program in C#, so I'm having troubles with arrays and loops. When I try to print content of dynamic array using a loop, it says corrupted requested area... For example I will give it recognize the condition used with content of array but doesn't print content of it:

// Array.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void main()
{
    int size=3;
    int *p;
    int myarray[10];
    myarray[3]=4;
    p=new int[size];
    p[2]=3;
    if(myarray[3]==4){
        cout << myarray[3] +"/n";
        cout << "Why?";
    }
    else
        cout << "Not equal " << endl;
    cin.get();
    delete [] p;
}
Tim
  • 14,999
  • 1
  • 45
  • 68

3 Answers3

7

Code looks fine, unless it should be

cout << myarray[3]  << "\n";

Not +

Blood
  • 4,126
  • 3
  • 27
  • 37
  • 2
    The `"/n"` should also be a `"\n"` – sfstewman Jul 02 '12 at 17:55
  • ...and I'd recommend using std::endl in this case instead of a newline character. – Shaun Jul 02 '12 at 17:58
  • @Shaun: endl also flushes the stream, so it's not at all better than simple '\n'. –  Jul 02 '12 at 18:44
  • @jons34yp - that's why I said "in this case". For a simple hello world style application, its entirely appropriate and recommended. However, we're now broaching religious territory and this was just a simple comment so for further info on \n vs std::endl, see http://stackoverflow.com/questions/213907/c-stdendl-vs-n – Shaun Jul 02 '12 at 21:07
0

The problem is that myarray[3] +"\n".

"\n" represents the memory location of the string "\n". You are trying to add 4 to that location and printing it. This should give you junk data or a hardware exception (resulting in a coredump) if you are accessing a protected memory location.

To get what (i think) you are asking for do,

cout << myarray[3] << '\n'

Chip
  • 3,226
  • 23
  • 31
-1

While a solution has been given:

cout << myarray[3] << "\n"

the point to get is that myarray[3] is an integer while "\n" is a string and the only way to "add" them together as strings is to first make the integer into a string. The << operator will handle the work of converting myarray[3] into a string, nothing special, and then the second << pumps a new line after it. I personally prefer code like this and find it more flexible, but it may be more that you're looking for at this stage of learning:

printf("%i\n", myarray[3]);

where printf searches for flags and loads in the other arguments as strings and outputs it in one command.

mwilliams
  • 61
  • 1
  • 5
  • In C++ it is better to use << instead of printf. This link explains the reasons - http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.1 – Chip Jul 02 '12 at 21:19
  • Interesting. At the moment I still feel more comfortable with a printf but that link provides some points I hadn't thought about. The point I intended was to illustrate that myarray and "\n" are distinctly different, which << hides (sometimes for the better). – mwilliams Jul 02 '12 at 22:18