toupper and tolower error

closed account (4jzvC542)
I am stuck up in a seemingly simple program:
my homework task is:


Write a program that will ask user to enter his name.
Convert all uppercase to lowercase & vice-verse
(will have to use inbuilt ctype.h heade file and functions...



my code is :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void main()
{
	char x[256];
	bool y;
	std::cout << "Enter your full name : \n";
	std::cin.getline (x, 256);

	y = isalpha(x);
	if (y == true)
	{
		tolower(x);
		std::cout << x;
	}
	else
		toupper(x);
	        std::cout << x;
}



compiling it in MS Visual C++ 2006
i get 6 errors:
they are:

error C2664: 'isalpha' : cannot convert parameter 1 from 'char [256]' to 'int'error C2664: 'isalpha' : cannot convert parameter 1 from 'char [256]' to 'int'error C2664: 'isalpha' : cannot convert parameter 1 from 'char [256]' to 'int'

error C2143: syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '}'
error C2143: syntax error : missing ';' before '}'


please help me where i am wrong
and which is the correct way to code it...
my submission date is cumming near and i am stuck up
thanks in advance,
parjanya
MS Visual C++ 2006

Ha ha, I didn't know that version existed!

Well what you're doing wrong in the code is that you think tolower() and toupper() can convert an entire char array at once. This isn't the case, they can only convert one solitary char.

So what you need to do is: use a loop to iterate all elements of the char array x, check if alpha then check if lower or upper, then convert accordingly.

Code below wasn't tested.
size_t (which is usually an alias for long unsigned int) is meant to be used for sizes and array lengths, which is why we're using it instead of something like int.

1
2
3
4
5
6
7
8
9
10
11
12
13
for (size_t i=0, xl = strlen(x); i < xl; ++i)
{
    if (isalpha(x[i]))
    {
        if (isupper(x[i]))
            x[i] = tolower(x[i]);
        else
        if (islower(x[i]))
            x[i] = toupper(x[i]);
    }

    cout << x;
}
closed account (4jzvC542)
Absolutely Great...............that's all i can say
didn't knew about that single car thing
thank man @catfish
You need not to check whether a character is alpha or not.

The code can look the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cctype>

int main()
{
	char s[256];

	std::cout << "Enter your full name :";
	std::cin.getline( s, sizeof( s ) );

	char *p = s;
	while ( *p )
	{
		*p = std::isupper( *p ) ? std::tolower( *p ) : toupper( *p );
		++p;
	}

	std::cout << s << std::endl;
}
Last edited on
closed account (4jzvC542)
@catfish

MS Visual C++ 2006

not 2006 BUT ... :
MS Visual C++ 6.0 it 1998 version i think
closed account (4jzvC542)
I did it:
thanks to your great code:
here is my final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void main()
{
	char x[256];
	std::cout << "Enter your full name : \n";
	std::cin.getline (x, 256);
		for (size_t i=0, xl = strlen(x); i < xl; ++i)
{
    if (isalpha(x[i]))
    {
        if (isupper(x[i]))
            x[i] = tolower(x[i]);
        else
        if (islower(x[i]))
            x[i] = toupper(x[i]);
    }

	std::cout << x;
	std::cout <<"\n\n\n";
}
}



it converts to upper case

but the out put is like this

input : john

output :

John

JOhn

JOHn

JOHN


it shows each stage
not final product
any idea what to?
parjanya
Print x after the loop, not inside it.
Printing x instead of x[i] was my mistake, oh well.

MS Visual C++ 6.0 it 1998 version i think

I suggest you upgrade to Visual C++ Express 2012, or at the very least install service pack 6 for Visual C++ 6.0.

http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-products
http://www.microsoft.com/en-us/download/details.aspx?id=9183
closed account (4jzvC542)
@Catfish 4
i would very much like to upgrade but
my pc is really old: P4 512 mb ram 845 mainboard
its a great feat that VC++ 6.0 is working fine
my dad's going to buy me new pc soon..........
any way thanks a lot for
pointing out single char thing
damn my textbook they never nemtioned and i was
thanks a lot man........
Because you are learning C and C++, I suggest you bookmark the following sites for later reading:

http://www.cplusplus.com/reference/
http://en.cppreference.com/w/
http://www.icce.rug.nl/documents/cplusplus/
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
http://www.parashift.com/c++-faq/
http://yosefk.com/c++fqa/
http://c-faq.com/

my pc is really old: P4 512 mb ram 845 mainboard

That should be good enough for a lot of tools that are not fancy.

C compiler and IDE:
http://www.smorgasbordet.com/pellesc/

C and C++ compiler (command line tools):
http://nuwen.net/mingw.html

C and C++ compiler and IDE:
http://sourceforge.net/projects/orwelldevcpp/

Finally if all else fails, and as a last resort, you could try Bloodshed Dev-C++.
http://www.bloodshed.net/download.html

Bloodshed Dev-C++ is old, buggy but at least has good support for C++98 and C++03. The newest standard of C++ is C++11 (from 2011).
use int main() in C++ rather than void main();
Topic archived. No new replies allowed.