Using macros to parse string to expression C++ - c++

I tried to write a program that takes input as a string and then passes that string to a macro which is supposed to insert the string as a plain-text expression but the macro is not behaving as I would suspect.
#include <iostream>
#include <cmath>
#include <string>
#define PARSE(a) a;
using namespace std;
int main() {
string c ;
int b;
cin >> c;
b = PARSE(c);
cout << b;
return 0;
}
This code will not compile, it gives an error saying that I cannot convert string to int, however PARSE(c) should not be treated like a string it should just be replaced by plain text.

The error means you are really trying to convert std::string (c) to int and it can't be done (b).
If you want to access the plain text, once you are using std::string, you should call the c_str() method, example:
cout << c.c_str();
#edit:
If you are trying to do something like "eval()" from PHP, you can check this: https://stackoverflow.com/a/11078610/12385171

Related

how do i fix this no matching function for call to 'stoi(int&)'|

i keep getting this error. i know this is a c++ 11 function but it still isnt working with code blocks c++ compiler. am i using this function correctly of is it a problem with the codeblocks compiler. i tried changing the compiler. using the "have g++ follow the c++11 iso standard" i still keep getting this error. or getting the "stoi() does not exist in the current scope" error
#include <iostream>
#include <string>
using namespace std;
int main()
{
int test = 34;
cout << stoi(test);
}
stoi means "String To Int". It will read an int from a std::string (or std::wstring). See also the reference.
You were probably looking for the reverse std::to_string (reference). But you don't need either, there is no need to convert to string before printing:
#include <iostream>
int main()
{
int test = 34;
std::cout << test;
}
stoi means string to int. So it takes a string as an input.
This should work:
string test = "34"; cout << stoi(test);

"no operator >> matches these operands"

I'm a complete noob at C++, and the first problem I am encountering is the following:
no operator >> matches these operands
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "hello world!";
cin >> "hello world!";
}
std::cin needs to write to a variable, but you are passing it a const char[13] string literal instead.
You need to pass it something like a std::string instead:
std::string str;
std::cin >> str;
P.S. This is a good time to a) read compiler messages, b) avoid using namespace std; globally, c) get a good C++ book.

C++ Using Visual Studio 2020 cant use .length()

I am sorry if this is really basic but I just started and am extremely confused. I am trying to find the length of a string s using .length() I have included #include , #include "genlib.h"and #include "simpio.h", but am still getting an error.
#include <iostream>
#include "genlib.h"
#include "simpio.h"
#define MAX_HASH_CODE 10000
int Hash( int maxCode, string s);
#define Multiplier -1664117991L // Multiplier used in Hash function
int Hash(int maxCode, string s)
{
unsigned long hashcode = 0;
for (int i = 0; i < s.length(); i++)
hashcode = hashcode * Multiplier + s[i];
return (hashcode % maxCode);
}
int main ()
{
std::cout << "Please enter your name: ";
string name = GetLine();
int hashcode = Hash(MAX_HASH_CODE, name);
std::cout << " The hash code for your name is " << hashcode << "." <<std::endl;
return 0;
}
s.length() just gives an error and says:
request for member 'length' in 's', which is of non-class type 'string' {aka 'char*'}gcc
image of error
By looking at your error message, it looks like you're using the Visual Code IDE, not Visual Studio.
The reason why you are experiencing the error is because it does not recognize s as a string because you have not defined it as such. Even though you declared s to be of type string, it is not the std string. The data type string is only available if you import the <string> library with #include <string>; however, in C++ all standard libraries are including inside a namespace, std. To access the functions like cout and string inside of the std just include using namespace std; at the top like an include statement. However, note that by doing this any methods you declare with the same name as a function inside the std namespace will create a name conflict. You can instead just call the function by the namespace prefix std::, to which you are using by std::cout.
To fix your problem either import the <string> library, include the std namespace, or every time you want to use a string, use std::string s to clarify that you want the std string.
Method 1 (Only works in C, not C++):
#include <string>
string s;
int length = s.length()
Method 2:
using namespace std;
string s;
int length = s.length()
Method 3:
std::string s;
int length = s.length()
Also, a similar question like this has been posted here:
Why am I getting string does not name a type Error?

When do I use '#include <string>' at the start of a C++ program?

I am confused about the use of #include <string> at the start of a program. For example, in the code below, I don't use #include <string> but the function will still print out the string "Johnny's favorite number is" when it is run.
#include <iostream>
using namespace std;
void printVariable(int number){
cout << "Johnny's favorite number is" << number << endl
}
However, in this code below, it does contain #include <string>.
#include <iostream>
#include <string>
using namespace std;
class Var{
public:
void setName(string x){
name = x;
}
string getName(){
return name;
}
private:
string name;
};
int main(){
Var Classy;
Classy.setName("Johnny Bravo");
cout << Classy.getName() << endl;
return 0;
}
Do I only use #include <string> if a variable represents a string?
Do I only use #include <string> if a variable represents a string?
Yes.
Use #include <string> when you use a variable that has type std::string.
The code "text here", contrary to intuition, is not a std::string; it is a string literal, and a C-style string, and a const char[10] convertible to const char*. Welcome to C++ with its legacy oddities.
Your question arises from the fact that you know that something like "aabcd" is a string literal. So, its type should be string. Well, that's not quite true.
C++ has a lot of features from C. Including data types. So, that is a pointer to char (char*), not a string(an instance of the string class). You can create an instance of the string class from a char* (including a string literal) by passing it as argument to the constructor of string. But it is not a string, it's just some misleading terminology.
A similar case is calling things vectors when they are arrays.
If you use the type std::string in your code then you should include the <string> header. There are also a few other types and functions in that header, but std::string is the most commonly used one.
However, you do not need to include this header just to use string literals, which are built into the core language.
In your first case, library "string" is not needed. The object "cout" is supported by library "iostream", thus you have:
#include <iostream>
For the second case, you do explicitly use "string", thus library "string" is required:
#include <string>

C++ String Declaring

I have been working with VB for a while now. Now I'm giving C++ a shot, i have came across strings, i cant seem to find a way to declare a string.
For example in VB:
Dim Something As String = "Some text"
Or
Dim Something As String = ListBox1.SelectedItem
Whats the equivalent to the code above in C++ ?
Any help is appreciated.
C++ supplies a string class that can be used like this:
#include <string>
#include <iostream>
int main() {
std::string Something = "Some text";
std::cout << Something << std::endl;
}
using the standard <string> header
std::string Something = "Some Text";
http://www.dreamincode.net/forums/topic/42209-c-strings/
In C++ you can declare a string like this:
#include <string>
using namespace std;
int main()
{
string str1("argue2000"); //define a string and Initialize str1 with "argue2000"
string str2 = "argue2000"; // define a string and assign str2 with "argue2000"
string str3; //just declare a string, it has no value
return 1;
}
Preferred string type in C++ is string, defined in namespace std, in header <string> and you can initialize it like this for example:
#include <string>
int main()
{
std::string str1("Some text");
std::string str2 = "Some text";
}
More about it you can find here and here.
In C++, strings' are not primitive data types. Therefore, we have to #include the string class OR use the std namespace. Here is an example of using namespace.
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello.\n"<<"What is your name?\n";
string name;
cin>>name;
cout<<"Hello "<<name<<"!";
}