Not the results I wanted

Weegee

Go Weegee!
Reaction score
102
Recently I was learning from a tutorial site and tried to replicate one of their examples. The link to it is here.

The code they used
PHP:
// rememb-o-matic
#include <iostream>
#include <new>
using namespace std;

int main ()
{
  int i,n;
  int * p;
  cout << "How many numbers would you like to type? ";
  cin >> i;
  p= new (nothrow) int[i];
  if (p == 0)
    cout << "Error: memory could not be allocated";
  else
  {
    for (n=0; n<i; n++)
    {
      cout << "Enter number: ";
      cin >> p[n];
    }
    cout << "You have entered: ";
    for (n=0; n<i; n++)
      cout << p[n] << ", ";
    delete[] p;
  }
  return 0;
}
Their code displays perfectly. I copied it over into my IDE (Visual C++ 2010) and it worked as the example intended. My replicated code:

PHP:
// Example use of dynamic memory (memory allocated during runtime)
#include <iostream>
#include <new>
using namespace std;

int main ()
{
	int i,n;
	int * p;
	cout << "How many numbers would you like to type? ";
	cin >> i;
	p= new (nothrow) int[i];
	if (p == 0)
		cout << "Error: Memory could not be allocated";
	else
	{
		for (n=0; n<i; n++)
			cout << "Enter number: " << endl;
		    cin >> p[n];
	}
	cout << "You have entered: ";
	for (n=0; n<i; n++)
	{
		cout << p[n] << ", ";
	delete[] p;
	}
	return 0;
}
This displays oddly for me. It displays all of the "Enter number" texts at once (So if i typed that I wanted to enter 2 numbers, it would display "Enter Number" twice and only gives me the option to input once). I think i copied I replicated it correctly, but I might have missed something, does anyone see or know what is wrong with my code? Let me know if you need to know something more
 

Attachments

  • vc++ 2010 error 4-15-2011.jpg
    vc++ 2010 error 4-15-2011.jpg
    100.6 KB · Views: 227

Artificial

Without Intelligence
Reaction score
326
You need to pay attention to the curly brackets ({ and }).
  • Theirs:
    Code:
      else
      {
        for (n=0; n<i; n++)
        {
          cout << "Enter number: ";
          cin >> p[n];
        }
        cout << "You have entered: ";
        for (n=0; n<i; n++)
          cout << p[n] << ", ";
        delete[] p;
      }
    Yours:
    Code:
        else
        {
            for (n=0; n<i; n++)
                cout << "Enter number: " << endl;
                cin >> p[n];
        }
  • Theirs:
    Code:
        for (n=0; n<i; n++)
        {
          cout << "Enter number: ";
          cin >> p[n];
        }
    Yours:
    Code:
            for (n=0; n<i; n++)
                cout << "Enter number: " << endl;
  • Theirs:
    Code:
        for (n=0; n<i; n++)
          cout << p[n] << ", ";
    Yours:
    Code:
        for (n=0; n<i; n++)
        {
            cout << p[n] << ", ";
        delete[] p;
        }

If something like for, if, else, or while is followed by { statement1; statement2; }, all of code will be inside it. However, if they're followed by statement1; statement2;, only statement1 will be inside them.

So this will print nothing:
PHP:
if (false)
{
    cout << "Hello!" << endl;
    cout << "Hello!" << endl;
}
while this will print "Hello!":
PHP:
if (false)
    cout << "Hello!" << endl;
cout << "Hello!" << endl; // This line isn't inside the if, so it's executed!
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top