Thread: getline overload, solution didn't work

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    20

    getline overload, solution didn't work

    I had this problem last night. (http://www.cprogramming.com/cboard/s...threadid=12648).

    But what worked then, doesn't seem to work now: on the 2nd cin it skips the getline and propts for "make". Sorssen (I think) suggested input.ignore() to skip the '/n' in the buffer, but it's not working now.

    I'll attach the whole prog if that helps, thanks in advance

    Code:
    istream& operator>>(istream& input, cars& c)
    {
    	cout<<"Make ";
    	input.getline(c.make,10);
    	cout<<"model ";
    	input.getline(c.model,10);
    	cout<<"capacity ";
    	input>>c.cpcty;
    	cout<<"price ";
    	input>>c.price;
    	input.ignore();
    	return(input);
    };
    
    ostream& operator<<(ostream& oput, cars& c)
    
    //....
    
    main()
    {
    	cars car1;
    	cars car2;
    	cars car3;
    	cars car4;
    
    	cin>>car1;
    	cin>>car2;
    	cin>>car3;
    	cin>>car4;
    	cout<<car1;
    	cout<<car2;
    	cout<<car3;
    	cout<<car4;
    	return (0);
    }

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Worked fine here. Shouldn't it prompt for "make" first as that's the first element? What are your inputs?

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    Thanks for the help!

    When you say it works fine, how far did you get. There are four inputs, char, char, int, double? It's the second cars record that messes up?

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Entered four complete records and got four complete records as output. From stdout -

    Code:
    Make 1
    model 1
    capacity 1
    price 1
    Make 2
    model 2
    capacity 2
    price 2
    Make 3
    model 3
    capacity 3
    price 3
    Make 4
    model 4
    capacity 4
    price 4
    1       1       1       1
    2       2       2       2
    3       3       3       3
    4       4       4       4

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    I ran the prog again, just to see. It work fine. I did 9 more times and it failed once more. 8 out of 10. Are compliers funny like this, or am I seeing things. I'm useing bcc55.

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Are compliers funny like this

    No, it's normally the programmers. You said in your previous post that the inputs were char, char, int, double. Your struct contains char, char, int, int. The problem may be here. What inputs are causing it to fail?

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    20
    Well, it ran through car1 fine. At car2 it promts for both make and model on the same line (it did this just twice out of nearly 20 now), then go through the rest of the cars vars (car3, car4) the same as car two. I'm chalking it up to the smurf in my computer now, because I can't get it to happen again. Thanks for you help Sorensen (your fist name isn't Jessica, is it?).

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >your fist name isn't Jessica, is it?

    No. My second name isn't Sorensen either.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why wont getline() work?
    By lilhawk2892 in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2009, 09:55 PM
  2. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  3. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 03:03 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. nu b q: overload and getline
    By Clane in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2002, 05:53 PM