Problem with input output functions

Ok. Hello people... please FORGIVE me if this exact same question is somewhere in these forums. But here we go..
I was trying to write a program to expand my knowledge and that would help me out a little in school. It basically is supposed to display my school schedule for whatever day it is and let me record homework in it.

At first I was having trouble in that every time I'd get to the part where I input my homework it would skip the first word of it during the output (to text document). I figured that that was probably due to me mixing cin<< and getline(); so I started using the cin.get() function instead. Now my problem is everytime cin.get precedes getline it skips over my getline input all together! I do not understand what's going on.. can you not use cin.get and getline together either??

And here is a copy of the program (I got frustrated and scrapped the original so i could play with cin.get and getline inputs so i could understand them before putting them in the program.):

#include <iostream>
#include <string>
#include <fstream>

using namespace std;
void new_line();

int main()
{

string homework;

char subject;


cin.get(subject);

getline(cin,homework,'\n');
cout<<"Your homework assignment is "<<homework<<". Is this right?"<<endl;


system ("Pause");

very simple...
but what am I doing wrong?? Please help!!

If I might ask... what's the output of that program for an input
CPeriodic Table
?

-Albatross
Well in the original program it was structured with a switch statement, so I used character like a, b, c. etc, for the subject of choice options. So the input would be a, b, c, or whatever char type variable is specified. The homework variable would be whatever string line i put in.. for example "Read chapter 4, Do study questions" or something like that.... does that answer your question?

my problem is when i test the program it completely skips over the getline input.
How do you know it skips over the getline input? Indeed it doesn't pause and it wouldn't because there is at least one character in the stream when getline() is called (there are at least two, the letter and the newline, and cin.get() clears only one), at which point the rest of the characters are read and the newline is discarded. However, does it really skip over getline()? Does homework contain either nothing or garbage when cout read it to the console?

-Albatross
Last edited on
Well when I get to the part where I would input my homework it does not let me input anything. It skips straight to the last statement saying "Your homework assignment is [homework variable would be here]. Is this right?".. so you're saying that my cin.get statement is taking in two characters into the stream instead of one? and because of that when it gets to the getline function, getline is taking the input that is left in the stream (the newline)? If that's the case how can I correct this? I need my cin.get to only take in the one variable (subject) so my getline can take in my homework variable...
would a void function work?
Ok I got a chance to ask my teacher today, she suggested something like cin.ignore(1000); but that didnt' work either. It ended up prompting the user to keep tabbing after the input (I'm guessing for a hundred spaces) so I tried cin.ignore() and it works perfectly. Although I'm not entirely sure why it worked
Topic archived. No new replies allowed.