Still sounds like getline will do the job and all you do is count up the alpha characters in the line to set the size of the array. length() might be the hard way to do the counting.
I just wanted to see how you would get rid of the spaces (actually was just curious)
I actually needed the spaces for the program I am trying to implement.
I am actually trying to set a pointer reference the first element in an array, and another pointer to reference the last element of the same array.
#include <iostream>
#include <string>
typedefchar* CharPointer;
usingnamespace std;
int main()
{
CharPointer first,last, diamond;
string astring;
int alength;
cout << "This program reverses a string entered.\n\n"
<< "Enter a string : \n";
getline(cin, astring);
alength = astring.length();
diamond = newchar[astring.length()]; //creates a dynamic array called diamond
first = diamond; //references the first element?
last = diamond + (astring.length()-1); //reference the last element
//adds a single char to an element of the array
for(int i = 0; i < astring.length(); i++)
diamond[i] = astring[i];
//prints the elements in the array(basically reprints //what was inputted)
for(int i = 0; i < astring.length(); i++)
cout << diamond[i];
cout <<"\nfirst : " << first;
cout << "\nlast : " << last;
return 0;
}
when i cout the pointer 'first' it reprints all of the elements in the array, and the 'last' pointer prints to the last element.
I guess my question is, is this syntax correct the way I have this program. I understand 'first' and 'last' aren't the best names but i chose them for the sake of this example.
I my plan is to actually flip the contents of the array to be printed out reversely using pointers.
I don't want to use any .reverse methods .
A couple of thoughts:
1. On face value at a quick glance, give it a go.
2. Have you thought of string tokenizers? eg strok();
3. If you are just reversing the line why not leave it as a string and just read the characters in reverse order, character by character, using standard <string> functions? If needs be if a character is a blank then ignore it or whatever. It seems you have overcomplicated the problem but, of course, I might have misunderstood what you are trying to do.
1) is my syntax correct for my pointes 'first' and 'last' to point to the first element, and last element in the array called 'diamond'?
2) my plan is to flip the the contents of the array to be reversed using pointers
3)why is my 'first' pointer printing everything in the array? versus the single element diamond[0]?
I put in a few diagnostics to make it clearer for me. The reason for the junk is because C-strings require '\0' as the last hidden character to terminate the string. You need to add that last character. Make sure diamond is created to accommodate the extra character.
Also char* diamond (no brackets) is a pointer to the whole array, by definition (new), whereas diamond[x] is the x-th character of that array just as you say.