Question:
How to create a program that stores names in C++?
dd_100in
2008-05-15 00:43:33 UTC
I have a program in which I have used a for loop to take user entered number of names in to an array.Now, what I want is, after the loop terminates, all the names should be displayed rather than in every iteration of the loop.Is a pointer to char array needed?Yes,or otherwise, how do I program it?
Three answers:
cja
2008-05-15 05:03:02 UTC
jplatt makes several good points about arrays and pointers, there are many pitfalls to be avoided. See below for how I would solve your problem in C++.



The STL jplatt refers to is the Standard Template Library, which has been part of the C++ standard library for many years. The containers provided are very good, and can free you from worries of bad pointers and buffer overruns that may be a problem in C.



I can appreciate your need, as a beginner, to learn how to write good code using simple arrays and pointers. Keep in mind, though, that with C++ there is a better way.



#include

#include

#include



using namespace std;



int main(int argc, char *argv[]) {

vector names;

string s;

bool done = false;



cout << "Enter names (CR to quit): " << endl;

while (done == false) {

getline(cin,s);

if ((done = (s.length() == 0)) == false) {

names.push_back(s);

}

}

vector::iterator i = names.begin();

while (i != names.end()) cout << *i++ << endl;



return 0;

}
jplatt39
2008-05-15 01:57:08 UTC
Pointers are dangerous and should be used as sparingly as possible. I am a C programmer who uses C++ and I am more comfortable with ASCIIZ strings than the STL string type, so I shall use the former and try to couch my answer in general terms, so it can be useful to both.



A string is a collection of chars. That means in an ASCIIZ string it is literally a collection, or array, of chars, and you just have to set aside enough memory to hold it (which I'm told the string type does). An array of arrays of chars is a two-dimensional array. This mean when you create an array of strings you are setting aside enough memory to handle all your strings. One shortcut with ASCIIZ strings used by selfish programmers who want to confuse everyone is rather than doing a Buffer[x][y] they will do a Buffer[x*y] and then enter each string at Buffer[start+=y] rather than Buffer[start][y] which makes sense. There are actually places (such as VGA programming) where that can make sense. But it's confusing.



Nevertheless the above points to a simple solution to your problem. By thinking of these names as an array of the data type array, if all you want to do is print them out after, then all you have to do is keep track of how large a block of text you are using -- how many names -- and use a for loop. If you are having a problem getting the names into a buffer of AsciiZ strings, use the cstring function strcpy(). For C++ strings check out this page:



http://www.cplusplus.com/reference/string/string/



What is dangerous about pointers is 1. They make it incredibly easy to continue manipulating data without checking to see if the data is in the range of memory you set up to hold it -- you don't want to overwrite program code or worse OS code -- and 2. they make it easy to perform actions which the compiler decides will be performed on a COPY of the address you are trying to perform it on -- I've seen "professional" work fail because the programmer used pointers rather than arrays.



So I'm being long-winded. WOULD YOU RATHER I SHOUT? Never, ever, do a pointer to a char array just to print out a series of names. Preferred is, set aside a two dimensional array. Acceptable is set aside enough space for a two-dimensional array and work inside that. You will find plenty of situations where pointers are helpful. This isn't one.



EDIT: Here is a C program which does what I tell you:



#include

#include

int main()

{



char Array[30][12];

char Buffer[30];

int i;

for (i=0;i<12;i++)

Array[i][0]='\0';

i=0;

while (i<12){

printf("Enter a word. Enter z to end:");

scanf("%s", &Array[i]);

if (strcmp(Array[i], "z")==0) break;

i++;}

for (i=0; i<12;i++)

printf("%s\n", Array[i]);

return 0;

}
?
2016-12-29 20:35:16 UTC
unlikely to offer you the respond rapidly on your homework. Create an array of string form to the dimensions you choose. Assign the worker names to the array. execute a for loop. for the time of each era in the loop output them to the console.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...