Thread: A Newbie's question in C++

  1. #1
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Question A Newbie's question in C++

    Hi,
    Well this is very simple program. But it isnt working. Giving me three errors when compiled using VC++. However i compiled the same program with borland C++ and i didnt get any errors.

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    	string userName;
    
    	cout << "What is your Name? ";
    
    	cin >> userName;
    
    	cout << "Hello " << userName << "!" << endl;
    
    	return 0;
    }
    The errors are as follows

    --------------------Configuration: hello3 - Win32 Debug--------------------
    Compiling...
    hello.cpp
    C:\Workspace\Practise\hello3\hello.cpp(6) : error C2065: 'string' : undeclared identifier
    C:\Workspace\Practise\hello3\hello.cpp(6) : error C2146: syntax error : missing ';' before identifier 'userName'
    C:\Workspace\Practise\hello3\hello.cpp(6) : error C2065: 'userName' : undeclared identifier
    Error executing cl.exe.

    hello3.exe - 3 error(s), 0 warning(s)

    Please help

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	string userName;
    
    	cout << "What is your Name? ";
    
    	cin >> userName;
    
    	cout << "Hello " << userName << "!" << endl;
    
    	return 0;
    }
    Use C++ style header's when coding C++.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Try to avoid the old <*.h> headers and opt for the the namespace safer headrers...like so.

    Code:
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    #include <string>
    using std::string;
    
    int main()
    {
    string userName;
    
    cout << "What is your Name? ";
    
    cin >> userName;
    
    cout << "Hello " << userName << "!" << endl;
    
    return 0;
    }

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Doh......

    Sorry Adrian.......you beat me this time

  5. #5
    Registered User Silverdream's Avatar
    Join Date
    Feb 2002
    Posts
    53

    Talking

    Thanx

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> Sorry Adrian.......you beat me this time

    Ah yes, but our solutions did show both ways of resolving the namespace!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    106

    The better way

    Ruflano's way is always the better way.


    #include <iostream.h>
    #include <conio.h>
    #include <conio.c>

    int age(0);
    char username[30];

    int main()
    {
    textcolor( 1 ); //love to show off
    cout << "Type in your username punk: " << endl;
    cin >> username;
    cout << "\nHi " << username << ". How old are you?" << endl;
    cin >> age;

    cout << "\nSo, you're " << age << " years old and your name is " << username << ". Thats sad" << endl;
    getch();
    return 0;
    }

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> Ruflano's way is always the better way.

    Hmmm.

    >>> #include <iostream.h>

    Ah, one of the problems he was having...

    >>> #include <conio.c>

    Including a .c file, hmmm.

    >>> textcolor( 1 ); //love to show off

    Non standard, won't work with VC++, (i.e. what he was asking about).

    >>>
    cout << "\nSo, you're " << age << " years old and your name is " << username << ". Thats sad" << endl;
    getch();
    <<<

    Mixing traditional C I/O with C++ stream I/O. An endless source of difficult to debug problems.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question. 4 Newbies 4all time
    By lifeisendless4m in forum C Programming
    Replies: 14
    Last Post: 10-07-2004, 12:19 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM