#include "stdafx.h"
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string a;
string b = "hey";
cin >> a;
if (a == b) {
cout << "hello \n";
}
int z = 40;
string x = "";
string y = "weather";
cin >> x;
if (x == y) {
cout << "the temp is " << z << endl;
}
return 0;
}
it works correctly but once I change string y to a sentence that have spaces in it it does not work for example :
string y = "this is a sentence";
Use getline() function or you can also use gets() function.
If you are using STL string then,
getline(cin,str); /*it will work */
If you are not using STL string then,
#include <cstdio>
char *gets( char *str );
Related
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;
}
The final element in the vector is the char to search for.
Here’s my code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
vector<string> words;
string in;
while(cin>>in)
{
words.push_back(in);
}
int size = words.size()
string check = words.at(size-1);
}
I tried your code and the loop was infinite. Try asking a set number of words then use the find method for each string. Here's an example.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> words;
string in;
cout << "Enter 5 words: \n";
for (int i{0}; i < 5; ++i)
{
cout << "Word: ";
cin >> in;
words.push_back(in);
}
cout << "What character do you want to search for? ";
char c;
cin >> c;
for (auto word : words)
{
if (word.find(c) != string::npos)
{
cout << "Character \"" << c << "\" found in " << word << endl;
}
}
return 0;
}
Edit: I noticed that you tried to use int to store the size for the string. Use size_t instead. I also think you meant to use the length method of the string, which also returns size_t.
I'm trying to convert a string of characters into their ASCII int values. However I cannot get this to work for one even one character in the string. I would expect a result of 72 when entering 'H', but it returns a 0 (the same for every character I've tried).
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
const char * b = a.c_str();
int c = atoi(b);
cout << int(c) << endl;
}
Thanks in advance.
atoi parses the C-string interpreting its content as an integral number, i.e.
int i = atoi("123"); // i = 123
You don't want this: you want to know the ASCII value of every single character of the input string. To figure this out, you can use this code snippet:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
for(int c: a)
cout << c << '\n';
}
I'm not quite used to the string library but simply type:
cout<<(int)a[pozition];
You can place that in a for like this.
for(int i=0;i<a.length();i++)
cout<<(int)a[i]<<endl;
You can just cast each character to an int
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a;
cin >> a;
for (int i = 0; i < a.length(); i++) {
cout << (int) a[i] << endl;
}
}
How to apply find_first_of function for char (char array). I know it can be done for a string but I want to know how to do it when I can declare only variable of type char. It doesn't work:
#include <iostream>
#include <string>
#include <Windows.h>
#include <complex>
using namespace std;
int main()
{
char str[10];
cin >> str;
if(str[10].find_first_of('z')!=string::npos)
{
cout << "nazwa: " << str[10] << endl;
}
return (0);
}
Error: "'find_first_of'must have class\struct\union";
Compiler stressed word "str" in if(**str**[10]...) -> expression must have class type.
find_first_of is a method on string. You can only use it on an object of that type:
std::string str
cin >> str;
if (str.find_first_of('z') != std::string::npos) {
// ....
}
A char array, while serving the same purpose, is not a string, so neither your cin nor your function work. To use an array instead, you'd have to do:
char str[10];
for (int i = 0; i < 10; ++i) {
cin >> str[i];
}
if (std::find(str, str + 10, 'z') != (str + 10)) {
// ....
}
#include <iostream>
#include <sstream>
using namespace std;
int get_4()
{
char c = '4';
stringstream s(ios::in);
s << c;
int i;
s >> i;
return i;
}
int main()
{
cout << get_4() << endl;
}
The conversion is not working for me. If I write a character '4' or character array {'4','\0'} to stringstream and then read it out to int i, I don't get back the 4. What is wrong with the above code?
Because you set the stringstream to input-only -- no output.
If you check the fail() bit after trying to extract the int, you'll see it didn't work:
s >> i;
bool b = s.fail();
if( b )
cerr << "WHOA DOGGIE! WE BLOWED UP\n";
In your code, change:
stringstream s(ios::in);
to:
stringstream s;