Archived

This topic is now archived and is closed to further replies.

ThomasSauder

Really confusing problem

Recommended Posts

/* ''01 Main'' */ #include <iostream> main (void) { /* Variable declerations */ unsigned char Age; long StartEnergy; char CharacterType; using namespace std; /* Get the Information */ cout << "What is your character''s age?: "; cin >> Age; cout << "How much start Energy?: "; cin >> StartEnergy; cout << "What is the character type?: "; cin >> CharacterType; /* Show Information */ cout << "Your character is " << Age << " years old." << endl; cout << "Has " << StartEnergy << " of starting energy." << endl; cout << "And its type is " << CharacterType << "." << endl; return 0; } This is my code. It doesn''t accept Age as a string, say the user inputs 32, it takes 3 as age and 2 as setenergy. I''ve tried using setw(3) and arrays but nothing works. Is my compiler/computer crapping out on me?

Share this post


Link to post
Share on other sites
That''s because you have age declared as a char, so when you read in 32, it reads the first character, which is 3. If you want to read in strings, you need to create a character array of some kind, or #include <string> to use strings, which really are just character arrays anyway.

Share this post


Link to post
Share on other sites
*sigh*

I know how to fix it, I''m just wondering if using std::string
would effect the performence or even the expected way the program should run. He has examples of unsigned char Age, if i put std::string, would that change the quality of the program?

Share this post


Link to post
Share on other sites
No it won''t, I would highly recommend using string. It''s apart of the STL it has been tested many many times.

The only thing it might do is add a little bit of size to your release build. If anything it will make your code safer.

Share this post


Link to post
Share on other sites
Plus doesn't string allocate the space for the char array dynamically? If so, you won't have to worry about setting char array sizes and wasting memory that isn't used.

Edit: i accidently had my finger touching down on return, resulting in a massive gap me so silly


*Scribble with excessive amounts of loops and curls*

[edited by - Lektrix on November 1, 2002 6:09:07 AM]

Share this post


Link to post
Share on other sites