Pointers assignment

Pages: 123
Hello,

As you can see, when I first posted the topic I did present some code of what I had started. So NO I’m not asking anyone to write the whole program for me.

However, I’m a beginner. I just know basic input and output. I don’t know the rest of the stuff and reason I’m asking for help with the rest of the program
Hello cblack618,

I do not know what the directions that you were given look like, but let us look at what you have posted a bit differently.


For this assignment, you will use pointers to write a program that asks the user to enter the name,
birth city and year of birth for five (5) family members.

1. Be sure to include comments throughout your code where appropriate.

2. The data should be stored in dynamically created parallel arrays.

3. Users are assumed to be born between the years 1900 and 2018, and should enter the year of birth
in one of the two formats 19XX and 20XX.

4. Output the data from the arrays in a formatted table.


Putting this in one block of type may save some space, but it is hard to read and pick out what you need to do. Also numbering can help.

Looking at #1 this is something that should be done as you code. The compiler does not care about comments nor does it include them in the compiled code. This is also true for white space, i.e., blank lines. But this does make a difference in how the code reads. Thinking that you will go back later and add comments does not help you when writing the code.

As I said the first thing you need to do is get the input to have something to work with before you can work on output.

Looking at your code, which does not compile or follow the directions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <iomanip>

int main()
{
	string * name = nullptr;
	string * birthcity = nullptr;
	string * birthyear = nullptr;

	cout << "Enter your name" << endl;
	getline(cin, *firstname);

	cout << "Enter your birth city" << endl;
	getline(cin, *lastname);

	char* person[3]{ "name", "Birthplace", "Birthyear" }
		char* charles[3] = { "Charles Blackwell", "12/2/1980", "michigan" }

	return 0;
}

I changed some of the layout to make it easier to see what is happening.

Lines 1 - 9 is a good start. Lines 7 - 9 define pointers and sets them to "nullptr"s which is good. After that you have missed point 2 of the directions.

Line 12 is trying to use the variable "firstName" which is never defined. The same is true for line 15.

You have also missed entering the birth city and birth year.

For lines 17 and 18. With line 18 being indented should be the first hint that something is wrong with these two lines.

Looking deeper into lines 17 and 18. char* person[3] defines a three element array of type "char*", ("char" pointer), which can only hold an address to a "char", but you are trying to store a string in these elements and that does not work. Also these two lines have no business or reason to be in the program.

In line 18 you have "12/2/1980", but the variable name "birthYear" alludes to only needing the year not the rest.If you enter a full date you will need extra code to extract the year. Do not think that is what you want to de.

Before you can even think about output you need to get the program accepting input and to follow the directions.

againtry's first response gives you one possible solution for your input. One thing I would suggest is in place of using namespace std; I would use:
1
2
3
using std::cin;
using std::cout;
using std::string;

This will narrow the focus to what you need and also help you to learn what is in the standard name space.

Also I would put line 7 inside "main" because for now it does not need to be a global variable. "LIMIT" is an OK name, but I would be more likely to choose "MAX_ARRAY_SIZE" or just "MAXSIZE" as this tends to give the name a better understanding of what it is used for. Then I would give it the type "size_t", an alais for "unsigned int" because where it is used you can only use positive numbers and most of the time these numbers will be greater than (0) zero.

After that the only question is on line 13. Should the variable type be an "int" or a string". This would depend on what you need to do with the "birthYear" after it is entered.

When you correct your program to get the input then we can work on the output.

Hope that helps,

Andy
So this is what i changed my code to.

However, I'm not sure what i'm putting to get the input ? I already have input in my program

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std; 

const int MAXSIZE{ 5 };

