I have problem with converting integer to string in C++ using CodeBlocks and GCC Compiler. I tried with this but it eject error:
#include <iostream>
#include <string>
int main()
{
int clicks = 0;
string code = to_string(clicks); //error: "to_string" was not declared in this scope
}
And also i tried:
#include <iostream>
#include <string>
int main()
{
int clicks = 0;
std::string code = std::to_string(clicks); // error: "to_string" is not memeber of "std"
}
Is there any solution? Pls help
You need to have support for C++11. How I figured that out? I checked the ref here and saw the C++11 icon.
See this answer on how to do this.
It pretty much says to follow these steps:
Go to Toolbar -> Settings -> Compiler
In the "Selected compiler" drop-down menu, make sure "GNU GCC
Compiler" is selected
Below that, select the "compiler settings" tab and then the
"compiler flags" tab underneath
In the list below, make sure the box for "Have g++ follow the C++11
ISO C++ language standard [-std=c++11]" is checked
Click OK to save
You need to use stringstream like so:
stringstream ss;
ss << clicks;
std:string code = ss.str();
Include sstream.
Related
I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}
I am trying to take a string and parse it into an int. I have read the many answers out there, and it seems that using stoi is the most up-to-date way. It appears to me that stoi uses std, but I am getting Function 'stoi' could not be resolved despitre using namespace std;
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include<stdlib.h>
using namespace std;
int main(int argc, char* argv[]) {
string line = "";
string five = "5";
int number = stoi(five); //Error here with stoi
return 0;
}
Any ideas what is causing this?
Update:
I am using Eclipse. My flags are: -c -fmessage-length=0 -std=c++11
If you are using GCC or MINGW, then this is the answer:
std::stoi doesn't exist in g++ 4.6.1 on MinGW
This is a result of a non-standard declaration of vswprintf on
Windows. The GNU Standard Library defines
_GLIBCXX_HAVE_BROKEN_VSWPRINTF on this platform, which in turn disables the conversion functions you're attempting to use. You can
read more about this issue and macro here:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37522.
If you're willing to modify the header files distributed with MinGW,
you may be able to work around this by removing the
!defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF) macro on line 2754 of
.../lib/gcc/mingw32/4.6.1/include/c++/bits/basic_string.h, and adding
it back around lines 2905 to 2965 (the lines that reference
std::vswprintf). You won't be able to use the std::to_wstring
functions, but many of the other conversion functions should be
available.
Please always provide platform and compiler information.
Toggle on C++11 support in your compiler flags. -std=c++11 for a recent gcc. For Eclipse, please refer to the corresponding question in the FAQ and this answer explains how to get rid of the remaining Eclipse warning.
If you are amenable to parsing an int another way, how about using an STL algorithm and a C++11 lambda expression?
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "12345";
int num = 0;
for_each(str.begin(), str.end(), [&num](char c){ num = 10 * num + (c - '0'); });
cout << str << " = " << num << endl;
}
Hi I am just starting to learn C++. I bought this big C++ for Dummies book and have been going through it. Its been really interesting so far but now I am stuck. I have been googling this problem, but to no avail. I am using I am using codeblocks 10.05 with GNU GCC.
I keep getting an error that says:
In function 'main':
undefined reference to 'SafeCracker(int)'
The code isn't complicated. I am just new and am extremely frustrated. I don't want to skip over this part; I want to know what is going on.
Main:
#include <iostream>
#include "safestuff.h"
using namespace std;
int main()
{
cout << "Surprise, surprise!" << endl;
cout << "The combination is (once again)" << endl;
cout << SafeCracker(12) << endl;
return 0;
}
Function:
#include <iostream>
using namespace std;
string SafeCracker(int SafeID)
{
return "13-26-16";
}
Header:
using namespace std;
#ifndef SAFESTUFF_H_INCLUDED
#define SAFESTUFF_H_INCLUDED
string SafeCracker(int SafeID);
#endif // SAFESTUFF_H_INCLUDED
You are not compiling the second file you listed along with the first one. Try compiling directly with gcc to understand this.
assuming your files are named:
main.cpp
SafeCracker.cpp
safestuff.h
This is what you are doing
gcc main.cpp
While you should be doing this
gcc main.cpp SafeCracker.cpp
Also, SafeCracker.cpp should be including the header file as well, just for clarity. Any reasons why you have them separated?
On another note, from seeing Daniel Hu's answer, <iostream> is automatically including <string> for you. You should not depend on this functionality, and should instead include <string> in each file that uses strings.
(From comment below)
You're probably trying to build your main.cpp as a stand-alone file. This will leave SafeCracker.cpp uncompiled. What you need is create a project in Codeblocks and add all three files to it (both *.cpp files as well as the *.h file).
I think it's because you did not #include <string>
C++ has to import the string library to use strings or else everything is treated as char arrays.
so I have this code:
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
...
char* b = new char [10];
strcpy(b, "1234567890");
error: microsoft visual studio 11.0\vc\include\string.h(110) : see declaration of 'strcpy'
How do I fix it?
A quick fix is to add the _CRT_SECURE_NO_WARNINGS definition to your project's settings
Right-click your C++ and chose the "Properties" item to get to the properties window.
Now follow and expand to, "Configuration Properties"->"C/C++"->"Preprocessor"->"Preprocessor definitions".
In the "Preprocessor definitions" add
_CRT_SECURE_NO_WARNINGS
but it would be a good idea to add
_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)
as to inherit predefined definitions
IMHO & for the most part this is a good approach.
There's an explanation and solution for this on MSDN:
The function strcpy is considered unsafe due to the fact that there is
no bounds checking and can lead to buffer overflow.
Consequently, as it suggests in the error description, you can use
strcpy_s instead of strcpy:
strcpy_s( char *strDestination, size_t numberOfElements,
const char *strSource );
and:
To disable deprecation, use
_CRT_SECURE_NO_WARNINGS. See online help for details.
http://social.msdn.microsoft.com/Forums/da-DK/vcgeneral/thread/c7489eef-b391-4faa-bf77-b824e9e8f7d2
The message you are getting is advice from MS that they recommend that you do not use the standard strcpy function. Their motivation in this is that it is easy to misuse in bad ways (and the compiler generally can't detect and warn you about such misuse). In your post, you are doing exactly that. You can get rid of the message by telling the compiler to not give you that advice. The serious error in your code would remain, however.
You are creating a buffer with room for 10 chars. You are then stuffing 11 chars into it. (Remember the terminating '\0'?) You have taken a box with exactly enough room for 10 eggs and tried to jam 11 eggs into it. What does that get you? Not doing this is your responsibility and the compiler will generally not detect such things.
You have tagged this C++ and included string. I do not know your motivation for using strcpy, but if you use std::string instead of C style strings, you will get boxes that expand to accommodate what you stuff in them.
I had to use strcpy_s and it worked.
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
struct student
{
char name[30];
int age;
};
int main()
{
struct student s1;
char myname[30] = "John";
strcpy_s (s1.name, strlen(myname) + 1 ,myname );
s1.age = 21;
cout << " Name: " << s1.name << " age: " << s1.age << endl;
return 0;
}
Add this line top of the header
#pragma warning(disable : 4996)
If you are getting an error saying something about deprecated functions, try doing #define _CRT_SECURE_NO_WARNINGS or #define _CRT_SECURE_NO_DEPRECATE. These should fix it. You can also use Microsoft's "secure" functions, if you want.
For my problem, I removed the #include <glui.h> statement and it ran without a problem.
I've a problem with my CDT. Code completion doesn't work for standard library classes.
For example in this code after entering x. and presing ctrl+space IDE doesn't display the list of API elements.
#include
void f() {
string x = "sss";
x.
}
String and vector header files are available in Includes directories. When I press ctrl+click on the include line I'm redirected to header file.
Code completion seems to work fine for C std library.
My version of eclipse:
Eclipse IDE for C/C++ Developers
Version: Helios Release
Build id: 20100617-1415
Eclipse C/C++ Development Tools
Version: 7.0.0.201006141710
Build id: 201006141710
Please help.
Try this:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string str ("Test string");
cout << "The size of str is " << str.size() << " characters.\n";
return 0;
}
first: check whether it compiles, then place cursor before "return" and try code assist: "str." and see if it pops up.