question on string in c++ - c++

does this work on string in c++?
string s="lomi";
cout<<s<<endl;
what is bad in this code?
#include <iostream>
#include <cstring>
using namespace std;
int main(){
string s=string("lomi");
for (int i=0;i<s.length();i++){
s[i]= s[i]+3;
}
std::cout<<s<<std::endl;
return 0;
}
?

Yes.
(after you have #included the corresponding headers, and using the std namespace, etc.)
Edit: What's wrong with your code is you should
#include <string>
instead of
#include <cstring>
cstring is C's string.h header, which defines functions like strlen, strcpy, etc. that manipulates a C string, i.e. char*.
string defines C++'s string class which you're using.

short answer: yes
long answer: string s = "lomi"works due to the string(const char*) constructor.

Works for me -- does it work for you?
Remember to do this first:
#include <ostream>
#include <string>
using namespace std;

yes, cout in C++ knows how to deal with strings

Yes it should compile and work, if you want to print "lomi\n", and you have included <string> and <ostream> and declared using namespace std;.

Related

All strings are unidentified

#include <iostream>
#include <time.h>
#include <string.h>
#include <stdio.h>
int main()
{
string msg;
printf("Enter the message that you wish to display as scroller: ");
getline(cin,msg);
msg=msg+". ";
int x=0;
while(1)
{
Scroll(msg);
wait(100);
system("cls");
x++;
}
cin.get();
return 0;
}
I Have this C code and all strings in the file say 'identifier "string" is undefined'. I tried including <string> instead of <string.h> but it didn't work. Why is it not working?
Add
using namespace std;
After includes (but before main). Or, better, use notion of:
std::string // instead of string
Update: I missed the point of this being C-question. I will leave this answer, but for the sake of formality, use it if you came from Google and you are working with C++.
This is C++ code, not C.
The compiler is probably getting confused because it cannot parse it, so then it finds C-like code and all identifiers do not exist.
The includes should be:
#include <iostream>
#include <ctime>
#include <string>
#include <cstdio>
You are also missing a:
using namespace std;
Plus the definitions for Scroll and wait etc.

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>

identifier "string" undefined?

I am receiving the error: identifier "string" undefined.
However, I am including string.h and in my main file, everything is working fine.
CODE:
#pragma once
#include <iostream>
#include <time.h>
#include <string.h>
class difficulty
{
private:
int lives;
string level;
public:
difficulty(void);
~difficulty(void);
void setLives(int newLives);
int getLives();
void setLevel(string newLevel);
string getLevel();
};
Can someone please explain to me why this is occurring?
<string.h> is the old C header. C++ provides <string>, and then it should be referred to as std::string.
You want to do #include <string> instead of string.h and then the type string lives in the std namespace, so you will need to use std::string to refer to it.
You forgot the namespace you're referring to. Add
using namespace std;
to avoid std::string all the time.
Because string is defined in the namespace std. Replace string with std::string, or add
using std::string;
below your include lines.
It probably works in main.cpp because some other header has this using line in it (or something similar).
Perhaps you wanted to #include<string>, not <string.h>. std::string also needs a namespace qualification, or an explicit using directive.
You must use std namespace. If this code in main.cpp you should write
using namespace std;
If this declaration is in header, then you shouldn't include namespace and just write
std::string level;
#include <string> would be the correct c++ include, also you need to specify the namespace with std::string or more generally with using namespace std;

std has no member 'getline'?

I'm trying to use std::getline, but my compiler is telling me that getline isn't identified?
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <fstream>
#include <cstdlib>
int main(){
using namespace std;
string line;
ifstream ifile("test.in");
if(ifile.is_open()){
while(ifile.good()){
getline(ifile,line);
}
}
}
std::getline is defined in the string header.
#include <string>
Also, your code isn't using anything from cstring, cstdio, cmath, or cstdlib; why bother including these?
EDIT: To clarify the confusion regarding the cstring and string headers, cstring pulls the contents of the C runtime library's string.h into the std namespace; string is part of the C++ standard library and contains getline, std::basic_string<> (and its specializations std::string and std::wstring), etc. -- two very different headers.
As ildjarn points out, the function is declared in <string>, and I'm suprised you didn't get an error at:
string line;
Also, this:
while(ifile.good()){
getline(ifile,line);
}
is not the way to write a read loop. You MUST test the success of the read operation, not the current stream state. You want:
while( getline(ifile,line) ) {
}
this is happening because getline comes from the string library, you need to #include <string> or #include <cstring>

String Undeclared In C++

I'm sure this is a really simple thing, but I haven't worked in C++ forever.
14 C:\Dev-Cpp\mainCurl.cpp `string'
undeclared (first use this function)
> #include <stdio.h>
> #include <curl/curl.h>
> #include <string>
> #include <iostream>
>
> int main(void) {
> string url("http://www.google.com"); //
> system("pause");
>
> return 0; }
What am I missing here?
Cheers
You haven't declared your namespace. You need to either declare:
using namespace std;
Or tell the compiler that "string" is in the standard namespace:
std::string url("...");
Or you can announce that you are specifically using std::string and only std::string from std by saying:
using std::string;
Add using namespace std; above the main() definition.
Also, you don't need <stdio.h> if you include <iostream>. Also, in C++ a function that doesn't take arguments doesn't need a "void" argument, simply use parentheses with nothing in between them.
This a so recurring problem...
You missed std:: before string, so it will look like std::string
That's because string belongs to std namespace and if you don't use using directive you must specify where string is.
Alternatively you can use
using namespace std; or more conveniently using std::string before using string class.
You need std::string or using std::string.
try std::string url ("http://www.google.com");
the string class is in std namespace