Char/ Int problems.

Jan 24, 2019 at 1:30am
Hello. I'm an c++ beginner, and I want to make a little program, but i am suck here... i don't know how can I do with char, to input more that 1 character.
What I need to use, and how?
Thanks!

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
  #include "pch.h"
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>

using namespace std;

char enter;
char yes;
char no[2];

int main()
{
	cout << "Welcome to my program! \n";
	cout << "Do you want to open the program? yes/no \n";

	
	cin >> enter;

	if (enter == 'yes') {

		cout << "We're getting you in!";
	}
	else if (enter == 'no'){

		cout << "We're sorry about your decision. \n Closing the app!";

	}
	else {
		cout << "Error! Allowed answers is yes & no.";

	};

	return 0;

}
Jan 24, 2019 at 2:01am
closed account (E0p9LyTq)
std::cin.getline()

http://www.cplusplus.com/reference/istream/basic_istream/getline/
Jan 24, 2019 at 2:04am
closed account (E0p9LyTq)
Or use C++ strings and then std::cin will get more than one character, whole word, at a time.

std::getline and std::string you can get an entire line of text including what std::cin would normally see as delimiters.

http://www.cplusplus.com/reference/string/string/getline/?kw=getline
Jan 24, 2019 at 3:38am
(enter == 'yes') and (enter == 'no') should be (enter == "no") and (enter == "no")

Single quotes are used only for single characters.

As for your question, you can simple do
1
2
char enter[10];
cin >> enter;
Only problem with this is that if the user enters more than 9 characters then your program will write to indexes out of memory. Which is bad. So you should use std::cin.getline() like FurryGuy said. Note that cin.getline() also reads whitespaces.

Another thing, (enter == "yes") doesn't work for character arrays. You need to use strcmp http://www.cplusplus.com/reference/cstring/strcmp/

But like FurryGuy said if you know about the string datatype, you should use that instead. And you seemed to have included <string> already. == is supported by string so you don't need to use strcmp in that case.
Jan 24, 2019 at 11:15am
Grime, what do you mean with
(enter == 'yes') and (enter == 'no') should be (enter == "no") and (enter =="no") .

I should use " instead of '?
Jan 24, 2019 at 11:25am
Yes instead of. You should use that if you're using string the datatype otherwise you've got to use strcmp like I said.
Jan 24, 2019 at 2:05pm
if you put s on the end of it you can use == because this forces the string literal into a string object.

eg
string enter;
if(enter == "yes"s) //I may have messed up the syntax here, but if you need/want this you can look it up, my apologies as I don't do a lot of string processing.

Jan 24, 2019 at 2:23pm
By the way you declared enter as string so that would work even without specifying "yes" to be string

1
2
3
4
	char enter[4] = "yes";

	if (enter == "yes"s)
		cout << "Yep jonnin's right";


Ooo jonnin's right. Didn't know you could do that! But what exactly happens when you change "yes" to "yes"s? And why does the comparison work only when it's specified as string as in what's the difference in how it's read by the compiler?

Doesn't work on older compilers though it seems.
Jan 24, 2019 at 2:39pm
its just making a string instead of a char* for the literal behind the scenes. Its probably a mashup for efficiency behind the scenes, I don't know... but the user can treat it like a string.

I missed many generations of the language, I basically skipped from 99 to 2017 c++ versions... so I am no good at telling you when a feature was added. Im doing good just to know some of whats out there!

It works because the undelying type. String supports overload for ==, char* does not.
Last edited on Jan 24, 2019 at 2:40pm
Jan 24, 2019 at 2:47pm
So "yes"s is treated like an array (yes it's a string literal) as opposed to being a pointer? But why is "" a pointer in the first place?
Jan 24, 2019 at 7:23pm
no, "yes"s is treated like a string.
"yes" without the s is treated like a char*

"" is a pointer because strings were added to the language later. When I learned c++, string did not exist. There were borland strings, microsoft strings, your company strings, bob's string class, joe's string class.... everyone took a crack at it and they were all different and most of them were terrible. Microsoft to this day has a lot of clunk in its tools because of its approach to strings (known by the wonderful names like slznptr or something like that to tell what kind of string it was by the letter codes in the name)
Jan 25, 2019 at 3:11am
String the datatype :0? I don't get it. What happens if you don't include string?
Topic archived. No new replies allowed.