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...
#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
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]);
elseif (islower(x[i]))
x[i] = toupper(x[i]);
}
cout << x;
}
@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........