How to solve this issue? Invalid operands to binary expression - c++

I'm beginning with c++ and I don't understand how to solve this issue :
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int main(int argc, const char * argv[]) {
//std::cout << "Hello, World!\n";
string str ("Teststring");
cout << str.end() << endl;//HERE
Thanks

str.end() will give unexpected behaviour - it returns an iterator starting at the character after the end of the string.
Try replacing the last line with std::cout << str.c_str() << endl; and you'll be making progress. See http://www.cplusplus.com/reference/string/string/c_str/ for more. The pointer/iterator is actually unnecessary, so std::cout << str << endl; will work just as well.

Related

a "?" before the string

I want to use strings to input the path of files:
char** argv;
char* mytarget[2]={ (char*)"‪D:\\testlas\\BigOne.pcd",(char*)"‪‪D:\\testlas\\SmallOne.pcd" };
argv = mytarget;
for(int i=0;i<2;i++)
{
std::cout << "m.name: " << argv[i] <<std::endl;
}
However, cout outputs:
m.name: ?‪D:\\testlas\\BigOne.pcd
m.name: ?‪D:\\testlas\\SmallOne.pcd
Why is there a ? before the strings?
I use VS2017 C++11.
I created a new program and used the code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
std::string test = "‪abc789";
cout << test << endl;
return 0;
}
It also outputs "?abc789". Why?
std::string test = "‪abc789";
There is a hidden LEFT-TO-RIGHT EMBEDDING character between the opening quote " and the first letter a (Unicode character U+202A, or UTF-8 E2 80 AA). Remove it, for example by deleting and retyping the line, then the ? will go away.

how to read a const char* from keyboard input and perform strlen() on it?

So I have been trying for 1.30 hour to get this to work. I am new indeed, but I have searched all over the place and couldn't find an exact answer. I do not wish to do this another way, as it would take away the entire purpose of learning to code. I have to find why this thing isn't working. I tried dozens if not hunderds of syntaxes, but nothing works.
I want to read in a const char* name, than count the number of elements in it, so I thought had to be strlen(), and than output the name and the number of elements. If that works I can write the rest of the code.
#include <iostream>
using namespace std;
int main()
{
//writing your name, and counting the characters including \0
int a;
const char* name;
a = int strlen(name);
cin.getline(name);
cout << name;
cout >> a;
return 0;
}
There are a lot of problems with your code.
You are not allocating any memory for cin.getline() to read into. const char* name; is declaring an uninitialized pointer to nothing. You have to allocate memory for name before you can then read any data into it.
cin.getline() expects two input parameters (a pointer to an allocated buffer, and the max number of characters the buffer can hold), but you are only passing in one value.
You are calling strlen() before you have read anything into name (and there is a syntax error on your strlen() statement anyway).
You are passing a to std::cout using >>, but std::ostream does not implement the >> operator. You have to use << instead.
And lastly, don't use using namespace std;.
Try this instead:
#include <iostream>
#include <cstring>
int main()
{
//writing your name, and counting the characters including \0
int a;
char name[32];
std::cin.getline(name, 32);
a = std::strlen(name);
std::cout << "You entered: " << name << std::endl;
std::cout << "It is << a << " chars in length" << std::endl;
return 0;
}
Or, if you really don't like using std:: everywhere, at least use using <identifier>; instead of using namespace std;:
#include <iostream>
#include <cstring>
using std::cin;
using std::strlen;
using std::cout;
using std::endl;
int main()
{
//writing your name, and counting the characters including \0
int a;
char name[32];
cin.getline(name, 32);
a = strlen(name);
cout << "You entered: " << name << endl;
cout << "It is " << a << " chars in length" << endl;
return 0;
}
Now, that being said, the preferred solution is to use std::getline() instead of cin.getline():
#include <iostream>
#include <string>
int main()
{
int a;
std::string name;
std::getline(std::cin, name);
a = name.length();
std::cout << "You entered: " << name << std::endl;
std::cout << "It is " << a << " chars in length" << std::endl;
return 0;
}
I found a working solution, although I don't see where I had gone wrong. But this does exactly what I want using const char* and strlen() without using std::string.
Thanks for all your help, you have all pointed me to the correct direction.
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int main ()
{
const char *name;
int len;
name = "stephane";
len = strlen(name);
cout << name;
cout << len;
return(0);
}
As another user has pointed out, I think it's a good idea for you to take a few steps back and read the basics until you understand how pointers work.
A const char* is that: const. It could be used usually while doing things like this:
const char* cpName = "Stephane"; //expected not to change through the program's lifetime
char* pName = "Stephane"; //can be changed to point to something else
char *pOther = "Vada";
pName = pOther; //pName now points to the string "Vada"
cpName = pOther; //this won't compile as cpName is const

