#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
char* saying;
int n;
int x;
int main (/*int nArg, char* pszArgs[]*/)
{
//attempt to read a string of variable length
cout << "here goes nothing\n";
cin>>n;
cin.width(++n);
saying = new char[n];
cout << endl;
cin >> *saying;
cout << endl;
x = n;
n = 0;
while (n <= x)
{
cout<< *(saying + n);
n++;
}
delete [] saying;
return 0;
}
I am new to C++ and am attempting to teach myself how to use pointers to create variable length arrays. When i run this program it successfully stores the data from cin to *saying (or at least i think it dose) but when i go to display the content of the array i get the first char returned and then multiple garbage letters. for example "====2z".
i tried changeing line 23 to:
cout<< *saying;
and deleting the while loop but then i only got the first letter out.
What could i do to make the program display all of what the user inputs, and i don't want the answer "use #include <string>".
another thing i would like to know is how do i read how long the data is that the user inputs with out storing it in a string and without asking?
any help is appreciated,
thanks
The Blue Phoenix

New Topic/Question
Reply




MultiQuote








|