Need Help w/ Structures & Constant Values

Hello guys. I'm a need of some assistance. I finished my C++ book yesterday (yes!) and stuck 98% of the codes into my gray matter (brain). The book ended with structures.

Example:

1
2
3
4
5
6
7
8
9
10
struct Students
{
   char Name [80];
   short testScore1, testScore2, testScore3;
};

// to determine the amount of students I need a constant value

const short numStudents = 10;
Students worms [numStudents];


Now... From what I read, there was another way of getting a constant value even though the user inputted the value. This is called Dynamic Memory Allocation. Basically, a pointer is created only for one purpose and that's to hold the address/value of the users input and since it can only do that, it is known, also, as a constant value.

Example:
1
2
3
4
5
6
short num;
cout << "Enter # of students: ";
cin >> num; // this is not a constant due to the fact it can be changed
cin.ignore ();

short*stuPtr = new short [num]; // this is a constant value and is known as stuPtr 


Now, what I want to do is use the newly obtained Dynamic Memory Allocation to declare the amount of "Students" the structure will handle.

Here is the 2/4 of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Students
{
   char Name [80];
   short testScore1, testScore2, testScore3;
};

int main (void)
{
   short num;

   cout << "Enter # of students: ";
   cin  >> num;
   cin.ignore ();

   short*stuPtr = new short [num];

   Students worms [*stuPtr]; // this is where the problem occurs

   // ... rest of code
}


Is there something I'm doing wrong? I'm pretty sure that the newly obtained pointer is a constant value. If I'm doing it wrong then can you explain a different method of making this work? Thanks.
That doesn't sound like a very useful book.

In C++, that program would look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <string>
#include <vector>
#include <iostream>
struct Students
{
   std::string Name;
   short testScore1, testScore2, testScore3;
};

int main()
{
   short num;

   std::cout << "Enter # of students: ";
   std::cin  >> num;

   std::vector<Students> worms(num);

   // ... rest of code
}

demo: http://ideone.com/x6pZm

In the implausible case that you actually need a C-style array here, modern C++ program would be like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <memory>
#include <string>
#include <iostream>
struct Students
{
   std::string Name;
   short testScore1, testScore2, testScore3;
};

int main()
{
   short num;

   std::cout << "Enter # of students: ";
   std::cin  >> num;

   std::unique_ptr<Students[]> worms(new Students[num]);

   // ... rest of code
}



Your program, minimally modified to compile, is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
struct Students
{
   char Name [80];
   short testScore1, testScore2, testScore3;
};

int main (void)
{
   short num;

   std::cout << "Enter # of students: ";
   std::cin  >> num;
   std::cin.ignore (); // why?

   Students* worms = new Students[num]; // change this

   // ... rest of code

   delete[] worms; // add this
}

but it's probably best to find a good book.
Last edited on
I'm a bit confused, can't you just do something like

 
Students *worms = new Students[num];


Why would you need to pass it a pointer to a short, is that required in your program?
Last edited on
@Cubbi

The book was C++ Demystified. It was for learning C++ from scratch. Thanks for the minimally modified code. It makes sense.
That book is listed as "Not recommended" by ACCU
http://accu.org/index.php?module=bookreviews&func=search&rid=109
@Cubbi

Do you know any books that can lend me a hand on increasing my C++ knowledge?
I would start out with a Principals of problem solving in C++ book to get started. (try amazon and read the reviews)

After you are familiar up to the point of pointers I would opt for an object oriented programming book to learn inheritance, polymorphism, and the STL library.

If those terms confuse don't worry, it's pretty straight forward actually. IN smaller terms those concepts are easy to learn. You won't encounter the most difficult scenarios until you start designing or working on very large programs. That's when the bugs start to rape your brain. lol
@IceThatJaw

Alright man. The book will ship in 3 days. Got if for cheap too. Programming is so freaking awesome. Thanks for your recommendation.
Topic archived. No new replies allowed.