Why doesn't this work?

S

Silver

I compile it with vstudio .Net, it runs but when I enter a string, I get an
error message

//Program creates char d, sets it equal to lowercase letter

//Converts it to uppercase and outputs it

#include <ctype.h>

#include <iostream.h>

int main()

{

char d[20];

cin >> d;

for (int i = 0; i < 20; i++)

{

if (isalpha(d))

d=toupper(d);

}


cout<<d;

return 0;

}
 
R

Ron Natalie

Silver said:
I compile it with vstudio .Net, it runs but when I enter a string, I get an
error message
Can't vouch for .NET, but if you manage to type more than 19 characters without
a whitespace character you will cause undefined behavior. The following is safer.


#include <ctype.h>
#include <iostream>
#include <string>
using namespace std;

int main() {
string d;
cin >> d;
....
 
J

Jon Bell

I compile it with vstudio .Net, it runs but when I enter a string, I get an
error message

Don't ypu think it would help us to know what the error message *is*?
 
H

Heinz Ozwirk

:
: > I compile it with vstudio .Net, it runs but when I enter a string, I get an
: > error message
: >
: Can't vouch for .NET, but if you manage to type more than 19 characters without
: a whitespace character you will cause undefined behavior.

And if there are less than 19 characters uninitialized data will be passed to isalpha, which again causes undefined behavior. Also, .NET's is* functions assert if their argument is not EOF or in the range 0-255.

Heinz
 
S

Silver

I also get an error message when I run this (no error during compile/build)

//Program creates char d, sets it equal to lowercase letter

//Converts it to uppercase and outputs it

#include <ctype.h>

#include <iostream.h>

int main()

{

char d[20];

cin >> d;

for (int i = 0; i < 20; i++)

{

if (isalpha(d))

d=toupper(d);

}


cout<<d;

return 0;

}

The error message is :

Debug Assertion Failed!
Program c:\...\test.exe
File : isctype.c
Line 56

Expression : (unsigned)(c+1) <= 256


Should I work with VC++ 6 instead of .NET ( I 'm still learning the
language, I won't write an application soon)?
 
R

Rolf Magnus

Silver said:
I also get an error message when I run this (no error during
compile/build)

//Program creates char d, sets it equal to lowercase letter

//Converts it to uppercase and outputs it

Try the following modifications:
#include <ctype.h>

#include <iostream.h>

int main()

{

char d[20];

char d[20] = {0};
cin >> d;

for (int i = 0; i < 20; i++)

{

if (isalpha(d))

d=toupper(d);



if (isalpha((usigned char)d)
d = toupper((unsigned char)d);
}


cout<<d;

return 0;

}

The error message is :

Debug Assertion Failed!
Program c:\...\test.exe
File : isctype.c
Line 56

Expression : (unsigned)(c+1) <= 256

isalpha and toupper need the character value converted to an unsigned
char. I guess char is signed in .NET, so you're providing wrong values.
Should I work with VC++ 6 instead of .NET ( I 'm still learning the
language, I won't write an application soon)?

No, just always remember to only provide unsigned char values to any is*
and to* function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top