Pointer clarification.

M

Makwana

I am a grad student trying to get my hands on c++. I am trying to code
simple programs using pointers and it would be great if some gurus
here point me to the correct method of referencing.

My code complies but crash's .. can someone point me the right way to
do it. Thanks in advance!

*******************************************************************************************************************************
// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
string title;
int year;
};

int main ()
{
string mystr;
int mov_num,i;
cout << " Enter the number of movies you want to enter " << endl;
cin >> mov_num;

movies_t amovie[mov_num];
movies_t * pmovie[mov_num];
// strcpy(&amovie, pmovie);
// pmovie = &amovie;

for (i=0;i<mov_num;i++);
{
cout << "Enter title: " << endl;
getline (cin, pmovie->title);
cout << "Enter year: " << endl;
getline (cin, mystr);
(stringstream) mystr >> pmovie->year;
}
for (i=0;i<mov_num;i++);
cout << "\nYou have entered:\n";
cout << pmovie->title;
cout << " (" << pmovie->year << ")\n";
system ("pause");
return 0;
}
 
E

Erik Wikström

I am a grad student trying to get my hands on c++. I am trying to code
simple programs using pointers and it would be great if some gurus
here point me to the correct method of referencing.

My code complies but crash's .. can someone point me the right way to
do it. Thanks in advance!

*******************************************************************************************************************************
// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
string title;
int year;
};

int main ()
{
string mystr;
int mov_num,i;

This is C++, you declare the variables right before you use them for the
first time, in particular 'i' should be declared in the for-loop.
cout << " Enter the number of movies you want to enter " << endl;
cin >> mov_num;

movies_t amovie[mov_num];

This is not allowed in C++, the size of the array have to be a constant.
If your compiler allows you to do that you need to tell it to disallow
extensions to the language. Instead of an array you should be using
std::vector in this situation, considering that you are using string-
streams I think you should know about them by now.
movies_t * pmovie[mov_num];

There is really no need for pointers in this program, they will only
make things more complicated than necessary, you can use the array (or
vector) directly.
// strcpy(&amovie, pmovie);
// pmovie = &amovie;

for (i=0;i<mov_num;i++);

for (int i = 0; i < mov_num; ++i)

Notice the lack of the semicolon at the end, it is very important. The
way you have written it only the semicolon is inside the loop, the code
below is not. The effect is that when the code below is executed 'i'
will be equal to mov_num, which is one past the end of the array, which
is probably why your program crashes.
{
cout << "Enter title: " << endl;
getline (cin, pmovie->title);
cout << "Enter year: " << endl;
getline (cin, mystr);
(stringstream) mystr >> pmovie->year;
}
for (i=0;i<mov_num;i++);


Remove the semicolon, and add the brackets, if your text-editor supports
automatically indenting the code you should use this feature since it
will tell you which code belongs to which code-block.
cout << "\nYou have entered:\n";
cout << pmovie->title;
cout << " (" << pmovie->year << ")\n";
system ("pause");
return 0;
}
 
P

peter koch

I am a grad student trying to get my hands on c++. I am trying to code
simple programs using pointers

There's your first problem. It is not so easy to write (nontrivial)
programs with pointers. Even experienced programmers might put in a
bug or two in this case.
and it would be great if some gurus
here point me to the correct method of referencing.

My code complies but crash's .. can someone point me the right way to
do it. Thanks in advance!

***************************************************************************­****************************************************
// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;

};

int main ()
{
  string mystr;
  int mov_num,i;

As Erik mentioned, declare variables when you use them - not before.
This habit might improve the performance of your program, but more
important it reduces the number of bugs.
  cout << " Enter the number of movies you want to enter " << endl;
  cin >> mov_num;

  movies_t amovie[mov_num];
This is not valid C++. mov_num is not a compile-time constant - your
program should not compile.

Why amovie? This is a bad name for an array.
  movies_t * pmovie[mov_num];

This is a pointer to an array of movies - probably not what you want.
//  strcpy(&amovie, pmovie);
//  pmovie = &amovie;

Why these commented out statements - they make no sense.
  for (i=0;i<mov_num;i++);
  {
  cout << "Enter title: " << endl;
  getline (cin, pmovie->title);
  cout << "Enter year: " << endl;
  getline (cin, mystr);
  (stringstream) mystr >> pmovie->year;


This is a C-cast. Never do that. What are you trying to do here? If
you want to read an integer simply do e.g. std::cin << pmovie-
  }
  for (i=0;i<mov_num;i++);
This line is a complete for loop, that doesn't do anything.
  cout << "\nYou have entered:\n";
  cout << pmovie->title;
  cout << " (" << pmovie->year << ")\n";
  system ("pause");
  return 0;



}


Also your intendation is bad. Use copy-paste from your source-file.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top