C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-21-2009, 07:39 PM   #1
Registered User
 
Join Date: Jan 2009
Posts: 2
Unhappy Strings

What i'm trying to do is, ignore the first 14 digits of the variable w with a string, and use only the other 7, but i've been unsuccessful with my attempts. Could someone explain to me in detail how to fix this or just fix it for me. Here's my original code.

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();
}
Here's my attempt at fixing it which returned the error:

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   Reply With Quote
Old 01-21-2009, 08:06 PM   #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;
I think you need to cast the string to a double.
Kudose is offline   Reply With Quote
Old 01-21-2009, 08:26 PM   #3
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 01-21-2009, 09:23 PM   #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   Reply With Quote
Old 01-21-2009, 10:47 PM   #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   Reply With Quote
Old 01-21-2009, 11:15 PM   #6
CSharpener
 
vart's Avatar
 
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   Reply With Quote
Old 01-21-2009, 11:45 PM   #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   Reply With Quote
Old 01-22-2009, 02:30 AM   #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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 01-22-2009, 02:55 AM   #9
Registered User
 
Join Date: Dec 2006
Posts: 1,780
Quote:
who cares?
Exactly.
cyberfish is offline   Reply With Quote
Old 01-22-2009, 03:58 AM   #10
Hail to the king, baby.
 
Akkernight's Avatar
 
Join Date: Oct 2008
Location: Faroe Islands
Posts: 714
If I understood what cyperfish just said correctly, then hah! Nicely said xD
__________________
I deny the Holy Spirit; burn me.

http://blasphemychallenge.com/ <- Do you dare?
Akkernight is offline   Reply With Quote
Old 01-22-2009, 06:44 AM   #11
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by Corus View Post
Tabstop, who cares?
I don't remember who I'm ripping off (I think bash.org):
Quote:
If we ask you "why do you need to know about jackhammers", and your answer is "so I can open my grandmother's skull and release the demons that are giving her cancer", then maybe teaching you how to use a jackhammer isn't the right thing to do.
In other words, more context may be needed (for instance do you mean 7 digits or 7 characters -- since it's a double there might be a decimal point running loose), etc.
tabstop is offline   Reply With Quote
Old 01-22-2009, 10:46 AM   #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   Reply With Quote
Reply

Tags
strings

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22