#include<iostream>
#include<string>
using namespace std;



void main()
{
	char *inputString;
	inputString=new char[];

	cout<<"Enter your string:"<<endl;
	cin>>inputString;
	//int stringLength=inputString.length();

	int stringLength=strlen(inputString); 
    inputString=new char[stringLength];
	cout<<stringLength<<endl;


for(int i=0; i<stringLength; i++)
{
	if(inputString[i]>='a' && inputString[i]<='z')
		{
			if (inputString[i] != ' ')
				{
					inputString[i]=toupper(inputString[i]);
					cout<<inputString[i];
				}
			else
				cout<<inputString[i];
		}
	else
	cout<<inputString[i];
}


cout<<endl;
}

First it takes only the first word, and second I feel there's a big mess with the dynamic arrays!

yes I'm a beginner :$ any help pointing out my mistakes?

do you want your code to read in string by string or the whole word??

do you want your code to read in string by string or the whole word??

word by word!
i'm not sure!

it should allow the user to input any string and output it all in uppercase

and I SHOULD use dynamic arrays :S

The problem is that you need to know how long the string is before you can allocate memory for it, and you need to input the string and store it somewhere before you can get the length... :confused: That's why the string class is so great, it does all of that for you by growing dynamically. If you don't want to use the string class, you have to simulate the same behavior by reading and allocating a bit at a time:

#include <iostream>
#include <cstring>

char *getString();

int main()
{
  char *p;

  while ((p = getString()) != 0) {
    std::cout << p << '\n';
    delete[] p;
  }
}

char *getString()
{
  char *result = 0;
  int size = 0;
  char ch;

  while (std::cin.get(ch) && ch != '\n') {
    ++size;

    // Don't forget to add 1 for the null character
    char *temp = new char[size + 1];

    if (result != 0)
      std::strcpy(temp, result);

    temp[size - 1] = ch;
    temp[size] = '\0';

    // Free the old memory and reset to the new
    delete[] result;
    result = temp;
  }

  return result;
}

There are a bunch of ways to do it, some more efficient and some less so. This one is pretty inefficient, but it's very simple.

The problem is that you need to know how long the string is before you can allocate memory for it, and you need to input the string and store it somewhere before you can get the length... :confused: That's why the string class is so great, it does all of that for you by growing dynamically. If you don't want to use the string class, you have to simulate the same behavior by reading and allocating a bit at a time:

#include <iostream>
#include <cstring>

char *getString();

int main()
{
  char *p;

  while ((p = getString()) != 0) {
    std::cout << p << '\n';
    delete[] p;
  }
}

char *getString()
{
  char *result = 0;
  int size = 0;
  char ch;

  while (std::cin.get(ch) && ch != '\n') {
    ++size;

    // Don't forget to add 1 for the null character
    char *temp = new char[size + 1];

    if (result != 0)
      std::strcpy(temp, result);

    temp[size - 1] = ch;
    temp[size] = '\0';

    // Free the old memory and reset to the new
    delete[] result;
    result = temp;
  }

  return result;
}

There are a bunch of ways to do it, some more efficient and some less so. This one is pretty inefficient, but it's very simple.

error C2039: 'strcpy' : is not a member of 'std'
:(

I want to run the program to understand it more! It's a bit complicated for me!

And the code I posted earlier does convert the input to uppercase but the problem is that it does this to the first word only

for example:

if the input was

DeeRewdfj Sdaka

the output:

DEEREWDFJ ========

#include <iostream>
#include <cstring>
#include <string>

char *getString();


void main()
{
  char *p;

  while ((p = getString()) != 0) 
  {
    std::cout << p << '\n';
    delete[] p;
  }

}

char *getString()
{
  char *result = 0;
  int size = 0;
  char ch;

  while (std::cin.get(ch) && ch != '\n') 
  {
    ++size;

    // Don't forget to add 1 for the null character
    char *temp = new char[size + 1];

    if (result != 0)
      strcpy(temp, result);

    temp[size - 1] = ch;
    temp[size] = '\0';

    // Free the old memory and reset to the new
    delete[] result;
    result = temp;
  }
  return result;
}

it works this way, thank you so much :D i just need to understand it more now and add toupper to it

but if you could tell me if I did anything right the first time, so I can know what to concentrate on

don't tell me all of it was wrong :/

> error C2039: 'strcpy' : is not a member of 'std'
Some compilers get it wrong. strcpy is a member of the std namespace if you include <cstring>, but not if you include <string.h>.

> And the code I posted earlier does convert the input to uppercase
Just because it seemed to work when you ran it doesn't make it any less wrong. The problem is that you didn't allocate enough memory to the pointer before filling it with characters. What you really did was rely on your compiler's mistake--it should have told you that inputString=new char[]; is illegal--and clobbered the memory starting at whatever address was stored in inputString. The next time you run your program it could crash, and the time after that it could send nasty emails to your boss. Or it could work perfectly until you try to sell it to someone and then bomb at the demo. ;)

> the problem is that it does this to the first word only
That's because you used >> to read a string from cin. The >> operator stops reading at whitespace. What you really wanted was cin.getline because it stops on a newline character by default. But that's the next problem after you get your memory straight. ;)

> void main()
I don't care, but some people get all uppity about how you define main, and using void as the return type is a good way to draw flames from those people. :(

> don't tell me all of it was wrong :/
I wouldn't tell you that because it was mostly right. The only real problem was inputString=new char[]; , like I described above.

Some compilers get it wrong. strcpy is a member of the std namespace if you include <cstring>, but not if you include <string.h>.

I removed std:: and it worked fine

Just because it seemed to work when you ran it doesn't make it any less wrong. The problem is that you didn't allocate enough memory to the pointer before filling it with characters. What you really did was rely on your compiler's mistake--it should have told you that inputString=new char[]; is illegal--and clobbered the memory starting at whatever address was stored in inputString. The next time you run your program it could crash, and the time after that it could send nasty emails to your boss. Or it could work perfectly until you try to sell it to someone and then bomb at the demo. ;)

lol I got that :D no more inputString=new char[];
but what if I gave the array a size of let's say 1000 and changed it later to the size of the string's length???

That's because you used >> to read a string from cin. The >> operator stops reading at whitespace. What you really wanted was cin.getline because it stops on a newline character by default. But that's the next problem after you get your memory straight. ;)

what about doing this

cin>>noskipws>>inputString;

I don't care, but some people get all uppity about how you define main, and using void as the return type is a good way to draw flames from those people. :(

I didn't mean anything by reposting your code, just wanted to let you know that I managed the error and the program worked fine, trying not to tire you more.

I wouldn't tell you that because it was mostly right. The only real problem was inputString=new char[];, like I described above.

> but what if I gave the array a size of let's say 1000 and
> changed it later to the size of the string's length???
No problemo! Well, mostly. ;) If you have lines longer than 999 characters, the program will end up accidentally splitting the long line into multiple lines. If that's ok for your program then it's ok for Edward, but if it's not ok for your program, then it's a bug.

> what about doing this
> cin>>noskipws>>inputString;
noskipws tells cin what to do at the beginning of a word, it doesn't change what cin does at the end of a word. That's not to say you can't force >> to ignore whitespace as an end of word delimiter, you can. But it's a pretty advanced solution and not really worth it. The idea is to trick the character traits into thinking that certain whitespace characters aren't really whitespace. cin.getline is much easier.

> I didn't mean anything by reposting your code
Ed posted the code for you, you can do whatever you want with it. :) Edward's comment was a warning that in any code you post, there are people who will jump on you for returning void from main instead of int.

> trying not to tire you more
Edward doesn't get tired of talking about C++. :D

No problemo! Well, mostly. ;) If you have lines longer than 999 characters, the program will end up accidentally splitting the long line into multiple lines. If that's ok for your program then it's ok for Edward, but if it's not ok for your program, then it's a bug.
It doesn't contradict with the purpose of dynamic arrays, does it?
I remember our doctor telling us that dynamic arrays solve the problem of having to give the array size before letting the user input it or so.


noskipws tells cin what to do at the beginning of a word, it doesn't change what cin does at the end of a word. That's not to say you can't force >> to ignore whitespace as an end of word delimiter, you can. But it's a pretty advanced solution and not really worth it. The idea is to trick the character traits into thinking that certain whitespace characters aren't really whitespace. cin.getline is much easier.
aha that makes sense :)


Ed posted the code for you, you can do whatever you want with it. :) Edward's comment was a warning that in any code you post, there are people who will jump on you for returning void from main instead of int.
Edward doesn't get tired of talking about C++. :D
OK great, so I can ask stupid questions as much as I want :)
what's the meaning of this while ((p = getString()) != 0)
and why using 0??

thank you so much even if you didn't answer, you were very helpful I appreciate it

> It doesn't contradict with the purpose of dynamic arrays, does it?
It does, actually. The purpose of dynamic arrays in this case is to avoid a set limit on the array. If you put a limit on the array and then shrink it, you still haven't solved the original problem, but your memory usage is a little more streamlined.

> OK great, so I can ask stupid questions as much as I want
The only stupid question is the one you don't ask. :)

> what's the meaning of this while ((p = getString()) != 0)
getString returns a null pointer if it fails to read a line either because there was some kind of error, or the user typed an EOF key combination like Ctrl+Z.

> and why using 0??
getString returns a pointer right? Well, a pointer with the value of 0 is called a null pointer. It's a predictable but unusable pointer value that you can use for error checking or control flow.

> you were very helpful I appreciate it
Edward appreciates being appreciated. ;)

> It doesn't contradict with the purpose of dynamic arrays, does it?
It does, actually. The purpose of dynamic arrays in this case is to avoid a set limit on the array. If you put a limit on the array and then shrink it, you still haven't solved the original problem, but your memory usage is a little more streamlined.

> OK great, so I can ask stupid questions as much as I want
The only stupid question is the one you don't ask. :)

> what's the meaning of this while ((p = getString()) != 0)
getString returns a null pointer if it fails to read a line either because there was some kind of error, or the user typed an EOF key combination like Ctrl+Z.

> and why using 0??
getString returns a pointer right? Well, a pointer with the value of 0 is called a null pointer. It's a predictable but unusable pointer value that you can use for error checking or control flow.

> you were very helpful I appreciate it
Edward appreciates being appreciated. ;)

OK cool :) thank you

char *temp = new char;
by doing this wouldn't you creat arrays of the number 'size' and waste them?

Yes, if size weren't exactly how many characters Ed needed. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.