Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Char?
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Flikerator




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Mon Oct 31, 2005 10:37 am   Post subject: (No 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;
}
md




PostPosted: Mon Oct 31, 2005 4:52 pm   Post subject: (No 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).
Flikerator




PostPosted: Wed Nov 02, 2005 8:13 am   Post subject: (No subject)

Alright thanks Razz
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: