C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 22nd, 2006, 12:53 AM
xreddawg909x xreddawg909x is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 91 xreddawg909x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 27 m 20 sec
Reputation Power: 4
String input

def.cpp
#include "booktype.h"



void bookType::setbookTITLE(string titl)
{
cout<<"Please Enter Title: ";
cin.ignore(40,'\n');
titl=cin.get();
bookTitle=titl;
}
void bookType::setPUBLISHER(string pub)
{
cout<<"Please Enter Publisher: ";
cin.ignore(40,'\n');
pub=cin.get();
Publisher=pub;
}
void bookType::setISBN(string ISB)
{
cout<<"Please Enter the IBSN: ";
cin.ignore(40,'\n');
ISB=cin.get();
ISBN=ISB;
}
void bookType::setAUTHORS(string auth1, string auth2, string auth3, string auth4)
{

cout<<"Please enter first author: ";

cin>>auth1;
cout<<endl;

cout<<"Please enter second author: ";
cin>>auth2;
cout<<endl;

cout<<"Please enter third author: ";
cin>>auth3;
cout<<endl;



cout<<"Please enter fourth author: ";

cin>>auth4;

author1=auth1;
author2=auth2;
author3=auth3;
author4=auth4;

}
void bookType::setPRICE(float pri)
{
cout<<"Please Enter the price of the book: ";
cin>>pri;
Price=pri;
}
void bookType::setCOPIES(int cop)
{
cout<<"Please Enter how many copies there are: ";
cin>>cop;
Copies=cop;
}




string bookType::getbookTITLE() const
{
return bookTitle;
}

string bookType::getPublisher() const
{
return Publisher;
}

string bookType::getISBN() const
{
return ISBN;
}

string bookType::getAuthors() const
{
return author1 +", "+author2 + ", " + author3 + ", "+author4;
}

float bookType::getPrice() const
{
return Price;
}

int bookType::getCopies() const
{
return Copies;
}

void bookType:rint() const
{
cout<<"Book Title:"<<bookTitle<<" "<<"Author 1:"<<author1<<" "<<"Author 2:"<<author2<<endl;
cout<<"Author 3:"<<author3<<" "<<"Author4:"<<author4<<endl;
cout<<"Publisher:"<<Publisher<<" "<<"ISBN:"<<ISBN<<endl;
cout<<"Price of Book:"<<"$"<<setprecision(2)<<fixed<<Price<<" "<<"Number of Copies Available:"<<Copies<<endl;
cout<<"\n\n\n\n";
}


i'm having trouble getting the string input to accept spaces inbetween, i.e. title should be "moby ****" etc.. etc.. i've tried cin.ignore(256,'\'), it works until it asks for auth 3 and 4, and publisher and after asking fot all the strings it just keeps looping infinately

main.cpp
#include "booktype.cpp"


int main()
{
string t,p,aOne,aTwo,aThree,aFour,I;
float pr;
int cpy;
bookType x[100];

for(int j=0;j<99;j++)
{
x[j].setbookTITLE(t);
x[j].setPUBLISHER(p);
x[j].setAUTHORS(aOne,aTwo,aThree,aFour);
x[j].setISBN(I);
x[j].setPRICE(pr);
x[j].setCOPIES(cpy);
x[j].print();
}



return 0;
}
any advice?

Reply With Quote
  #2  
Old February 22nd, 2006, 01:19 AM
Icon's Avatar
Icon Icon is online now
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 739 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 3 Days 12 h 30 m 32 sec
Reputation Power: 4
I haven't run your source code (yet), but can offer a quick hint which may help you. Instead of using the stream operator use
cin.getline(char *buffer, int length)
to get a whole line of text (including spaces) until an enter is given. It's a little bit more of a C then C++ style function but you can wrap it up in a string class object easily.
Hope this helps.

Reply With Quote
  #3  
Old February 22nd, 2006, 05:08 PM
xreddawg909x xreddawg909x is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 91 xreddawg909x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 27 m 20 sec
Reputation Power: 4
ok .. i made variables

char hold[30];
cin.getline(hold, 30);

only two problems with this, for empty fields, constructor default values aren't assigned strings and after the first time around

x[j].title input is skipped and it goes straight to publisher input, any suggestions?

Reply With Quote
  #4  
Old February 22nd, 2006, 05:23 PM
xreddawg909x xreddawg909x is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2005
Posts: 91 xreddawg909x User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 27 m 20 sec
Reputation Power: 4
ok .. i made character arrays
char hold[30];
cin.getline(hold, 30);
stringvariable = hold;

privatedatamember = stringvariable;

only two problems with this, constructor default for empty values aren't assigned for strings and

after the first time around x[j+1].title input is skipped and it goes straight to publisher input, any suggestions?

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > String input


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT