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?
Related
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);
This question already has answers here:
String Undeclared In C++
(5 answers)
Closed 1 year ago.
I am new to C++ and am currently working on a project that wants me to turn a Roman Numeral into a Hindu-Arabic number (our normal number system). I'm currently writing a class RomanNumeralType which needs to store a roman numeral type string. When I try to run the code below I get error code C3646: unknown override specifier and also error codes C4430 and C2061 which both address string (in bold) not being a valid identifier. It was my impression that #include fixed this however I am unsure why it is not working. I currently do not have a function that actually converts the numeral to a number but I want to fix this problem first. How do I get the string data type to work in my class?
Here is a minimally reproduced example of my code
//main
#include <iostream>
#include <iomanip>
#include "Header2.h"
#include <string>
using namespace std;
int main() {
string ans;
cout << "Please enter a string of Roman Numerals (M,D,C,L,X,V,I): " << endl;
cin >> ans;
}
RomanNumeralType r1(ans);
cout << "Your original Roman Numeral was: " << r1.getNumeral() << endl;
cout << "Your Roman Numeral as a integer is: " << r1.getNumber(r1.getNumeral()) << endl;
return 0;
}
//Header2.h
#ifndef HEADER2
#define HEADER2
#include <string>
class RomanNumeralType {
public:
**string** romanNumeral;
RomanNumeralType(**string** x)
{romanNumeral = x;}
**string** getNumeral()
{return romanNumeral;}
}
When you do #include <string> think of it as though you are pasting in the contents of a file shared across all developers that has all the definitions for string. That class lives within the std namespace, so when you want to use a string, you have to tell the compiler that string is in std. The preferred method is to use the fully qualified name, so every time you use string, you would replace it with std::string.
The other option is to do what you did in main.cpp, using namespace std;This tells the compiler that the namespace to look in is in std. This is generally considered bad practice for actual programs, but it is OK if it is a small, one off program like you seem to be writing.
If you do the first option, you will have to change everything in the std namespace such as cout, but it is clearer that you didn't write your own.
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
I was trying to play around with Strings in a Hangman program that I'm writing and couldn't get them to work so tried working with them on a simpler basis and I'm still having no luck.
As far as I've read online in the references and what other people have said this code should work:
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int main (int argc, char** argv){
string word = {"Hello"};
int length = strlen(word);
}
But I get this compiler error:
'string' was not declared in this scope
and consequently, 'word' is also not declared in scope.
Can anyone see what I'm doing wrong? I'm using the g++ compiler on Ubuntu if that makes a difference, no idea which version though.
You are confusing C and C++.
You included only C libraries, whereas std::string comes from the C++ header string. You'd have to write:
#include <string>
to use it. However, you'd then have to make other changes, such as not using strlen.
You should learn from your C++ book, not random posts on the internet (#lolirony)
C version
#include <string.h>
int main(void)
{
const char* word = "Hello";
const size_t length = strlen(word); // `size_t` is more appropriate than `int`
return 0;
}
C-like C++ version
#include <cstring>
using namespace std;
int main()
{
const char* word = "Hello";
const size_t length = strlen(word);
}
Idiomatic C++ version (recommended)
#include <string>
int main()
{
const std::string word = "Hello";
const std::size_t length = word.size();
}
'string' was not declared in this scope
You need to include the header <string> and refer to it as std::string. Also, strlen does not understand std::string or any user defined types, but you can use the size() method instead:
#include <string>
int main()
{
std::string word = "Hello";
size_t length = word.size();
}
<cstring> is the header for C++ support of C-style null-terminated strings. You should include <string>.
You haven't included the C++ string header in your project.
#include <string>
The libraries that you've included are all plain-C headers.
Additionally, strlen() doesn't work with a c++ string; you should use word.size() instead.
string is a specialization of standard class std::basic_string . It is declared in header <string>
So if you want "to play around with standard class std::string:" you need to include directive
#include <string>
Header <cstring> is not the same as header <string> and contains declarations of standard C functions such as strlen.
However there is no any sense to apply function strlen to an object of type std::string The compiler in this case will issue an error.
I advice you to play with the following code that to see the difference
#include <iostream>
#include <string>
#include <cstring>
int main (int argc, char** argv)
{
std::string word = "Hello";
std::string::size_type length = word.length();
std::cout << "Object word of type std::string has value "
<< word << " with length of " << length
<< std::endl;
std::cout << "The size of the object itself is " << sizeof( word ) << std::endl;
char another_word[] = "Hello";
size_t another_length = std::strlen( another_word );
std::cout << "Object another_word of type char [6] has value "
<< another_word << " with length of " << another_length
<< std::endl;
std::cout << "The size of the object itself is " << sizeof( another_word ) << std::endl;
}
I'm having trouble understanding where I went wrong with my code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[]) {
string str = "";
cin >> str;
remove(str.begin(), str.end(), ' ');
cout << str;
cin.ignore();
}
The error says "'remove': function does not take 3 arguments (C2660)"
Try adding
#include <algorithm>
"algorithm" is an STL header containing a lot of functions, including std::remove, which the OP is trying to call. The error he got was because there is another function that takes a single argument, called "remove", which deletes a file.