-
compilation error
i am writing a program which will find a string from an array of strings.
PHP Code:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
#include<vector>
using namespace std;
typedef std::<vector std::string> str;
str s("abcd","efgh","ijkl","mnop","123"); // string initialized
int main()
{
string s1="ijkl"; // target string
std::vector<std::string> iterator p ;
for(iterator p = s.begin(); p!=s.end();p++)
{
if( s1==s[p]) cout<<"string has been found"<<endl; // string has been found
}
int n = atoi("123"); // is it possible in C++ ?
cout<<n<<endl;
}
my errors >
error C2589: '<' : illegal token on right side of '::'
error C2589: '<' : illegal token on right side of '::'
warning C4091: 'typedef ' : ignored on left of 'int' when no variable is declared
error C2143: syntax error : missing ';' before '::'
error C2143: syntax error : missing ';' before '::'
error C2955: 'vector' : use of class template requires template argument list
c:\program files\microsoft visual studio\vc98\include\vector(244) : see declaration of 'vector'
error C2146: syntax error : missing ';' before identifier 's'
error C2501: 'str' : missing storage-class or type specifiers
fatal error C1004: unexpected end of file found
Error executing cl.exe.
why these errors ? how can i fix those errors ?
thanks
-
Code:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
#include<vector>
using namespace std;
typedef std::vector <std::string> str;
int main()
{
str s;
s.push_back("abcd");
s.push_back("efgh");
s.push_back("ijkl");
s.push_back("mnop");
s.push_back("123");
string s1="ijkl"; // Also unnecessary comment, use a better name
std::vector<std::string>::iterator p;
for(p = s.begin(); p!=s.end();p++)
{
if( s1==*p) cout<<"string has been found"<<endl; // Unnecessary comment
}
int n = atoi("123"); // Yes, it is possible. :-)
cout<<n<<endl;
}
The more obvious errors are:
>typedef std::<vector std::string> str;
Misplaced opening angle bracket. It should be:
Code:
typedef std::vector <std::string> str;
>str s("abcd","efgh","ijkl","mnop","123"); // string initialized
Sadly, you cannot initialize a vector like this.
>std::vector<std::string> iterator p ;
iterator is a data type of the vector class, use the scope resolution operator to access it:
Code:
std::vector<std::string>::iterator p;
>for(iterator p = s.begin(); p!=s.end();p++)
p already exists, you're trying to declare it again with an unknown type. This should be:
Code:
for(p = s.begin(); p!=s.end();p++)
>if( s1==s[p])
An iterator is like a pointer, not like an index. s[p] is an undefined operation. Instead, dereference the iterator like a pointer:
>int n = atoi("123"); // is it possible in C++ ?
Yes it is possible, but calling atoi on a string literal directly serves no purpose. It would be easier and faster to simply do this:
-
thanks prelude . i understood the errors. i have one more question.
Code:
#include<iostream>
#include<cstdlib>
#include<string>
#include<cmath>
#include<vector>
using namespace std;
int main()
{
string s1 = "123";
int n = atoi(s1); // code-1 .....this code is giving compile error
cout<<n<<endl;
int n = atoi("123"); // code-2....... this code is ok
cout<<n<<endl;
}
i want the code-1 ready... bcoz i want to store string in a variable and then want to use atoi() to get integers .
how can i remove error from code-1 ?
code-1 is giving error >
cannot convert `std::string' to `const char*' for argument `1' to `
int atoi(const char*)'
(g++ compiler)
what this error demands ?
is it possible to remove error from the code-1 ?
thanks
-
>int n = atoi(s1);
s1 is a C++ string, but atoi takes a C-style string. To get a const char * from a C++ string, use the c_str member function:
Code:
int n = atoi(s1.c_str());