Why did I get error by using strchr() in C++? [duplicate] - c++

This question already has answers here:
C++ string equivalent for strrchr
(4 answers)
Closed 6 years ago.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main(){
string a="asdasd";
if(!strchr(a,'a')) cout<<"yes";
return 0;
}
I just began to learn C++ programming and I don't know why I got error in this line
if(!strchr(a,'a')) cout<<"yes";
But if I tried to code it like this, it would run very well.
if(!strchr("asdasd",'a')) cout<<"yes";
I know it is a stupid question but I really don't know why.. sorry..

The library function strchr is for use with C-style strings, not the C++ string type.

When using std::string, the closest equivalent of strchr is find:
#include <iostream>
#include <string>
int main(){
std::string a="asdasd";
if(a.find('a') != std::string::npos) std::cout<<"yes";
}

Related

How to insert data into an array of single structure data type [duplicate]

This question already has answers here:
Single quotes vs. double quotes in C or C++
(15 answers)
Closed 1 year ago.
Im trying to put data into an array
#include <iostream>
#include <stdio.h>
#include <cstring>
using namespace std;
struct table {
int number;
double rate, hour;
string name;
} test[5];
int main()
{
test[0]={2,0.0,1.1,'m'};
test[1]={2,0.0,1.1,'m'};
return 0;
}
I know the syntax is wrong for this test[0]={2,0.0,1.1,'m'};. please correct it.
You have the wrong type of quotes: 'm' is a char but you need a std::string. Change it to "m" and your code compiles.

"gets() was not declared in this scope" error [duplicate]

This question already has answers here:
why g++ shows "gets()" not declared ,even after including <cstdio>
(3 answers)
Closed 3 years ago.
With the following code, I get the "gets() was not declared in this scope" error:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
// string str[]={"I am a boy"};
string str[20];`
gets(str);
cout<<*str;
return 0;
}
The function std::gets() was deprecated in C++11 and removed completely from C++14.
As gets() is a C style function, so if you need to include it in your c++ code then you need to include the header file called stdio.h and moreover you can only pass a c style string to gets() function not c++ string class.
So after slight modification in your code it becomes:
#include <iostream>
#include <string.h>
#include "stdio.h"
using namespace std;
int main()
{
// string str[]={"I am a boy"};
char str[20];`
gets(str);
printf("%s",str);
return 0;
}

Error C++ : does not name a type [duplicate]

This question already has answers here:
"Y does not name a type" error in C++
(2 answers)
Closed 7 years ago.
I have a ploblem with the next code, I get the error: 'ptab' does not name a type and 'pfreeC' does not name a type, I don't understand how to solve that, thanks for your help =)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
int *ptab; //Here is the error
ptab=new int[64];
bool *pfreeC; //Here is the error
pfreeC=new bool[11];
The problem is that you have code outside a function body
using namespace std;
int *ptab;
bool *pfreeC;
int main()
{
ptab = new int[64];
pfreeC = new bool[11];
return 0;
}
Of course you should also delete the allocated memory. Or even better, use smart pointers.

expected expression in c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
No output for cout
i writed this code in c++ for my uni , but i have an error in return 0 , the code don't work . i am using xcode to develop
#include <iostream>
#include <string>
using std::string;
int main( void )
{
string portF("PORTOFINO IM SOMMER 2012");
std::cout<<portF<<
portF.erase(0,5);
portF.insert(3,"IT");
portF.erase(7,3);
portF.insert(13,"SEMESTER");
portF.append("!");
std::cout<<portF<<
return 0;
}
std::cout<<portF<<
should be
std::cout<<portF;
Note you've made the same error twice. A semicolon is what ends a statement. When you put an insertion operator instead of it, compiler expects another expression (and that's what it is telling you).
use:
std::cout<<portF;
instead of,
std::cout<<portF<<
1. You haven't added a semicolon after the statement.
2. you are using one extra << operator
#include <iostream>
#include <string>
using std::string;
int main( void )
{
string portF("PORTOFINO IM SOMMER 2012");
// You have to end this statement with semi colon
std::cout<<portF;
portF.erase(0,5);
portF.insert(3,"IT");
portF.erase(7,3);
portF.insert(13,"SEMESTER");
portF.append("!");
// Similarily here
std::cout<<portF;
return 0;
}

c++ toupper - standard function? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Convert a String In C++ To Upper Case
Hi,
I need a portable function to convert string in c++ to upper case. I'm now using toupper( char); function. Is it a standard function? If not, what it's the correct way to do it across platforms? Btw, is there any web / wiki where I can list all c++ standard functions? Thank you.
Yes, toupper is declared in the cctype header. You can transform a string with an algorithm:
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
int main()
{
std::string str("hello there");
std::cout << str << '\n';
std::transform(str.begin(), str.end(), str.begin(), std::toupper);
std::cout << str << '\n';
}
For the latter question, there's http://www.cplusplus.com/.
Hi in our project we use boost/algorithm/string to_upper function project for windows and linux