int main()
{
	string* name = new string[MAXSIZE];
	string* birthcity = new string[MAXSIZE];
	int* birthyear = new int[MAXSIZE];

	for (int i = 0; i < MAXSIZE; i++)
	{
		cout << "Enter your name: ";
		getline(cin, name[i]);

		cout << "Enter your birth city: ";
		getline(cin, birthcity[i]);

		cout << "Enter your birth year: ";
		cin >> birthyear[i];

		cin.ignore(1000, '\n');
	}


	return 0;

}[code]
Last edited on
I don't want to be irritable just for the sake of it, but this is starting to make water-boarding look like a holiday.
why are you still talking to me ? I'm not talking to you
Because you haven't progressed beyond the code I gave you.
what's next? A copy of cout << "blah, blah";
This is a beginners forum. I stated right from the jump I"M A BEGINNER". I am not Bjarne Stroustrup. I only know the pure basics. Cout and CIN. Most of everything else i do not know. So yes, thats all the code that i have and i'm stuck CAUSE I DONT KNOW WHAT IM DOING
Your challenge is to take a risk and start writing some code. Then and only then will you learn anything.

You haven't advanced because you won't take a risk. Your tantrums and shouting don't help you.

So you can't input the details of 5 people? And once that's done, you can't print them out.

Wow!!
cblack618, pay no attention to againtry. He/she is a troll, although they did provide some helpful code.

The code you have so far looks good. Do you understand how it works?

If you understand how it works, then try to write code to print out the info for the people. I suggest that you get code that writes the data out first, without paying too much attention to format. Once you're printing it out successfully, you can worry about how to make it look nice.
cblack618 and againtry quit bickering with each other. It is very unproductive and childish and I do not think either of you are a child. Focus on the problem not the petty stuff.

Hello cblack618,

You are showing some progress.

I wrote the code this way:
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
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

//using namespace std; // <-- last resort
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/


// What I would suggest.
//using std::cin;
//using std::cout;
//using std::endl;
//using std::string;

int main()
{
	constexpr size_t MAXSIZE{ 5 };

	std::string* name = new std::string[MAXSIZE];
	std::string* birthCity = new std::string[MAXSIZE];
	int* birthYear = new int[MAXSIZE];


	for (int index = 0; index < MAXSIZE; index++)
	{
		std::cout << "      Enter your name: ";
		std::getline(std::cin, name[index]);

		std::cout << "Enter your birth city: ";
		std::getline(std::cin, birthCity[index]);

		std::cout << "Enter your birth year: ";
		std::cin >> birthYear[index];

		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	}
}

I changed the order of the include files because I have found that an alphabetical order helps to remind you if you have missed an include file. In the end order of the include files makes no difference, most of the time, this is just a suggestion.

For line 18: First it does not need to be a global variable in a one function program. The "constexpr" is available from C++11 standards on. If you can not use this you may be able to adjust the IDE and compiler to use the 2011 standards. I would suggest looking into this as the 2011 standards have many changes that make coding a bit easier and better. And should be the minimum standard that you use. Lastly the "size_t" is an alias for an "unsigned int". "size_t" is something you should get use to using because there are many places where it is useful , like; the size of an array that can only be a positive number and comparing it to the return value of several functions that return a type of "size_t". My point is not to explain every use, but to get you exposed to it.

Having said that my first question would be how much of this code do you understand and what needs better explained?

In the code I have been working with it now produces the output of:


                  Table Title Change as Needed
 ===============================================================

         Name               Birth City       Birth Year    Age
 ---------------------------------------------------------------
             John Doe        Columbus, OH       1950        70
             Jane Doe        Columbus, OH       1651       369
           Bob Smithe      Marysville, OH       1965        55
           Jack Sprat      Portsmouth, OH       1980        40
        Mary Horowitz        Deleware, OH       1990        30



 Press Enter to continue:


On line 2 notice where I made the typo which lead me to three revelations.

Looking back at the instructions:

3. Users are assumed to be born between the years 1900 and 2018, and should enter the year of birth
in one of the two formats 19XX and 20XX.

First the prompt would be better as std::cout << "Enter your birth year (19YY or 20YY): ";. This way you give the user an idea of what you need.

Second the year has to be within a certain range (1900 - 2018).

Third std::cin >> birthyear[i]; is formatted input meaning that "cin" expects a number to be entered. If you do not enter a number, (say a letter), "cin" will be put in a failed state and unusable the rest of the program.

To start with, the lines
1
2
std::cout << "Enter your birth year: ";
std::cin >> birthyear[i];

Need to be in a loop, (do/while, while or for) hint a for loop is not a good option here, to check the number entered for the proper range.

