![]() |
| | #1 |
| Registered User Join Date: Jan 2009
Posts: 2
| Code: #include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
long double w, t, p;
cout << "Please enter the price.";
cout << endl;
cin>>p;
t = 0;
cout << "Please scan the the weight.";
cin >>w;
cout << endl;
cout << endl;
t = w * p + t;
cout << endl;
cout <<"Total:"<<t;
cout << endl;
int i = 1000; while (i-- > 0) { cin >> w; t += w * p; cout << endl; cout<<"Total:"<<t; cout << endl; };
cin.get();
cin.get();
}
21 no match for 'operator*' in 'str2 * p' 25 no match for 'operator*' in 'str2 * p' Code: #include <iostream> /* Declaration */
#include <sstream>
#include <string>
using namespace std;
int main() {
long double t, p; /* Variables */
string str2, w;
cout << "Please enter the price."; /* output */
cout << endl; /* next line */
cin>>p; /* sets input as p */
t = 0; /* sets t as 0 */
cout << "Please scan the the weight."; /* output */
cin >>w; /* input as w */
cout << endl; /* next line */
cout << endl;
string str= w;
str2 = str.substr (14,7);
t = str2 * p + t;
cout << endl;
cout <<"Total:"<<t;
cout << endl;
int i = 1000; while (i-- > 0) { cin >> w; t += str2 * p; cout << endl; cout<<"Total:"<<t; cout << endl; };
cin.get();
cin.get();
}
|
| Corus is offline | |
| | #2 |
| Registered User Join Date: Jun 2006
Posts: 72
| I am pretty new to C++, so this may not be the problem ... but ... It looks like you are trying to multiply a string, which the compiler see as a pointer. Code: t = str2 * p + t; |
| Kudose is offline | |
| | #3 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| I guess the question is, what's wrong with the first 14 digits of w? Is it letters that you need to ignore? If they're all 0, who cares? If they're all numbers that aren't zero, why should you ignore them? |
| tabstop is offline | |
| | #4 |
| Registered User Join Date: Jan 2008
Posts: 575
| If the first 14 digits are important enough to ignore and the next 7 digits are important enough to store you probably need to input the value as a string. If the below example doesn't print '21' the 'double' type isn't going to work. You need to look at the interface for 'std::string' and 'std::stringstream'. You are trying to get the one to do the job of the other. Soma Code:
#include <iostream>
#include <limits>
int main()
{
typedef std::numeric_limits<double> double_limits;
if(double_limits::is_bounded)
{
std::cout << double_limits::digits10 << '\n';
}
else
{
std::cout << "unknown";
}
return(0);
}
|
| phantomotap is offline | |
| | #5 |
| Registered User Join Date: Jan 2009
Posts: 2
| None of you had answered my question. Phantom Map, if you gave me an example of that being used with a cin(So i could get a feel for it), then it would help, also how would i import it as a string? Tabstop, who cares? Kudose, how would I do that? |
| Corus is offline | |
| | #6 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| if you want to convert string to double (after you have cut the instreasting part) - use stringstream Code: std::string test("1.23");
std::stringstream stream(test);
double d;
stream >> d;
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #7 |
| Registered User Join Date: Jun 2006
Posts: 72
| You should probably use a much more experienced users approach, but the following works. Code: #include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
int p = 2;
string w;
cin >> w;
int t;
const char *cStyle = w.c_str();
t = atoi(cStyle) * p;
cout << t;
return 0;
}
|
| Kudose is offline | |
| | #8 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Do I see someone totally ignore the solution vart just gave out? And corus, start working on that attitude. If someone asks you a question, you can be polite enough to answer; otherwise we are wasting out time helping someone who is rude.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #9 | |
| Registered User Join Date: Dec 2006
Posts: 1,780
| Quote:
| |
| cyberfish is offline | |
| | #10 |
| Hail to the king, baby. Join Date: Oct 2008 Location: Faroe Islands
Posts: 714
| If I understood what cyperfish just said correctly, then hah! Nicely said xD |
| Akkernight is offline | |
| | #11 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| I don't remember who I'm ripping off (I think bash.org): Quote:
| |
| tabstop is offline | |
| | #12 |
| Registered User Join Date: Jun 2006
Posts: 72
| Not that my comments hold water, but vart and Elysia is right. You should be using the stringstreams (istringstream and ostringstream) in the header file sstream |
| Kudose is offline | |
![]() |
| Tags |
| strings |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Strings Program | limergal | C++ Programming | 4 | 12-02-2006 03:24 PM |
| Programming using strings | jlu0418 | C++ Programming | 5 | 11-26-2006 08:07 PM |
| Reading strings input by the user... | Cmuppet | C Programming | 13 | 07-21-2004 06:37 AM |
| damn strings | jmzl666 | C Programming | 10 | 06-24-2002 02:09 AM |
| menus and strings | garycastillo | C Programming | 3 | 04-29-2002 11:23 AM |