Pointers assignment

Pages: 123
You just have to run it and play with it.
some stuff you may need to know...
when running console programs at a console, you can redirect a text file into them and it is as if you typed it. this is great to run with the same input over and over but only type it up once. its more or less the same in unix and windows too; in windows a.exe < file.txt will do it. (and > will do the reverse and stuff all couts into a text file for you).

once you can easily run it over and over, start playing with the formatting.
'\t' is tab. you can use this to align things (on top of the width etc formatting of cout).
learn to print doubles with N decimal places and a fixed width so they look nice in a table.

if you want to clear the screen before making a pretty output, a simple way is:
system(command) ... on windows, that is system("cls") ... I forget unix, clear or something similar

if you want to keep the info but add some spacing, just cout << endl a few times to split the input and output visually.

Is that enough to get you where you want to be?




no i still dont get it. I added two space cout at 30 and 31 but there is still not a space between the user output and the 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
//Charles Blackwell CIS 211
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>

int main()
{
	constexpr size_t MAXSIZE{ 5 };
	//pointer array
	std::string* name = new std::string[MAXSIZE];
	std::string* birthCity = new std::string[MAXSIZE];
	int* birthYear = new int[MAXSIZE];

	//Input and For loop to get user input for 5 people
	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'); /
	};
	{
		std::cout << endl;
		std::cout << endl;
	};
	{
		//Formatted Table
		std::cout << "Name                  Birth City  Birth Year\n";
		std::cout << std::left; 
		std::cout << "________________________________________________";
		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";
	}
	
};
You need to pay more attention to the output of your compiler. The reason you don't see any change is that the code doesn't even compile. So you were running the old compiled code.

You need to put std:: in front of the endl's. And you have accidentally added a / at the end of line 27 above.
Last edited on
This code does compile. It just not adding the spaces i want
@handyPandy, Stop bickering? Who are you to order me around. Your namby pamby “Hope this helps” bullshit every time sounds like an Alice in Wonderland grovel from the phoniest of Tin Men.
you don't need {} around non-blocks, and this will get you into trouble as a habit because it will scope variables inside the {} pairs and that will break things if you did not do it by intent. The extra ;s on the brackets are also not useful. This stuff is visual clutter at best and buggy under some conditions (if you get ; happy and add extras you will invariably poke one on a loop or if statement and break something).

the 2 endls should have added 2 blank lines (spaces are something else, and go between words, in programming terms).

This makes me agree with dutch, are you sure you recompiled a clean version with the 2 endls in it?

/ I think is legal but you don't want it there. I think its the line continue 'operator', but I am not double checking, and I havent used that since macros became mostly obsolete.
you can double down. cout << endl << endl; or slightly better, cout << "\n\n";

If you compile by hand, delete the executable first thing when you start a compile. If you use an IDE, you can set it to do this on many.
Last edited on
can you reference specifc lines becuase i don't understand where i need to make changes
cblack618 wrote:
This code does compile.

The code you posted does not compile.
I am now assuming that you are in fact a troll.
Fuck off and die.
This code does compile weirdo

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

int main()
{
	constexpr size_t MAXSIZE{ 5 };
	//pointer array
	std::string* name = new std::string[MAXSIZE];
	std::string* birthCity = new std::string[MAXSIZE];
	int* birthYear = new int[MAXSIZE];

	//Input and For loop to get user input for 5 people
	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'); /
	};
	{
		std::cout << endl;
		std::cout << endl;
	};
	{
		//Formatted Table
		std::cout << "Name                  Birth City  Birth Year\n";
		std::cout << std::left; 
		std::cout << "________________________________________________";
		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";
	}
	
}
They say if a gibbon taps away at a typewriter long enough it will produce a Shakespeare play ...
This code does compile weirdo

Really? I get several errors and a couple of warnings.

1
2
3
4
5
6
7
8
9
main.cpp||In function ‘int main()’:|
main.cpp|16|warning: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Wsign-compare]|
main.cpp|27|error: expected primary-expression before ‘/’ token|
main.cpp|28|error: expected primary-expression before ‘}’ token|
main.cpp|30|error: ‘endl’ was not declared in this scope|
main.cpp|30|note: suggested alternative:|
/opt/gcc-8.1.0/include/c++/8.1.0/ostream|590|note:   ‘std::endl’|
main.cpp|38|warning: comparison of integer expressions of different signedness: ‘int’ and ‘size_t’ {aka ‘long unsigned int’} [-Wsign-compare]|
||=== Build failed: 3 error(s), 2 warning(s) (0 minute(s), 0 second(s))
Sorry you right it’s not running. But why when I try to run it on visual studio it goes ? Is it running the last successful build ?
Last edited on
Well then maybe you should cut and paste the code above into your system and try again.

can you help me add spacing between the output from the user input and the formatted report ?

everything is all jammed together with no spacing ?
Hello cblack618,

The program is only as good as what you tell it to do.

Have a look at this:
1
2
3
4
5
6
7
8
9
10
11
12
13
//Formatted Table
//std::cout << std::string(40, ' ') << "Birth\n";
std::cout << "    Name              Birth City     Birth Year\n";
std::cout << std::string(47, '_') << '\n'; // <--- Does the same as the next line.
//std::cout << "________________________________________________\n";

for (size_t index = 0; index < MAXSIZE; index++)
	std::cout
		<< std::left
		<< std::setw(20) << name[index] /*<< "  "*/
		<< std::setw(20) << birthCity[index] /*<< "  "*/
		<< std::right // <--- Better to set numbers to the right.
		<< std::setw(4) << birthYear[index] << '\n';

On lines 10 and 11 I used the setw's to control the spacing. The end of the line is not needed unless you want extra space. Since you are not using a floating point number the setw in line 13 could be omitted and then line 12 would not be needed.

Andy

Edit
Last edited on
Thank you Andy. I appreciate this so much
Sorry you right it’s not running. But why when I try to run it on visual studio it goes ? Is it running the last successful build ?

Yes. If you build an executable program, and then you hit compile on any compiler, and your recent edits make this new compile fail, you can still run the executable program from earlier. And, it won't do the new stuff that did not compile in the old program version's executable.
Last edited on
Topic archived. No new replies allowed.
Pages: 123