Before we get into the code that is necessary I wanted to give you the opportunity to figure out which loop would be best and to see what you come up with before we start working on the output.

Hope that helps,

Andy
This is what i have but getting 3 errors at 30 and 43

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
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

const int MAXSIZE{ 5 };

int main()
{
	string* name = new string[MAXSIZE];
	string* birthcity = new string[MAXSIZE];
	int* birthyear = new int[MAXSIZE];

	for (int i = 0; i < MAXSIZE; i++)
	{
		cout << "Enter your name: ";
		getline(cin, name[i]);

		cout << "Enter your birth city: ";
		getline(cin, birthcity[i]);

		cout << "Enter your birth year: ";
		cin >> birthyear[i];

		cin.ignore(1000, '\n');
	
	
		cout << "Displaying the Array Content" << endl;
		for (int i = 0, i < [MAXSIZE], i++)
	

		cout << "Name: " << name[i] <<
			"t/Birth city: " << birthcity[i] <<
			"t/Birth year: " << birthyear[i] << endl;
	}
	delete[] name, birthcity, birthyear;
	return 0;
}

return 0;
};
Your second for loop should be after the first one (after its ending brace).

The tabs in the display strings should be \t (not t/).

You need a separate delete[] statement for each array.
What does your compiler say? It is important to learn to understand those messages.

One compiler says:
 In function 'int main()':
30:21: error: expected initializer before '<' token
30:21: error: expected ';' before '<' token
30:21: error: expected primary-expression before '<' token
30:24: warning: capture of variable 'MAXSIZE' with non-automatic storage duration
7:11: note: 'const int MAXSIZE' declared here
 In lambda function:
30:32: error: expected '{' before ',' token
 In function 'int main()':
30:37: error: expected ';' before ')' token
37:28: warning: right operand of comma operator has no effect [-Wunused-value]
37:37: warning: right operand of comma operator has no effect [-Wunused-value]
 At global scope:
41:1: error: expected unqualified-id before 'return'
42:1: error: expected declaration before '}' token

It says a lot about line 30. Among them:
warning: capture of variable 'MAXSIZE' with non-automatic storage duration
 In lambda function:

This compiler supports C++11, which has lambda closure syntax.
The compiler thinks that the brackets [] start a lambda, but the rest of the line does not fulfill that expectation.

What is the purpose of the brackets on line 30?


There is a closing brace } on line 39. Where is the matching opening brace?
Your indentation is quite systematic. It does give a hint.
On line 10. Line 39 is the end of function main().
 At global scope:
41:1: error: expected unqualified-id before 'return'
42:1: error: expected declaration before '}' token

Lines 41 and 42 are outside of any function and therefore do not fit.


Line 37:
warning: right operand of comma operator has no effect [-Wunused-value]

Line 37 is like you had written:
1
2
auto pointer = (name, birthcity, birthyear);
delete[] pointer;

It deallocates only one of the three memory blocks.
You have to deallocate them separately.
Last edited on
can you take a look at this ?

I get it to ouput the data but its not a formatted table though. I need it to be a formatted table

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
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

//using namespace std; // <-- last resort
// The most recent post that is worth reading. http://www.cplusplus.com/forum/beginner/258335/


// What I would suggest.
//using std::cin;
//using std::cout;
//using std::endl;
//using std::string;

int main()
{
	constexpr size_t MAXSIZE{ 5 };

	std::string* name = new std::string[MAXSIZE];
	std::string* birthCity = new std::string[MAXSIZE];
	int* birthYear = new int[MAXSIZE];


	for (int index = 0; index < MAXSIZE; index++)
	{
		std::cout << "Enter your name: ";
		std::getline(std::cin, name[index]);

		std::cout << "Enter your birth city: ";
		std::getline(std::cin, birthCity[index]);

		std::cout << "Enter your birth year (19YY or 20YY)";
		std::cin >> birthYear[index];

		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	}

	for (int index = 0; index < MAXSIZE; index++)
	{
		std::cout;
		std::cout << "Name: " << name[index] <<
			"Birth City: " << birthCity[index] <<
			"Birth Year: " << birthYear[index];
	}

}
The std::setw has already been suggested to you.
table still isnt formatted

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
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