c++ regex pattern to check file extension

I want to check if a string ends with .h5 and tried the c++ regex class. But for any Input the regex_search function returns false. Other examples in the internet looks similar to my code below, so I do not understand whats going wrong.
What is wrong with my code? Thanks for any help.
#include <iostream>
#include <regex>
#include <string>
int main(int argc,char *argv[]){
std::string text = argv[1];
std::regex rx(".*\\.h5$");
bool found = std::regex_search(text.c_str(),rx);
std::cout << text << std::endl;
std::cout << "res: " << found << std::endl;
}
What about using just substr ?
#include <string>
int main (int argc, char* argv[]) {
std::string filename(argv[1]);
std::string last = filename.substr(filename.length() - 3);
return last == ".h5";
}
Replace regex_search with regex_match
std::regex rx(".*\\.h5$");
bool found = std::regex_match(argv[1], rx);
std::cout << "Result: " << std::boolalpha << found << std::endl;

std::regex not working as expected

I googled around but still cannot find the error.
Why does the following code print false, I expected true?
#include <iostream>
#include <regex>
using namespace std;
int main()
{
std::string in("15\n");
std::regex r("[1-9]+[0-9]*\\n",
std::regex_constants::extended);
std::cout << std::boolalpha;
std::cout << std::regex_match(in, r) << std::endl;
}
The option to use regex_search is not given.
There is an extra slash before the "\n" in your regex. The code prints true with just the slash removed.
#include <iostream>
#include <regex>
using namespace std;
int main()
{
std::string in("15\n");
std::regex r("[1-9]+[0-9]*\n",
std::regex_constants::extended);
std::cout << std::boolalpha;
std::cout << std::regex_match(in, r) << std::endl;
}
Edit: #rici explains why this is an issue in a comment:
Posix-standard extended regular expressions (selected with std::regex_constants::extended) do not recognize C-escape sequences such as \n. See Posix base definitions 9.4.2: "The interpretation of an ordinary character preceded by a ( '\' ) is undefined."

Unable to use cout with a C++ string unless I run it through data() or c_str()

On the following program, I'm getting this when I attempt to use cout to output a C++ string to stdout - the other instructions produce the expected output. I'm using MS Visual Studio 2010 on a Windows 7 system.
First-chance exception at 0x00dd4e89 in Lab1.exe: 0xC00000FD: Stack
overflow. Unhandled exception at 0x00dd4e89 in Lab1.exe: 0xC00000FD:
Stack overflow. The program '[3740] Lab1.exe: Native' has exited with
code -1073741571 (0xc00000fd).
#include "StdAfx.h"
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <ostream>
#include <string>
#include <ctime>
//more code here
int main() {
int number = 1;
string myStr = "Hello, string!";
cout << "number: " << number << endl;
cout << "Hello, World!" << endl;
cout << myStr << endl; //failing instruction
cout << "\nHit any key to continue...." << endl;
cin.get();
return 0;
}
My instructor suggested changing the failing instruction to use data() or c_str() like so:
cout << myStr.data() << endl;
I did this, and this resolved the problem. He didn't know why, just said it worked so not to worry about it.
It seems to me that a C++ ostream object like cout should be able to handle a C++ string. Am I missing something, or do I really need to use data() or c_str() with cout?
I also tried using std::cout, std::string, and std::endl - it didn't help.
Thanks in advance for your advice; I'm really wanting to understand what's going on here.
Helen
You should include string instead of string.h:
#include <string>
I doubt that cout << myStr << endl; was the troublesome line.
This code works fine:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string s("Hello World!");
cout << s << endl;
return 0;
}
The error message indicates that you have a stack overflow: it seems some function is being called recursively. You didn't define your own output function for string by any chance? What is in "more code here" which may be related to output operators?