Homework Help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 4
Reputation: blsnls is an unknown quantity at this point 
Solved Threads: 0
blsnls blsnls is offline Offline
Newbie Poster

Homework Help

 
0
  #1
Aug 30th, 2009
My problem with the program listed below is when the question asked for the title of the book and you type in more than one word with white spaces inbetween the words like "History of Me" it will skip the next question. Any help will be appreciated.

#pragma hdrstop
#pragma argsused
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
char date[8];
int quantity;
char isbn[13];
char title [20];
float bookprice = 0.00;


//Sales slip information to be entered by user

cout << "Please enter the date in mm/dd/yy format:\n" ;
cin >> date;
cout << "Please enter the quantity being purchased:\n" ;
cin >> quantity;
cout << "Please enter the ISBN number for the book:\n" ;
cin >> isbn;
cout << "Please enter the title of the book:\n" ;
cin >> title;
cout << "Please enter the price of the book:\n" ;
cin >> bookprice;
system("pause");
return 0;
}
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Homework Help

 
0
  #2
Aug 30th, 2009
Did you see this on your way in?
Originally Posted by forum title page
Announcement: Please use BB Code and Inlinecode tags
cscgal (The Queen of DaniWeb)
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

Re: Homework Help

 
0
  #3
Aug 30th, 2009
This behavior is expected, because the operator >> of cin reads input in his stream until a end-of-line or a whitespace is found. So, the next operator>> call finds remaining input (the other words after the whitespace) in the cin buffer and takes the data. If you want to prevent this situation, you need to truncate the input to the first whitespace or the end-of-line character. You can do it by doing a fscanf call like this:

fscanf(stdin, %s\n, your_string_variable);

This is a C-style way to do it and I would like to see other ways, C++ oriented, to prevent a whitespace in the input.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,359
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 172
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Homework Help

 
0
  #4
Aug 30th, 2009
cin does not read white spaces.
use cin.getLine(...), or cin.get(...) or getline(...); They don't skip spaces.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC