| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
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? |
|
#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? |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > String input |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|