Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,041 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,593 people online right now. Registration is fast and FREE... Join Now!




char and c-string not playing nice

 
Reply to this topicStart new topic

char and c-string not playing nice, trying to get length and isdigit

ddollar
2 Feb, 2008 - 10:56 PM
Post #1

New D.I.C Head
*

Joined: 16 Jan, 2008
Posts: 38



Thanked: 1 times
My Contributions
Entry should be one key-stroke and not a digit. I tried Char but couldn't get the length, so I changed it; now i'm having problem counting keystrokes. How do I do both? The call to tooMany works great although I didn't show it here. Any attempt I make at isdigit, is not. It's a char.

If I change char grade[3] to char grade, I can use isdigit but can't get the length with strlen. I'm just stuck.

Here I tried to use atoi to change the char to an int but that didn't work either.

CODE

int main()
{
        char grade[3];
        char letter;
        // Collect first entry.
        cout << "What is your first letter grade? ";
        cin.get (grade,3);

        letter = (atoi(grade));
                cout << letter;

        if (!tooMany(strlen(grade)))

        cout << "\nnext part";

        cout << endl;
        cout << "Press any key to quit...";
        getch();

        return 0;
}

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Char And C-string Not Playing Nice
2 Feb, 2008 - 11:22 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
You can still use isdigit, but you will have to apply it element-by-element in a loop.

If you just need the first character entered, however, you can still store the input in a char, and then use cin.ignore() to toss all of the characters remaining in the stream.
User is offlineProfile CardPM
+Quote Post

WXY
RE: Char And C-string Not Playing Nice
2 Feb, 2008 - 11:26 PM
Post #3

D.I.C Head
Group Icon

Joined: 2 Jan, 2008
Posts: 85


Dream Kudos: 50
My Contributions
If I remember right, strlen requires the zero byte terminator in your string, however basic_istream::get() don't append one to your buffer, which could be why strlen is failing or producing unexpected results.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Char And C-string Not Playing Nice
2 Feb, 2008 - 11:59 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
The get method does append a null terminator, at least according to the docs at cplusplus.com and msdn. I think that the problem with strlen() that ddollar was describing was the inability to use strlen with a single character (rather than an array), to test how many characters were entered by the user of the program.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:50PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month