It is taking in the amount of responses, and only displaying one letter a multiple of times. How can I change it up to read in each name. So if I choose 3, read in "Jim, Bob, Joe", and then reitterate my answer. (cannot use strings... this is for a past due project, i want to understand what is needed so I can complete the final exam correctly in May)
if i understand you, you need to input some data(names), and after that you chose one number and need the name that is entered at that point( let us say the third name entered).
The program should look like this?
enter a name: Jim
enter a name: Bob
enter a name:Joe
I think what he meant is that you input the number of names, and then the names.
So. Here's what you can do: after the user inputs the number (n), create a char* array of size n, and put a function in a loop that repeats n times. On each iteration, the loop would get the name and store it in position n as well.
Then, for show, you can use another loop to list the names it got. It would be similar to the first one.
#include <iostream>
usingnamespace std;
void main ()
{
int NumofNames;
cout << "How many names do you wish to enter? " << endl;
cin >> NumofNames;
char ** TwoDArray;
char NumCols;
cout << "Enter " << NumofNames << " names to sort: " << endl;
cin >> NumCols;
TwoDArray = newchar * [NumCols]; // create the coloumns pointers first
for (int i = 0; i < NumCols; i++)
TwoDArray [i] = newchar [NumCols]; //creates an array for each row of my 2d array
NumCols = NumCols;
cout << "You entered: " << NumCols << endl;
for (int i = 0; i < NumCols; i++)
delete [] TwoDArray [i]; // erases the array's created for each row of the original array
delete [] TwoDArray; // erases the original array
}
I'd have used char * PsudoStringArray[] rather than char ** TwoPointerArray, but...
You should use a for loop around your second pair of cout and cin statements. Decrement NumofNames per iteration, and make sure it's greater than zero. You can even use NumofNames' value to determine where in the array the name is stored.
i may be wrong bzt you are using char numcols, and a char type variable can store just one char sign, wich means that if u input Alex, the char stores just A, try using sometging like char name[50], and after geting the data u need (the name) you should copy it to a char[] or char* that has exactly that amount of space that the entered name needs. and you should define that char name[50] in the input loop so it is automaticly deleted after the end of the loop.
All well, I was hoping you'd notice that you were putting a character array into a character. ;)
You can input directly into char * PsudoStringArray[length], though. You don't need another char.
Seeing as you might have some problem solving for the alphabetical order of these, try converting each individual character to an integer. Then, create another array that lists the order, so int arr[1] would store the number that you can plug into PSA[] to get the word, and would point to the one that would be at the top of the alphabetical stack, arr[2] would be the location of the one next down. To sort...
You'll need to run this several times and will need a special for loop for this. However...
Experienced C++ programmers: YES, I know there are vastly more efficient algorithms that can be run in log-space time, but a Bubble sort is easy to implement.
Still having issue... been setting this up differently, and it spits out some crud then seeks out some random number in memory...? I think my for statement is wrong in the (int main). What should my input be for cin? I want to say the Create2DArray, since NumCols is just my length, and NumOfNames is the number of names, not the actual char input...? Can someone compile and see this error?
and the other thing is if u want to print a pointer's addres then u write cout << pointer; , but if u want to printe the data at that addres,: cout << *pointer;
Whoops. For your for loop in main(), did you mean i < NumofNames;?
(After change)
Note what justAbeginner said. numCols is an integer, not a char []. In fact, you need numCols for something else it seems...
Try putting the names in a 2d character array... which it seems you have. Though I'd use char [][]...
I'd have used a char TwoDArray[NumofNames][20] in place of your char ** TwoDArray. However...
Do not input shtuff into NumCols. The types are incompatible. Also, I think using NumofNames in place of arraySize in i < arraySize
Is in order. Else, your loop will run 20 times regardless of how many names need to be entered.
With my method you could input what you get from cin directly into the TwoDArray[i]. With your method it's also possible, but I'm not 100% sure how I'd go about it.