int main()
{
	constexpr size_t MAXSIZE{ 5 };

	std::string* name = new std::string[MAXSIZE];
	std::string* birthCity = new std::string[MAXSIZE];
	int* birthYear = new int[MAXSIZE];


	for (int index = 0; index < MAXSIZE; index++)
	{
		std::cout << "Enter your name: ";
		std::getline(std::cin, name[index]);

		std::cout << "Enter your birth city: ";
		std::getline(std::cin, birthCity[index]);

		std::cout << "Enter your birth year (19YY or 20YY): ";
		std::cin >> birthYear[index];

		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	}

	for (int index = 0; index < MAXSIZE; index++)
	{
		std::cout << setw(20) << name[index] << setw(5) << birthCity[index] << setw(5) << birthYear[index];
		std::cout << "Name: " << name[index] <<
			" \tBirth City: " << birthCity[index] <<
			"\tBirth Year: " << birthYear[index];
	}
	delete[] name;
	delete[] birthCity;
	delete[] birthYear;
};
You need std:: in front of all your setw's.
MAXSIZE should be const int (not const size_t).
The semicolon after the very last brace (end of main) is not needed.

Your output should be more like this:

1
2
3
4
5
6
    std::cout << "Name                  Birth City  Birth Year\n";
	std::cout << std::left; // set to left-justified
	for (int index = 0; index < MAXSIZE; index++)
		std::cout << std::setw(20) << name[index]      << "  "
		          << std::setw(10) << birthCity[index] << "  "
		          << std::setw(10) << birthYear[index] << "\n";

Hello,

It's repeatin the table 5 times. How can i stop that so that it only outputs the table once ?

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
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

int main()
{
	constexpr size_t MAXSIZE{ 5 };

	std::string* name = new std::string[MAXSIZE];
	std::string* birthCity = new std::string[MAXSIZE];
	int* birthYear = new int[MAXSIZE];


	for (int index = 0; index < MAXSIZE; index++)
	{
		std::cout << "Enter your name: ";
		std::getline(std::cin, name[index]);

		std::cout << "Enter your birth city: ";
		std::getline(std::cin, birthCity[index]);

		std::cout << "Enter your birth year (19YY or 20YY): ";
		std::cin >> birthYear[index];

		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	}

	for (int index = 0; index < MAXSIZE; index++)
	{
		std::cout << "Name                  Birth City  Birth Year\n";
		std::cout << std::left; // set to left-justified
		for (int index = 0; index < MAXSIZE; index++)
			std::cout << std::setw(20) << name[index] << "  "
			<< std::setw(10) << birthCity[index] << "  "
			<< std::setw(10) << birthYear[index] << "\n";


	}
	delete[] name;
	delete[] birthCity;
	delete[] birthYear;
};
Now you've wrapped the loop in another loop!
Just remove the outer loop (lines 29, 30, and 39 above).
How would I fancy and add a space between where the user inputs the information and the report below ? everything is all crammed togther

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
//Charles Blackwell CIS 211
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

int main()
{
	constexpr size_t MAXSIZE{ 5 };

	std::string* name = new std::string[MAXSIZE];
	std::string* birthCity = new std::string[MAXSIZE];
	int* birthYear = new int[MAXSIZE];

	//Input and For loop
	for (int index = 0; index < MAXSIZE; index++)
	{
		std::cout << "Enter your name: ";
		std::getline(std::cin, name[index]);

		std::cout << "Enter your birth city: ";
		std::getline(std::cin, birthCity[index]);

		std::cout << "Enter your birth year (19YY or 20YY): ";
		std::cin >> birthYear[index];

		std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	}

	{
		
		std::cout << "Name                  Birth City  Birth Year\n";
		std::cout << std::left; 
		for (int index = 0; index < MAXSIZE; index++)
			std::cout << std::setw(20) << name[index] << "  "
			<< std::setw(10) << birthCity[index] << "  "
			<< std::setw(10) << birthYear[index] << "\n";
	}
	
};
Pages: 123