allocate memory, arrays

I found this program online, and I wanted to manipulate it some, but can't figure out what to fix, I keep hitting a bump. Here is the code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <algorithm> // std::sort
#include <cstddef> // std::size_t
#include <iostream>
#include <limits> // std::numeric_limits
#include <string>
 
std::size_t getNameCount()
{
  std::cout << "How many names would you like to enter? ";
  std::size_t length{};
  std::cin >> length;
 
  return length;
}
 
// Asks user to enter all the names
void getNames(std::string* names, std::size_t length)
{
  // Ignore the line feed that was left by std::cin.
  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 
  for (std::size_t i{ 0 }; i < length; ++i)
  {
    std::cout << "Enter name #" << i + 1 << ": ";
    std::getline(std::cin, names[i]);
  }
}
 
// Prints the sorted names
void printNames(std::string* names, std::size_t length)
{
  std::cout << "\nHere is your sorted list:\n";
 
  for (std::size_t i{ 0 }; i < length; ++i)
    std::cout << "Name #" << i + 1 << ": " << names[i] << '\n';
}
 
int main()
{
  std::size_t length{ getNameCount() };
 
  // Allocate an array to hold the names
  auto* names{ new std::string[length]{} };
 
  getNames(names, length);
 
  // Sort the array
  std::sort(names, names + length);
 
  printNames(names, length);
 
  // don't forget to use array delete
  delete[] names;
  // we don't need to set names to nullptr/0 here because it's going to go out
  // of scope immediately after this anyway.
 
  return 0;
}


Can someone tell me how this program would work, if we wanted to enter more information?

So instead of just having the name, maybe name, favorite color, and random age.

So the program will ask, how many people?

so, lets say 2.

Then it would it run like so...

"Enter name of one person:"

"Enter their favorite color:"

then to the next person...

"Enter name of next person:"
"Enter their favorite color:"

then it would just print that information with a random age...

Name: Joe
Color: Blue
Weight 125lbs

Name: Mark
Color: Green
weight: 220lbs

Any idea? I've been working on something like this...thanks. I don't care about the sorting. I want to see how to use the "new" in arrays, not vectors.

thanks
Hello jax16,

I have 2 ideas:

1. You could create a struct to hold the name and colour. The age or weight can be a generated random number later.

2. You could create parallel arrays, 1 for name and 1 for colour, then again the age or weight can be a generated random number later.

Then in the "getNames" function in the for loop add for the color. You would also have to add the second array to the parameters if you take that route.

Those are my ideas for not. I have not tried either one yet, but will work on both.

Andy
Let me know how it works...I am trying to do it without vectors, only arrays and pointers. Using the new ** functions...I am beginning still learning.
Hello jax16,

I tried it both ways. I found the struct easier to work with and it might be easier to sort than parallel arrays.

Both ways used a pointer to the beginning of the array which came from "new".

I adjusted the code a bit. For the includes:
1
2
3
4
5
#include <algorithm> // std::sort
//#include <cstddef> // std::size_t. I have never needed this for size_t and the "std::" is not needed, butOK if you leave it.
#include <iostream>
#include <limits> // std::numeric_limits
#include <string> 

I have not seen "cstddefs" needed for a C++ program. "size_t" has always worked in VS20?? or in other IDEs that use the MinGw header files and compiler.

The functions "getNames" and "printNames" need to be adjusted for the extra information.

I think that "std::sort" can be made to work with the struct, but for the parallel arrays you will need to write your own sort function.

Andy
if you can use struct, an array of those would be ideal, even if its just a c-struct (just bits of data) eg
struct s
{
int i;
double d;
string str;
};

s structarray[100]; //can do pointer/new here too

for(... however many times)
structarray[index].i = something; //etc

So instead of just having the name, maybe name, favourite color, and random age.


See http://www.cplusplus.com/forum/beginner/273210/ for an example using pets. The same applies for a person. Just change the variable names, the text displayed etc.
Last edited on
Because the OP has a habit of deleting their posts after getting a reply, here is the original question:

jax16 wrote:

I found this program online, and I wanted to manipulate it some, but can't figure out what to fix, I keep hitting a bump. Here is the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <algorithm> // std::sort
#include <cstddef> // std::size_t
#include <iostream>
#include <limits> // std::numeric_limits
#include <string>
 
std::size_t getNameCount()
{
  std::cout << "How many names would you like to enter? ";
  std::size_t length{};
  std::cin >> length;
 
  return length;
}
 
// Asks user to enter all the names
void getNames(std::string* names, std::size_t length)
{
  // Ignore the line feed that was left by std::cin.
  std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
 
  for (std::size_t i{ 0 }; i < length; ++i)
  {
    std::cout << "Enter name #" << i + 1 << ": ";
    std::getline(std::cin, names[i]);
  }
}
 
// Prints the sorted names
void printNames(std::string* names, std::size_t length)
{
  std::cout << "\nHere is your sorted list:\n";
 
  for (std::size_t i{ 0 }; i < length; ++i)
    std::cout << "Name #" << i + 1 << ": " << names[i] << '\n';
}
 
int main()
{
  std::size_t length{ getNameCount() };
 
  // Allocate an array to hold the names
  auto* names{ new std::string[length]{} };
 
  getNames(names, length);
 
  // Sort the array
  std::sort(names, names + length);
 
  printNames(names, length);
 
  // don't forget to use array delete
  delete[] names;
  // we don't need to set names to nullptr/0 here because it's going to go out
  // of scope immediately after this anyway.
 
  return 0;
}


Can someone tell me how this program would work, if we wanted to enter more information?

So instead of just having the name, maybe name, favorite color, and random age.

So the program will ask, how many people?

so, lets say 2.

Then it would it run like so...

"Enter name of one person:"

"Enter their favorite color:"

then to the next person...

"Enter name of next person:"
"Enter their favorite color:"

then it would just print that information with a random age...

Name: Joe
Color: Blue
Weight 125lbs

Name: Mark
Color: Green
weight: 220lbs

Any idea? I've been working on something like this...thanks. I don't care about the sorting. I want to see how to use the "new" in arrays, not vectors.

thanks
Topic archived. No new replies allowed.