Page 1 of 1

C++ std:getline.....

Posted: Fri Nov 18, 2011 10:15 pm
by Liidian
Why doesn't

string x;
std::getline(cin, x)

Do anything? It basically just jumps this line without doing anything at all. I don't understand why, is this outdated or something?

Posted: Sun Nov 20, 2011 7:16 pm
by AMindForeverVoyaging
This here works for me:

Code: Select all

#include <iostream>
using std::cin;
using std::string;

int main()
{
	string x;
	std::getline(cin, x);
	
	std::cout << "You entered: " << x << std::endl;
	
	return 0;
}

Posted: Mon Nov 21, 2011 2:38 pm
by Liidian
AMindForeverVoyaging wrote:This here works for me:

Code: Select all

#include <iostream>
using std::cin;
using std::string;

int main()
{
	string x;
	std::getline(cin, x);
	
	std::cout << "You entered: " << x << std::endl;
	
	return 0;
}
Basically im making a school project for the end term, and i figured ill make a program which does what you tell it to do. So i started exeprimenting with some raw code to see if i could get to the point i wanted. This for i got stuck in the main function since following code does not work for me!!

Code: Select all

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()

{
    std::string name;
    string b;
    int x;
    
    cout << "Welcome please enter the password code." << endl;
    cin >> x;
    
    
        if(x==7777){
        

    system("pause");
    
    cout << "What is your name?" << endl;
    cin >> b;
    
    
    cout << "Hello " << b << " what do you want to do today?" << endl;
    std::getline(cin, name);
    
    std::cout << name << endl;
    system("pause");
}
                    else{
                         cout << "Wrong Code" << endl << "press enter to close to application" << endl;
                         system("pause");
                         
}
return 0;
}
I know very well how badly this is written. I am only trying to get things to work so can write the actual program but id on't know why it jumps the fuction, and others usually spot you mistakes easier! :D
thanks for help.