This question already has answers here:
Problems with std::stoi, not working on MinGW GCC 4.7.2
(2 answers)
Closed 6 years ago.
I want to convert string to float with stof but it is not working
Also I was enabled c++ 11 in codeblocks
but give error me that "stof was not declared in this scope"
if I use of std::stof yet give error me that it is not member of std
Here is my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x;
x="23";
float y=stof(x)+2.1;
cout<<y;
return 0;
}
try this:
include <sstream>
std::stringstream ss;
ss << "23";
float f;
ss >> f;
You have declared y to be an integer.
By definition, integers do not have fractional or decimal parts.
When you add 2.1 it will truncate the decimal.
Related
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.
This question already has answers here:
Why is "using namespace std;" considered bad practice?
(41 answers)
Closed 5 years ago.
In my function sumofrange I decided to output an undeclared variable just to learn the different compiler errors in C++. To my surprise, time seems to output 1 even though it is not declared anywhere.
#include <iostream>
#include <cmath>
using namespace std;
int sumOfrange( int lower, int upper){
cout<<time<<endl;
return ((( (pow(upper,2)) + upper) - ((pow(lower,2)) + lower)) / 2);
}
int main(){
cout<<sumOfrange(7,100)<<endl;
return 0;
}
You are outputting the address of a std::time function declared in a <ctime> header. You are also using a using namespace std; statement. Why that should be avoided is explained in this SO post. Depending on the compiler and the platform you might get the hexadecimal output similar to (0x)00DC52E0 if using a VC++ compiler on Windows or a number 1 if using a g++ compiler on Linux.
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";
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert a number to string and vice versa in C++
I am using Qt Creator 2.5.0 and gcc 4.7 (Debian 4.7.2-4). I added "QMAKE_CXXFLAGS += -std=c++11" to .pro file. Everything seems to be OK, I used C++11 std::for_each and so on. But when I included "string" header and wanted to use stoi, i got the following error:
performer.cpp:336: error: 'std::string' has no member named 'stoi'
I found some questions related to MinGW and one more, to Eclipse CDT and they had their answers. But I use Linux, why it is NOT working here?
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = stoi(test);
std::cout << myint << '\n';
}
or
#include <iostream>
#include <string>
using namespace std
int main()
{
string test = "45";
int myint = stoi(test);
cout << myint << '\n';
}
look at http://en.cppreference.com/w/cpp/string/basic_string/stol
std::stoi is a function at namespace scope, taking a string as its argument:
std::string s = "123";
int i = std::stoi(s);
From the error message, it looks like you expect it to be a member of string, invoked as s.stoi() (or perhaps std::string::stoi(s)); that is not the case. If that's not the problem, then please post the problematic code so we don't need to guess what's wrong with it.
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;
}