Computer Science Canada

Char?

Author:  Flikerator [ Mon Oct 31, 2005 9:26 am ]
Post subject:  Char?

code:
#include <iostream>
int main()
{
    char name;
    std::cout << "Hello, whats your name? ";
    std::cin >> name;
    std::cin.ignore();         //Ignores the enter key
    std::cout << name << " is a cool name!";
    std::cin.get();
    return 0;
}


I have stdd::cin.get(); so I can see the bottem line. If you enter a single character then its fine, but more then that it keeps going. Im not exactly sure why :/

If the variable is changed to an int, then it works find with any length. So im guessing i need to change something with ignore for strings.

I use Bloodshed Dec C++

Note: Dont tell me not to use it, its all I have at school and I find it works pretty well.

Author:  rizzix [ Mon Oct 31, 2005 10:37 am ]
Post subject: 

maybe you meant this:
c++:
#include <string>
#include <iostream>

int main()
{
    std::string name;
    std::cout << "Hello, whats your name? ";
    std::cin >> name;
    std::cout << name << " is a cool name!" << std::endl;
    return 0;
}

Author:  md [ Mon Oct 31, 2005 4:52 pm ]
Post subject: 

rizzix's answer gives the right code; the reason is that you used a char. char = character, std::string is a string (there are other ways of doing strings too, but you probably want to avoid them).

Author:  Flikerator [ Wed Nov 02, 2005 8:13 am ]
Post subject: 

Alright thanks Razz


: