-
Stringstream problem
Hey there im having a problem trying to compile my code, i get :
Code:
C:\money.cpp variable `std::stringstream ss' has initializer but incomplete type
Im not totally sure why, i've tried googling about but am still stumped by this, code:
money.h:
Code:
#ifndef MONEY_H
#define MONEY_H
using namespace std;
class parse{
private:
string input;
public:
parse();
~parse();
double parseIn(string& unparse);
};
#endif
money.cpp:
Code:
#include <iostream>
#include <string>
#include "money.h"
parse::parse(){
input = "0";
}
parse::~parse(){
string *ptr;
ptr = &input;
delete ptr;
}
double parse::parseIn(string& inStr){
stringstream ss(inStr);
double out;
ss >> out;
return out;
}
main.cpp:
Code:
#include <iostream>
#include <string>
#include "money.h"
int main()
{
string in;
parse testParse;
cout << "enter stuff: " << endl;
cin >> in;
testParse.parseIn(in);
cout << "You entered: " << in << endl;
testParse.~parse();
return 0;
}
-
There is no such thing as std::stringstream unless and until you #include <sstream>.
-
-
Code:
parse::~parse(){
string *ptr;
ptr = &input;
delete ptr;
}
Code:
testParse.~parse();
What are you doing there? It's not relevant to your initial question, but that code makes no sense and is wrong in several ways. If you describe what you're trying to accomplish then perhaps an alternative can be suggested. In general, simply deleting the code I posted from your original code would be your best bet.
-
Yeah sorry im having a hard time trying to understand some of these concepts, and im under alot of pressure atm, either way im trying to write some code that will take in strings and strip the integers(or doubles) out of the string to be able to perform mathmatic operaions on(working out a cars tow load etc etc), not working out how i want it too though.
I thought this code:
Code:
double parse::parseIn(string& inStr){
stringstream ss(inStr);
double out;
ss >> out;
if(ss.fail()){
throw (inStr + " is not a number");
}
return out;
}
would do just that (parse numbers from words that is), but its not happening, the returned value is still a string type because i cant do work with it via operators(minus, multiply etc)
Code:
int main()
{
string in1, in2;
double out;
parse testParse;
cout << "enter stuff: ";
cin >> in1;
try{
testParse.parseIn(in1);
}
catch(string& e){
cerr << e << endl;
}
cout << "enter another number: ";
cin >> in2;
try{
testParse.parseIn(in2);
}
catch(string& e){
cerr << e << endl;
}
out = in1+in2;
cout << out << endl;
cin.ignore();
cin.get();
return 0;
will result in:
Code:
cannot convert `std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to `double' in assignment
-
Well obviously, out is a double and in1 & in2 are std::strings. You can't put a square peg in a round hole.
-
You need to store the return value of the call to parseIn for both inputs. Then use those values instead of in1 and in2 when you add them together and assign to out.