I don't get std::cin.getLine(); :(

Sorry, I know I'm dumb but I tried and I don't see how it works :( I Found the page about it here and I still don't get it :(

This is my Beeper source:
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
// Beeper.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <windows.h>


int _tmain(int argc, _TCHAR* argv[])
{
	unsigned int intFre, intDur;
	char chrAns;

	system("COLOR 02");
	system("TITLE Beeper by CometJack");
do
{
	std::cin.clear();
	system("CLS");
	std::cout << "Welcome to the Beeper application by CometJack!" << std::endl;
	std::cout << "Please type in how high you want the beep to sound." << std::endl;
	std::cin >> intFre;
	system("CLS");
	std::cout << "Please type in how long you want to hear the beep sound." << std::endl;
	std::cin >> intDur;
	system("CLS");
	std::cout << "Beeping freequency set at " << intFre << " milliseconds" << " & " << "beeping duration set at " << intDur <<" milliseconds." << std::endl;
	Beep(intFre, intDur);
	system("CLS");
	std::cout << "Enter Y to restart or press another key to exit." << std::endl;
	std::cin >> chrAns;
} while (chrAns == 'Y' || chrAns == 'y');
	return 0;
}


I am trying to replace std::cin >> with std::cin.getLine(); although I don't know how to get it working :( I want the program to be able to tell if it's the correct data type going into a variable so it doesn't crash when someone types the wrong data type. Help please :(

Also there is no string in my compiler, I try
string strInput;

and get errors saying there is no string class or something :( I am using MS Studio Express Edition 2008.
Last edited on
closed account (jwC5fSEw)
It's cin.getline(), not cin.getLine().

Also, are you #include ing <string>?
Okay thanks, also yes I have
#include <string>
near the top and I tried
string strTest;
and I got
1>c:\users\cometjack\documents\visual studio 2008\projects\beeper\beeper\beeper.cpp(14) : error C2065: 'string' : undeclared identifier
It's std::string.
Ah! Thanks! Now it works :P

Now, does anyone have any helpful comments about using std::cin.getline();?
Use std::getline() instead, the cin.getline() version wants a char*, not an std::string.
Now, does anyone have any helpful comments about using std::cin.getline();?


Ya. Don't use it. cin.getline() is for char arrays. getline() is for strings.

Don't use http://cplusplus.com/reference/iostream/istream/getline/

Use this instead http://cplusplus.com/reference/string/getline/
Oh :( Sorry, like I said I'm new, I don't know what's what lol

Well I'm working on a time converter, anyways how could I use std::getline in replace of std::cin >>?

Here is my source:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// TimeConverter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
	system("COLOR 02");
	system("TITLE Time Converter by CometJack");

	char chrOpt;
	int intVal1, intVal2;

	std::cout << "Welcome to the Time Converter, what would you like to convert?" << std::endl;
	std::cout << "1) Minutes -> Hours" << std::endl << "2) Seconds -> Hours" << std::endl << "3) Milliseconds -> Hours" <<
		std::endl << "4) Seconds -> Minutes" << std::endl << "5) Milliseconds -> Minutes" << std::endl << "6) Months -> Years" <<
		std::endl << "7) Weeks -> Years" << std::endl << "8) Days -> Years" << std::endl << "9) Minutes -> Years" <<
		std::endl << "10) Hours -> Years";
	std::getline(chrOpt, "");
	return 0;
}


I want it to be able to tell if it's a number between 1-10, but I have no clue how :(

EDIT: It's going great! Here's the updated source:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// TimeConverter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
	system("COLOR 02");
	system("TITLE Time Converter by CometJack");

	char chrRep;
	int intVal, chrOpt;

	do
	{
	system("CLS");
	std::cin.clear();
	std::cout << "Welcome to the Time Converter, what would you like to convert?" << std::endl;
	std::cout << "1) Minutes -> Hours" << std::endl << "2) Seconds -> Hours" << std::endl << "3) Milliseconds -> Hours" <<
		std::endl << "4) Seconds -> Minutes" << std::endl << "5) Milliseconds -> Minutes" << std::endl << "6) Months -> Years" <<
		std::endl << "7) Weeks -> Years" << std::endl << "8) Days -> Years" << std::endl << "9) Minutes -> Years" <<
		std::endl << "10) Hours -> Years" << std::endl;
	std::cin >> chrOpt;

	switch (chrOpt)
	{
	case 1:
		system("CLS");
		std::cout << "How many hours?" << std::endl;
		std::cin >> intVal;
		system("CLS");
		if (intVal !=0 && intVal !=1)
		{
			std::cout << "There are " << (60*intVal) << " minutes in " << intVal << " hours." << std::endl; 
			break;
		}
		else if (intVal == 1)
		{
			std::cout << "There are 60 minutes in 1 hour." << std::endl;
			break;
		}
		else
		{
			std::cout << "There are 0 minutes in 0 hours." << std::endl;
			break;
		}
	case 2:
		std::cout << "There are " << ((60*60)*60) << " seconds in an hour." << std::endl;
		break;
	case 3:
		std::cout << "There are ";
		break;
	}

	std::cout << "Press Y to restart the program." << std::endl;
	std::cin >> chrRep;

	} while (chrRep == 'Y' || chrRep == 'y');
	return 0;
}


Last edited on
Topic archived. No new replies allowed.