Strings

This is a discussion on Strings within the C++ Programming forums, part of the General Programming Boards category; What i'm trying to do is, ignore the first 14 digits of the variable w with a string, and use ...

  1. #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();
    }

  2. #2
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    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.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,260
    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);
    }

  5. #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?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    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;
    }

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    who cares?
    Exactly.

  10. #10
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    If I understood what cyperfish just said correctly, then hah! Nicely said xD
    Currently research OpenGL

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Corus View Post
    Tabstop, who cares?
    I don't remember who I'm ripping off (I think bash.org):
    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.

  12. #12
    Registered User Kudose's Avatar
    Join Date
    Jun 2006
    Posts
    92
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

Tags for this Thread


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