Why doesn't this work?

Discussion in 'C++' started by Silver, Nov 14, 2003.

  1. Silver

    Silver Guest

    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;

    }
    Silver, Nov 14, 2003
    #1
    1. Advertising

  2. Silver

    Ron Natalie Guest

    "Silver" <> wrote in message news:bp2soa$l8j$...
    > 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;
    ....
    Ron Natalie, Nov 14, 2003
    #2
    1. Advertising

  3. hi!

    > I compile it with vstudio .Net, it runs but when I enter a string, I get

    an
    > error message

    and that message is..???

    regards,
    sev
    Severin Ecker, Nov 14, 2003
    #3
  4. Silver

    Jon Bell Guest

    In article <bp2soa$l8j$>, Silver <> wrote:
    >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*?

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
    Jon Bell, Nov 14, 2003
    #4
  5. Silver

    Heinz Ozwirk Guest

    "Ron Natalie" <> schrieb im Newsbeitrag news:3fb4f94b$0$25315$...
    :
    : "Silver" <> wrote in message news:bp2soa$l8j$...
    : > 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
    Heinz Ozwirk, Nov 15, 2003
    #5
  6. Silver

    Silver Guest

    The error message is:

    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)?
    Silver, Nov 15, 2003
    #6
  7. Silver

    Rolf Magnus Guest

    Re: The error message is:

    Silver wrote:

    > 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.
    Rolf Magnus, Nov 15, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Prowak

    Why oh why doesn't my data view work?

    David Prowak, Jan 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    724
    Alvin Bruney [MVP]
    Jan 30, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    862
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  4. Sara
    Replies:
    6
    Views:
    249
    John W. Krahn
    Apr 12, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    246
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page