getline doesn't wait for my <enter> key

HI Everyone !
I'm trying to write a little program which waits for some minutes and then runs a command in terminal (Ubuntu 13) .
But when I enter the tasm variable , it starts counting down , even though the setting up time part is after getline .
For example enter 0.1 for tasm and after that it waits for 6 seconds and then terminates the program whithout you even typing anything or pressing any key .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <SFML/System.hpp>

using std :: cout ;
using std :: cin ;
using std:: string ;
using std :: getline ;

int main()
{
    //Minutes to be elapsed before the command runs
    float tasm ;
    cout << "Enter minutes : " ;
    cin >> tasm ;

    //Command to run
    string cmd ;
    cout << "Enter your command :\n\t" ;
    getline (cin , cmd) ;

    /*
        Here's where the problem occurs
        When I wait for almost "t*60" seconds it goes for the rest
        Without me even entering the 'cmd'
    */

    //Setting up time using SFML
    sf::Time t ;
    t = sf::seconds(tasm * 60) ;
    sf::Clock c ;

    //Making the time pass in a very inefficient way
    while (c.getElapsedTime().asSeconds() < t.asSeconds())
    {
    }

    //Running the command
    system (cmd.c_str ()) ;

    return 0 ;
}
Change line 20 to
getline (cin >> ws, cmd) ;.

The reason it "skips" the getline is because the cin >> tasm; line leaves a newline in the input buffer, and getline sees that and eats it instead of waiting for more input.
So cin >> ws gets rid of leading whitespace first so that getline won't think that it's already reached the end of the line.
Thanks !
Interesting !@
Topic archived. No new replies allowed.