reading a string from keyboard

This is a discussion on reading a string from keyboard within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a little problem. I'm trying to read a character string from the keyboard. So I have put: ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    Smile reading a string from keyboard

    Hi,
    I have a little problem. I'm trying to read a character string from the keyboard. So I have put:
    Code:
     
    #include <iostream> 
    #include <string.h>
    using namespace std; 
    
    void main(int argc, char **argv)
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    
            string name;
           getline(nameOfCourse, 50);
    }
    but when I compile it says: error C3861: 'getline': identifier not found

    can anyone explain why this is and do i have any alternatives please.
    Thank you :O)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Which getline are you trying to use? If you're trying to use a member function, then it needs to be written as a member function of whatever it's a member of (presumably stdin.getline).

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Thank you for your message.
    I just wrote your suggestion stdin.getline instead of getline but got this message:
    error C2228: left of '.getline' must have class/struct/union

    then I tried
    using std::cin;
    char *name = "";
    printf("Please enter the name:\n");
    cin.getline

    and then when compiling, it printed the sentence "Please enter ..." but when I tried to input something the program crashed

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Couple of problems.

    1)It should be int main not void main
    2)You should be including <string> not <string.h>

    Also look at: http://www.cplusplus.com/reference/string/getline.html
    Woop?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Thank you.
    I just did that, changed <string.h> to <string> and also changed void main to int main.
    Now it lets me input a name but when I press return it craches :O)

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Post the latest code.
    Woop?

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    sorry it has some glut and openGL stuff too :O)
    Code:
    #include <windows.h>  
    #include <gl\glut.h>
    #include <gl\gl.h>
    
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <windows.h>
    #include <conio.h>
    #include <math.h>
    #include <time.h>
    #include <float.h>
    #include <assert.h>
    #include <iostream> 
    #include <string>
    
    
    using std::cin;
    using namespace std;  //// needs #include <iostream>  
    
    
    
    class GradeBook
    {
     public:
    
    	void displayMessage(string courseName)
    	{
    		printf("Welcome to the grade book\n", courseName);
    	} 
    };
    
    void RenderScene(void)
    {
    	// Clear the window with current clearing color
    	glClear(GL_COLOR_BUFFER_BIT);
    
    	glutSwapBuffers();
    }
    
    void SetupRC(void)
    {
    	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }
    
    int main(int argc, char **argv)
    {
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    
    	GradeBook myGradeBook; 
    
    	char *nameOfCourse = "";  // = "hello class"; 
    
    	printf("Please enter the name:\n");
    
    	cin.getline(nameOfCourse, 50);
    
    	myGradeBook.displayMessage( nameOfCourse );
    
    	
    	Sleep(20);   //needs #include <windows.h> 
    
    	glutCreateWindow("Simple");
    	glutDisplayFunc(RenderScene);
    	SetupRC();
    	glutMainLoop();
    
    }

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Code:
    	char *nameOfCourse = "";  // = "hello class"; 
    
    	printf("Please enter the name:\n");
    
    	cin.getline(nameOfCourse, 50);
    Here lies the problem.

    You are trying to read in to a char * that hasn't been fully allocated.

    Preferably you would want to use an std::string and use that getline function I sent you
    Code:
    std::string nameOfCourse;
    getline(cin, nameOfCourse);
    Woop?

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Thank you very very much. I added std::string but it crashed again however I allocated char array and it worked.
    If I may ask you another question: Why is the memory read only with char *nameOfCourse = "";? and why didn't it work with std::string, I thought just putting using namespace std meant that all of the std were loaded.

    Thanks again

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Sorry the std:: stuff is a habit of mine.

    As far as the char * being readonly, when you assign a char * with a string literal that makes the memory there read only.

    The string shouldn't be crashing. There may be a problem somewhere else in the code if you are getting a crash.
    Woop?

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Thanks ever so much for your time :O)

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    Anytime!
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. sending a string as keyboard keystrokes wrongly
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 06-29-2007, 07:49 AM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21