Thread: stringstream help

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    stringstream help

    i'm struggling with understanding how to use stringstream objects

    i'm working on creating a program to calculate gross and net pay and i'm supposed to calculate both the pays in a function and return it in a stringstream to the main where it is displayed

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include <sstream>
    
    
    using namespace std;
    
    
    string AskForName();
    double AskForRate(string name);
    double AskForHours(string name);
    stringstream CalculatePay(double rate, double hours);
    
    int main()
    {
    	cout.setf(ios::fixed);
    	cout.precision(2);
    
    	float rate, hours;
    	string name;
    	stringstream pay;
    
    
    	name = AskForName();
    	rate = AskForRate(name);
    	hours = AskForHours(name);
    	pay = CalculatePay(rate, hours);
    
    	cout << pay;
    
    	system("pause");
    	return 0;
    }
    
    
    string AskForName()
    {
    	string name;
    
    	cout << "\nWhat is your name, good person? \n";
    	getline (cin, name);
    
    	return name;
    }
    
    double AskForRate(string name)
    {
    	double rate;
    
    	cout << "\nWhat is your hourly rate " << name << "? \n";
    	cin >> rate;
    
    	return rate;
    }
    
    double AskForHours(string name)
    {
    	double hours;
    
    	cout << "\nHow many hours a week do you work " << name << "? \n";
    	cin >> hours;
    
    	return hours;
    }
    
    stringstream CalculatePay(double rate, double hours)
    {
    	double gpay, npay;
    	stringstream pay (stringstream::in | stringstream::out);
    
    	
    
    	if (hours <= 40)
    	{
    		gpay = rate*hours;
    	}
    	
    	else if(hours > 40)
    	{
    		gpay = ((hours - 40.0)*rate*1.5) + (rate*40.0);
    	}
    
    	if (gpay < 250)
    	{
    		npay = gpay;
    	}
    	
    	else if( 250 <= gpay <= 500)
    	{
    		npay = gpay*.90;
    	}
    
    	else if(gpay > 500)
    	{
    		npay = gpay*.80;
    	}
    
    	?????
    	
    
    
    	return pay;
    }
    i'm not sure how to set up the CalculatePay function to return the stringstream

    any help to either that subject or the program in general would be appreciated
    Last edited by Airick92; 04-01-2011 at 07:46 PM.

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Well pay is a stringstream, which is a stream, so you can do:

    Code:
    pay << npay;
    However, you can't return a stringstream by value because that requires copying, and stringstreams can't be copied. You'd have to do something like this instead:

    Code:
    std::auto_ptr<stringstream> CalculatePay(double rate, double hours);
    or if you can use boost, this may be better as auto_ptr is deprecated:

    Code:
    boost::shared_ptr<stringstream> CalculatePay(double rate, double hours);

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Airick92
    i'm working on creating a program to calculate gross and net pay and i'm supposed to calculate both the pays in a function and return it in a stringstream to the main where it is displayed
    I think you misunderstood. You probably are supposed to output to a stringstream, or more likely a std::ostream. For example:
    Code:
    void CalculatePay(std::ostream& out, double rate, double hours)
    {
        double gpay, npay;
    
        // Your calculations
        // ...
    
        out << gpay << ' ' << npay;
    }
    Now in the main function you would call:
    Code:
    CalculatePay(cout, rate, hours);
    Mozza314 makes a good point in that you cannot pass/return such a stream by value, but the typical solution is to use references, not (smart) pointers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    here is the description of the problem

    Your program should use these functions:
    AskFor Name asks the user for his full name.
    AskForRate asks the user for his hourly rate in dollars
    AskForHours asks the user for the total number of hours he worked this week.
    CalculatePay is passed the name, rate, and hours value. It calculates the gross
    pay and net pay, and, using a stringstream object, returns a formatted string to main()
    where it is displayed to the screen. Show the hours worked and rate too.
    All functions are called and the resultant pay data is displayed from main().

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, then in fact you are supposed to return a string. The stringstream would be a local variable whose purpose is to help you format the string to return.

    By the way, name those variables gross_pay and net_pay, not gpay and npay.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by laserlight View Post
    Oh, then in fact you are supposed to return a string. The stringstream would be a local variable whose purpose is to help you format the string to return.

    By the way, name those variables gross_pay and net_pay, not gpay and npay.
    Or grossPay, netPay :-)

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Mozza314 View Post
    Or grossPay, netPay :-)
    Or GrossPay, NetPay.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stringstream problem
    By black_spot1984 in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2008, 05:45 PM
  2. stringstream problem
    By black_spot1984 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 04:09 PM
  3. stringstream clear
    By misterowakka in forum C++ Programming
    Replies: 3
    Last Post: 01-01-2008, 01:03 PM
  4. Socket: send stringstream
    By crisis in forum C++ Programming
    Replies: 5
    Last Post: 11-22-2007, 10:50 AM
  5. stringstream
    By jk1998 in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2007, 06:35 PM

Tags for this Thread