Go Back   Hardware Forums > Software Support Forums > Web Development

Reply
 
LinkBack Thread Tools
Old 17-11-2006, 09:54 PM   #1 (permalink) Top
out of date
 
zeus's Avatar
 
Join Date: May 2003
Age: 27 Male
Posts: 1,654
Times Helpful: 105
My Mood: Twisted
Status: Offline

My Computer

Default stringstream

Ive been following this tutorial but dont really get the point made in the stringstream section.....
Basic Input/Output

Am I right in thinking all the code is doing (right at the bottom of the page) is saving the getline variable as a string called mystr then converting it with stringstream into a numerical variable called price.

Then the same for quantity.

Why not just save the numbers as variables and multiply those. I dont get the point in saving it as a string and then turning it into a number.

But maybe ive got it all wrong!

  Reply With Quote
Whats this? Ultra Flat Keyboard
Ultra Flat Keyboard
Seller Price (inc. VAT) Delivery Total Price Availability Seller Rating
Dell £18.98 Free £18.98 In Stock Rated: 0 out of 5 - Number of votes: 0
Misco.co.uk £10.33 £4.69 £15.02 In Stock Rated: 4 out of 5 - Number of votes: 1355
Dell Business £9.15 £8.23 £17.38 In Stock Rated: 0 out of 5 - Number of votes:
Old 17-11-2006, 10:19 PM   #2 (permalink) Top
The King

 
Addis's Avatar
 
Join Date: Jan 2004
Age: 18 Male
Posts: 5,255
Times Helpful: 403
My Mood: Drunk
Status: Offline

My Computer

I'm not exactly sure why you wouldn't just accept integer only input, instead of messing around with stringstream, but it does have a purpose when a user specified string can be safely and easily converted to another type.

You'll probably find uses for it later on. Sometimes I use a traditional string with a char array, and then get user input in C++ by using the cin.getline() function.
Code:
#include <iostream>
using namespace std;

int main()
{
char str[200];
cin.getline(str, 200);
cout << str << endl;
return 0;
}
But its up to you how you do it. Sometimes its helpful to have C++ classes like stringstream to do some of the heavy work of checking bounds, and keeping everything safe.
__________________
Never trust a program you don't have the source code for.

My website | Powerful Desktop Linux | Linux for human beings | Linux for power users | Linux for ricers
Send a message via MSN to Addis   Reply With Quote
Old 18-11-2006, 12:17 PM   #3 (permalink) Top
out of date
 
zeus's Avatar
 
Join Date: May 2003
Age: 27 Male
Posts: 1,654
Times Helpful: 105
My Mood: Twisted
Status: Offline

My Computer

Thanks for the reply Addis. I'll keep working through the tutorial and see how it goes.

Ive got another problem though. Ive only just tried to compile some code (In dev-C++) but I think im doing something wrong.

Whenever I run my compiled code my DOS window closes just when I expect to see the final output.

ie
Code:
// stringstreams
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
  string mystr;
  float price=0;
  int quantity=0;

  cout << "Enter price: ";
  getline (cin,mystr);
  stringstream(mystr) >> price;
  cout << "Enter quantity: ";
  getline (cin,mystr);
  stringstream(mystr) >> quantity;
  cout << "Total price: " << price*quantity << endl;
  return 0;
}
I enter the price, then the quantity. Press enter and it closes.

When I open Dev-C++ I just go to file, new, source file. Is this correct or is this where im going wrong?
  Reply With Quote
Old 18-11-2006, 12:34 PM   #4 (permalink) Top
out of date
 
zeus's Avatar
 
Join Date: May 2003
Age: 27 Male
Posts: 1,654
Times Helpful: 105
My Mood: Twisted
Status: Offline

My Computer

It doesnt matter, I just directed DOS to my .exe It stays open now.
  Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +1. The time now is 02:44 AM.


Copyright © 2000 - 2008 · HARDWAREFORUMS.COM · All rights reserved