Converting a string to float using std::istringsteam in C++ - c++

I've been trying to convert a simple string to a float, but I'm having no luck with it. this is what I've got at the moment:
int main()
{
float value;
std::string stringNum = "0.5";
std::istringstream(stringNum) >> value;
return 0
}
but I'm getting this error:
Error 2 error C2440: '<function-style-cast>' : cannot convert from 'std::string' to 'std::istringstream' c:\users\administrator\desktop\Test\main.cpp 12
can anyone give me some guidance here on how to just simply convert the string to a float?
Thanks

Most likely you haven't included all the relevant headers:
#include <string>
#include <sstream>
Here is a live example showing that your code compiles when the appropriate headers are included.
In general, you should not rely on indirect inclusion of a necessary standard header file from another standard header file (unless, of course, this inclusion is documented in the Standard itself).
Also notice, that you are creating a temporary string stream, which will be destroyed at then end of the evaluation of the expression
std::istringstream(stringNum) >> value
You may want to create a stream object this way instead:
std::istringstream ss(stringNum);
ss >> value;
// Here you can use ss again...

Related

Compiling error while using getline(): 'mismatched types'

I keep getting this error (it's a really long one but I think the most important part is this):
main.cpp:9:30: note: mismatched types 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>' and 'const char [2]'
While compiling this bit of code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string x = getline(cin, " ");
return 0;
}
The lines in the error won't match with the ones in the code I brought up here because I don't know how to make a new line whilst writing code in the Stack Overflow editor; I'm new here ;) Anyways, the error points to the line with the declaration of string x.
Basically what I want this code to do is to get a line from the user until he/she hits space. Maybe I'm doing something wrong from the beginning, so I'm open for suggestions of fixing this problem. (I'm not very experienced in C++, it's just that my teacher required to complete a task using this language.) Thanks,
Anthony
The second parameter of std::getline() is a reference to a std::string variable that accepts the read data. The string is not outputted in the function's return value.
Also, std::getline() does not accept a string for the delimiter. It takes only a single character.
Try this instead:
#include <iostream>
#include <string>
using namespace std;
int main() {
string x;
getline(cin, x, ' ');
return 0;
}

Visual Studio not recognizing stod() even with precompiled headers turned off and<string> included

Below is a snippet to show the basic structure of my code right now. On that last line, Visual Studio is saying "no instance of overloaded function that matches the argument list". According to the references I can find from VS help, however, this is exactly how the function should be used. I have string and iostream included, and I have precompiled headers turned off, so I'm not sure if the fault for VS not recognizing this function is on me or not.
#include <iostream>
#include <vector>
#include <string>
int main(){
Stack<double> nums;
std::string input;
std::string::size_type index;
std::cin >> input;
double num = std::stod(input, index);
}
It should be a pointer to size_type. Verified in VS.
double num = std::stod(input, &index);
When overloading is around, you must be precise :)

More than one instance of overloaded function to_string matches the argument list

I did exactly what was written here: Easiest way to convert int to string in C++
But I get an error at the std of std::to_string
#include <iostream>
#include <string>
int main()
{
std::string s = std::to_string(42);
return 0;
}
The error message you get can't be generated for a standard-conforming library implementation.
So, the best solution is to upgrade the compiler (presumably it's some years old Visual C++).
An alternative is to use an argument of type long, and hope that that's one of the existing overloads:
std::to_string( 42L )

C++ compiler error with stringbuf / ifstream

I cannot understand why my compiler (MSVC++2010) doesn't like this code:
// get_sum(filename as c-string) returns sum from file
int get_sum(const char* const s) {
stringbuf bill_buf;
ifstream bill_file;
bill_file.open(s);
bill_file.get(bill_buf, '\0'); // read whole file
bill_file.close();
return get_sum_from_string(bill_buf.str());
}
I get these errors (I translated them from German to English and give the correct line numbers for the code excerpt without leading comment):
Error 1 error C2079: 'bill_buf' uses undefined class 'std::basic_stringbuf<_Elem,_Traits,_Alloc>' (Line 2)
Error 2 error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem *,std::streamsize)': Conversion of parameter 1 from 'int' to 'char *' not possible (Line 5)
Error 3 error C2228: To the left of ".str" there must be a class/structure/union. (Line 7)
Has anyone got an idea what's going on there? Thanks a lot! (If anyone has got a better idea how to quickly get the whole file contents into a string, I'd also appreciate it)
You're missing an include. Here's your code, this time without using streambuf:
#include<fstream>
#include<string>
#include<iterator>
int get_sum(const char* const s) {
std::ifstream bill_file(s);
std::string contents((std::istreambuf_iterator<char>(bill_file)),
std::istreambuf_iterator<char>());
return get_sum_from_string(contents);
}
For #1, you probably forgot to #include <sstream> and only have a forward declaration from some other header in scope. #2 and #3 are follow-up errors, don't mind them, fix #1 first and go on.
Looks like you need to #include <sstream>.
1) In your header file (.h) you should specify "using namespace std". Otherwise, all your stream operations/variables etc have to start with 'std::'
2) Have you included the right headers? You should add
#include <sstream>

"String.h" VS <string.h>

Friends
On HP-UX box when Iam passing a string object to function
Im getting the following below error
Error 422: "../header/Handler.h", line 24 # 'string' is used as a type, but has not been
defined as a type. Perhaps you meant 'String' as in class String
["/opt/aCC/include/SC/String.h", line 66].
int populateBindingHandle(rpc_if_handle_t p_if_spec, string p_cell_name);
why would I get an error to use String.h not
how does a declaration String newstr;
different from
string newstr; ??
Many Thanks
Looks like there is a String class in the header mentioned by the compiler. The compiler thinks you made a typo.
If you want to use STL strings use the following:
#include <string>
int populateBindingHandle(rpc_if_handle_t p_if_spec, std::string ...)
or have a using declaration somewhere:
using std::string;
int populateBindingHandle(rpc_if_handle_t p_if_spec, std::string ...)
Note, the old-style headers have been deprecated, i.e. you should no longer use #include <string.h>