problem of input in member function of class

This is a discussion on problem of input in member function of class within the C++ Programming forums, part of the General Programming Boards category; There is no syntax error here: Code: #include <iostream> #include <stdlib.h> #include <string> using namespace std; class A { public: ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    problem of input in member function of class

    There is no syntax error here:
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <string>
    using namespace std;
    class A
    {
    public:
    void get_input();
    }
    void main()
    {
         A obj1,obj2;
         cout<<"Input 1:";
         obj1.get_input();
         cout<<"Input 2:";
         obj2.get_input();
    }
    void A::get_input()
    {
            char Array[10];
    	cin.getline(Array,10);
    }
    Sometimes I get this and it is exactly what it is supposed to be :
    Code:
    Input 1://get input 1 first
    Input 2://then get input 2
    but I get this sometimes:
    Code:
    Input 1:Input 2:
    What's wrong with it?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There is a syntax error there. You need to terminate the definition of A with a semicolon:

    Code:
    class A
    {
    ...
    };
    But I assume that you just typed this example into the post without actually checking it.

    When you get the "weird" output, you probably have characters left in the input buffer. Is there any input before you call get_input()? If so, it may have left newlines in the stream.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Oops, I often forget to add ; after }.

    Yes, there is input before calling get_input()
    I added cin.ignore();after each get_input(). I still get the unwanted output. How to avoid this?

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    if there is other input ( like cin >> x; ) before calling get_input() then you should add the ignore before calling get_input().
    Kurt

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thx!

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> There is no syntax error here:
    Technically, void main should be an error on a standards-compliant compiler (it should be int main). Also, you #include <string> but do not use the C++ string class in your code. Switching from C style strings (character arrays) to the C++ string class is a good diea. If you cannot or do not want to switch, and you are using C style string manipulation functions like strcpy or strlen, then you should #include <cstring> or <string.h>, not <string>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More explicit template member function hell
    By SevenThunders in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2009, 10:36 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Replies: 6
    Last Post: 04-27-2006, 10:55 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM

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