std::cin not working

The following is part of my code which is a solution that i am trying to come up with for one of my book exercises...When i try to compiling this it skips both the request to get input for fram and to output fram.It just skips the 2 requests and goes on to print "joe" .Can anyone tell me what is happening?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <vector>
#include <string>
#include "Framer.h"

int main()
{
    std::vector<std::string> sentences;
    std::string s;
    std::cout<<"The following program will take an input of strings and frame \n"
                "it accroding to the users desires "<<std::endl;
    std::cout<<"Please enter your set of sentences :"<<std::endl;
    while(getline(std::cin, s)){
        sentences.push_back(s);
    }
    std::cout<<"Please choose your method of framing. Enter center, left, or right"<<std::endl;
    std::string fram;
    std::cin>>fram;
    std::cout<<fram;
    std::cout<<"Joe"<<std::endl;
}


EDIT:Thr same thing also happens with getline....
Last edited on
How do you leave your while loop?
ctrl+z which is displayed as ^Z
With ctrl+z (windows), the standard input stream goes into an eof (end of file) state; it won't read anything more (unless it comes out of that state).

Do something like this instead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::vector<std::string> sentences;
    std::string s;
    std::cout << "The following program will take an input of strings and frame \n"
                 "it according to the users desires \n"
                 "Please enter your set of sentences\n"
                 "enter an empty string (just hit enter) to end the input:\n" ;

    while( std::getline(std::cin, s) && !s.empty() ){
        sentences.push_back(s);
    }

    std::cout << "Please choose your method of framing. Enter center, left, or right\n" ;
    std::string fram;
    std::cin >> fram;
    std::cout << fram;
}
Thank you very much! Is there any way to still use the input stream by resetting it after having used ctrl +z ? (Just asking out of curiosity)
Try std::cin.clear() ; http://en.cppreference.com/w/cpp/io/basic_ios/clear
Recovering from eof on stdin may not work on all implementations.
Topic archived. No new replies allowed.