Pointers assignment

Pages: 123
Jan 14, 2020 at 2:22am
Hello, I need help with this pointers assignment. I'm a beginner at c++. So please don't have high expectations of my knowledge. I have attached what I started but it's all wrong and i need help

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. Be sure to include comments throughout your code where appropriate. The data should be stored in dynamically created parallel arrays. 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. Output the data from the arrays in 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
  #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;
}
Jan 14, 2020 at 2:43am
A start, you should be able to comment and complete it. if not have a read of the tutorials on this site. You might also like to find out why the ignore(...) line is required in order to explaining it to your prof/teacher/tutor when they ask??

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

using namespace std; // <-- last resort

const int LIMIT{5};

int main()
{
    string* name = new string[LIMIT];
    string* birthcity = new string[LIMIT];
    int* birthyear = new int[LIMIT];
    
    for(int i = 0; i < LIMIT; 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');
    }
    
    // blah blah
    
    return 0;
}
Jan 14, 2020 at 3:20am
Hello

Thank you so much for your help. How would I get it to Output the data from the arrays in a formatted table ?
Jan 14, 2020 at 3:37am
How would I get it to Output the data from the arrays in a formatted table ?


This is where <iomanip> comes into play.

You will need to use setw() function at least.
See: http://www.cplusplus.com/reference/iomanip/setw/

cout << setw(20) << name[3] << setw[5] << birthcity[3] blah blah

You can use the right/left functions in <iomanip> to right/left justify the entries.
http://www.cplusplus.com/reference/ios/right/?kw=right
Jan 14, 2020 at 4:07am
BTW Once you see you way clear on that you can then go on and thoroughly work through
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.


There's obviously a test involved and a string might be useful instead of an int. But then again ...
Jan 14, 2020 at 4:09am
As I mentioned, I’m a very new beginner. So I’m still stuck. I don’t know how to proceed. I’ll have to get more help from someplace else
Jan 14, 2020 at 4:18am
OK, best wishes ... bye :)
Jan 14, 2020 at 4:27am
Bye
Jan 14, 2020 at 4:41am
cblack618 wrote:
Bye

Good riddance.
Jan 14, 2020 at 11:16am
The data should be stored in dynamically created parallel arrays.

I think your teacher meant C-style char arrays (I may be wrong).
Jan 14, 2020 at 11:49am
HTF would you know what the teacher is thinking?
Jan 14, 2020 at 12:18pm
I was right he didn't know after all - not a schmik - just wounded pride :)
Jan 14, 2020 at 4:23pm
Hello cblack618,

Using againtry's code as a starting place I did manage to come up with output:

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

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



 Press Enter to continue:

So there is still hope.

Andy
Jan 14, 2020 at 5:05pm
How though ? What code did you use
Jan 14, 2020 at 5:16pm
How though ? What code did you use

I SAID GOOD DAY, SIR!
Jan 14, 2020 at 5:19pm
aint nobody talking to you
Jan 14, 2020 at 5:38pm
aint nobody talking to you

Ain't nobody writing your code for you, you pathetic fucking leach!
Jan 14, 2020 at 5:55pm
ok tough keyboard warrior
Jan 14, 2020 at 6:39pm
Listen clack618, just green tick the thread and go. People have been very kind to you but the free ride is over.
Jan 14, 2020 at 7:01pm
Hello cblack618,

againtry has given you some very nice code to start with along with some links to help with the output.

Now it is your time to write some code and present it if you are having problems.

I do not think that you will find anyone that will write the whole program for you.

Looking at the instructions that you posted I feel that there is a lot missing. The missing parts would help to figure out what the program should instead of having to guess at it.

As far as the output goes first you need to collect the input before you can deal with the output. That is where http://www.cplusplus.com/reference/iomanip/setw/ becomes useful.

Andy
Pages: 123