How can I get input as an empty string? - c++

#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
getline(cin, s);
cout << s << endl;
return 0;
}
I think it will help someone with my question and answer.

In the code you showed, I think pressing only the Enter key will return an empty string.

Related

Is it possible to put multiple names in one string?

I just started learning C++ and I'm currently following a tutorial on YouTube.
I thought it was fun to make a very simple 'access' program. If I type in my name it says, "Welcome!" If I type in another name it says, "access denied". It worked perfectly fine, but then I wanted the program to say "Welcome!" to two different names. So, I wanted to add a second name in the string, but I couldn't figure out how to do that. I googled a lot but I couldn't find anything. In the end, I came to string name = ("Joe", "Sean");, but here, it was only valid for Sean. I just can't figure out how to put multiple names in one string and make them both work. I hope you can help me, here is my code:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string name = ("Joe", "Sean");
string input;
cout << "What is your name?\nMy name is: ";
cin >> input;
if(input == name){
cout << "Welcome, "<< input <<"! ";
} else {
cout << "Access denied";
}
return 0;
}
This is a way to do it using a vector of strings, so you can adapt easily with more names :
#include <iostream>
#include <vector>
using namespace std;
void printMessage(string message)
{
std::cout << message << std::endl;
}
int main()
{
vector<string> names{"Joe", "Sean", "Paul"};
string input;
cout << "What is your name? " << endl;
cin >> input;
for (string name : names)
{
if (name == input)
{
printMessage("Welcome!");
return 0;
}
}
printMessage("Access Denied!");
return 0;
}
The problem is in the string variable "name". You need an array of strings, not a single string.
This is an example implementation:
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string names[] = {"Joe", "Sean"};
string input;
cout << "What is your name?\nMy name is: ";
cin >> input;
for (int i = 0; i < end(names) - begin(names); i++) {
if(input == names[i]){
cout << "Welcome, "<< input <<"! " << endl;
return 0;
}
}
cout << "Access denied" << endl;
return 0;
}
You encountered some quirky features of C++ in the approach you are using to initialize your string variable:
string s1 = ("Joe"); // creates a string "Joe"
string s2 = ("Joe", "Sean"); // creates 2 strings, "Joe" and "Sean", and the variable s2 stores only the latter!
For more details on the different methods for initializing variables there has been an interesting discussion in this previous question.

Convert char array to a string with cin.getline(.)

hi guys so my question is how to convert a char array to a string. here is my code:
#include<iostream>
using namespace std;
int main()
{
while (true) {
char lol[128];
cout << "you say >> ";
cin.getline(lol,256);
cout << lol << endl;;
}
return 0;
}
so I want to convert lol to a string variable like "stringedChar" (if thats even english lol)
so I can do stuff like:
string badwords[2] = {"frick","stupid"};
for (int counter = 0; counter < 2;counter++) {
if(strigedChar == badwords[counter]) {
bool isKicked = true;
cout << "Inappropriate message!\n";
}
}
Sorry im just a c++ begginer lol
Do something like this :
as char lol[128];
into string like: std::string str(lol);
Line : cin.getline(lol,256); <--> should be changed to cin.getline(lol,128)
Just invoke std::getline() on a std::string object instead of messing about with a char array, and use std::set<std::string> for badwords as testing set membership is trivial:
#include <iostream>
#include <set>
#include <string>
static std::set<std::string> badwords{
"frick",
"stupid"
};
int main() {
std::string line;
while (std::getline(std::cin, line)) {
if (badwords.count(line) != 0) {
std::cout << "Inappropriate message!\n";
}
}
return 0;
}
Note that this tests whether the entire line is equal to any element of the set, not that the line contains any element of the set, but your code appears to be attempting to do the former anyway.
First off, you have a mistake in your code. You are allocating an array of 128 chars, but you are telling cin.getline() that you allocated 256 chars. So you have a buffer overflow waiting to happen.
That said, std::string has constructors that accept char[] data as input, eg:
#include <iostream>
using namespace std;
int main()
{
while (true) {
char lol[128];
cout << "you say >> ";
cin.getline(lol, 128);
string s(lol, cin.gcount());
cout << s << endl;;
}
return 0;
}
However, you really should use std::getline() instead, which populates a std::string instead of a char[]:
#include <iostream>
#include <string>
using namespace std;
int main()
{
while (true) {
string lol;
cout << "you say >> ";
getline(cin, lol);
cout << lol << endl;;
}
return 0;
}

C++ :: Function to return user input until enter key is pressed

This is definitely NOT the current syntax but just so you'd get the idea :)
#include <stdio.h>
void getWord()
{
while((c=getchar())!='\n')
{
myString.=c;
}
return myString;
}
int main(void)
{
var c=getWord();
print_f("\nCLast Word:",c);
return 0;
}
Bare in mind that I do not what to break the current line and I'm expecting to get the user input and stay on the same line even after enter key is being pressed.
Use std::getline. The function reads one line from an input stream and save it into a string.
#include <iostream>
#include <string>
int main() {
std::string input;
std::getline(std::cin, input); // get input until enter key is pressed
std::cout << input << std::endl; // print the string
return 0;
}
or you can omit std:: by "using namespace std;"
#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
getline(cin, input); // get input until enter key is pressed
cout << input << endl; // print the string
return 0;
}

Why my code only outputs part of the input string?

thank you for reading. I am a new programmer in an introductory programming class, and I only have a month's worth of training in C++. I have tried to fix this code with many approaches, but I don't know why it only prints one word of my input string:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void printThetxt(string);
string inputText(string);
int main() {
string x;
printThetxt(inputText(x));
system("pause");
return 0;
}
void printThetxt(string y) {
cout << y << endl;
}
string inputText(string x) {
cout << "Type in your string: " << endl;
cin >> x;
return x;
}
Please tell me why this code only prints one piece of the input string? Thank you!!
Replace:
cin >> x;
with:
std::getline(std::cin, x);
because formatted input stops at whitespace.

Cout of a string is giving an error and a hard time some insight help pls?

I cant find the error in this piece of code could anyone please insight me? I ran the debugging but the errors are un-understandable..
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
string name;
cout << "Input your name please?" << endl;
cin >> name;
if
{
(name == "Bart Simpson")
cout << "You have been very naughty" << endl;
}
return 0;
}
Problems:
You have some missing #includes, which probably caused your initial compiler errors.
You have a simple syntax error with your if statement.
Using the stream extraction operator will never yield a string with whitespace inside of it.
The following should work as you expect:
#include "stdafx.h"
#include <iostream>
#include <ostream>
#include <string>
using namespace std;
int main()
{
cout << "Input your name please?" << endl;
string name;
getline(cin, name);
if (name == "Bart Simpson")
{
cout << "You have been very naughty" << endl;
}
return 0;
}
(You need to include string for std::string and std::getline, and ostream for std::endl.)
I assume the bracket in the wrong place is just a problem when pasting the code
if(name == "Bart Simpson")
name will never equal "Bart Simpson", since extracting a string stops when it encounters whitespace; so it would only be "Bart". Perhaps you want to use getline() instead?
Should be
if (name == "Bart Simpson")
{
cout << "You have been very naughty" << endl;
}
And you need to include <string>