Thread: cout<<"Hello "<<name;

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    58

    Question cout<<"Hello "<<name;

    for some odd reason Borland 5.0 compiler dosnt like the following code.

    #include <iostream.h>

    int main()
    {
    char name[25];
    cout<<"Please type in your name\n";
    cin<<name;
    cout<<"Your name is: "<<name;

    return 0;
    }


    i get no errors but when i run it winblow$ comes up with a critical error any clues what is happening???

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    cin<<name; should be cin>>name;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    58
    woops my bad thats just a typo when i wrote it out. In the real program its fixed
    --== www.NuclearWasteSite.com==--

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    try adding a null character to the end of the string.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    try this
    Code:
    #include <iostream.h> 
    
    int main() 
    { 
    char name[25]={0}; 
    cout<<"Please type in your name\n"; 
    cin>>name; 
    cout<<"Your name is: "<<name<<flush; 
    
    return 0; 
    }
    if that doesn't work then try this...
    Code:
    #include <iostream.h> 
    
    int main() 
    { 
    char name[25]={0}; 
    cout<<"Please type in your name\n"; 
    cin.getline(name,sizeof(name)); 
    cout<<"Your name is: "<<name<<flush; 
    
    return 0; 
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    2
    Dont blame the OS for errors in your simple code.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    79
    Or, do it the c++ way:

    #include <iostream>
    #include <string>
    using namespace std;

    int main(void)
    {
    string name;
    cout <<"Enter you name:"<<endl;
    cin >> name;
    cout <<"Your name is "<<name;
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    305

    Talking

    Right on, Hannwaas!!! C++ way is a LOT easier.

Popular pages Recent additions subscribe to a feed