struct value
{
	char c[29];

};

char toLower(char na)
{
char c;
char str[29];
int i=0;
	while (na[i])
  {
    c=word[i];
     str= (char)(tolower(c));
    i++;
  }

return (str);
}

[B][U]in the main program[/U][/B]

cout<<"Enter The your name: "<<endl;
			cin>>v.c;
		
			toLower(v.c);

This is what i wrote to convert the insert text to lower case, but it doesn't work, can some one help me write the code for this, all what i want to do is to convert the text entered by the user to lowercase

question 2

when the user inputs a text he might input two words, i want the user to enter only one word, so i want to write a function where it returns a warning if the user enters 2 words.

please help me with the code :( :(

Member Avatar for iamthwee

Maybe it is because your returning a char instead of a char array.

Maybe it is because your returning a char instead of a char array.

i changed it, but it doesn't work

i changed it, but it doesn't work

return (str[29]); but no use still errors

return (str[29]); but no use still errors

thats a char your returning, still not a char array.

thats a char your returning, still not a char array.

but how do u return a char array .. is all my other codes okk ...

Are you not allowed to use strings or something? If you are allowed, below gives you an example of what you want (albeit with use of string), it passes each letter in a string to the "tolower()" function 1 by 1, which changes the characters, if it meets a space on it's way, it resizes the string to the size of i.

string toLower(string name)
{
	for (unsigned int i = 1; i < name.length(); i++) //Leaves 1st letter in name as is. (i = 0 for all of name to be lowercase)
	{
		if (name[i] == ' ')
			name.resize(i);
		name[i] = tolower(name[i]);
	}

	return name;
}

Hmm shouldn't toLower(v.c) be more like v.c = toLower(v.c) to get the return anyway?

cin >> char_array extracts only one word from input stream (upto whitespace occured - blank, tab or newline). The rest of typed string will been extracted with the next operator >> calls.
For example if the user typed "Happy Guppy" then only Happy will be extracted from cin. To discard a tail of unnecessary input, call cin.ignore(1000,'\n'); .
But if an user type:
IwantToPunishYouForCarelessProgramming
and press Enter, your program crashed because targeted array has a size 29 bytes only.
To avoid (alas, so common) this buffer overload error, always set max extraction width by (in your case) the call of another useful istream member function:

cin.width(sizeof v.c);
cin >> v.c;
cin.ignore(1000,'\n');

Of course, better use std::string variables as input targets.

Finally, two obvious functions for char arrays and std::string:

char* toLower(char* pstr)
{
    char* p = pstr;
    if (p)
    while (int c = *p)
        *p++ = tolower(c);
    return pstr;
}

std::string& toLower(std::string& s)
{
    for (size_t i = 0; i < s.length(); ++i)
        s[i] = tolower(s[i]);
    return s;
}

cin >> char_array extracts only one word from input stream (upto whitespace occured - blank, tab or newline). The rest of typed string will been extracted with the next operator >> calls.
For example if the user typed "Happy Guppy" then only Happy will be extracted from cin. To discard a tail of unnecessary input, call cin.ignore(1000,'\n'); .
But if an user type:
IwantToPunishYouForCarelessProgramming
and press Enter, your program crashed because targeted array has a size 29 bytes only.
To avoid (alas, so common) this buffer overload error, always set max extraction width by (in your case) the call of another useful istream member function:

cin.width(sizeof v.c);
cin >> v.c;
cin.ignore(1000,'\n');

Of course, better use std::string variables as input targets.

Finally, two obvious functions for char arrays and std::string:

char* toLower(char* pstr)
{
    char* p = pstr;
    if (p)
    while (int c = *p)
        *p++ = tolower(c);
    return pstr;
}

std::string& toLower(std::string& s)
{
    for (size_t i = 0; i < s.length(); ++i)
        s[i] = tolower(s[i]);
    return s;
}

IS WIDTH (cin.width(sizeof v.c);) A FUNCTION DEFINED BY THE LANGUAGE ?

Didn't you know that the streamsize width(streamsize wide) function IS DEFINED BY THE C++ LANGUAGE STANDARD? ;)

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.