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 August 24th, 2005, 06:19 AM
Inspectah Inspectah is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 4 Inspectah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 57 sec
Reputation Power: 0
Question cin.getline help

Hi.

I'm in the process of teaching myself C++, so you might get a few questions from me

I'm currently writing my own String class for a program I'm making and I'm having trouble with the getline function.

With the code below I can enter one word and input that into a string object, but I also want to enter multiple words into one string object. It seams the getline function won't read the whole line. Any Ideas?

Code:
#include <vector>
#include <iostream>
using namespace std;

class cString
{
     public:
            cString(){}
            ~cString(){}
            void Create(char[]);
            void Show() const;
     private:
             vector<char> v;
             int length;
};

void cString::Create(char array[])
{
     int i;
     for (i=0; i<=(sizeof(array)/sizeof(char)); i++)
         v.push_back(array[i]);
     length = i;
}

void cString::Show() const
{
     for (int i=0; i<length; i++)
         cout << v[i];
}

int main()
{
    cString string;
    {
     char str[100];
     cout << "Enter string: ";
     cin.getline(str,100);
     string.Create(str);
    }
    string.Show();
}

Reply With Quote
  #2  
Old August 24th, 2005, 04:33 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
wow finally i can help yes finally i feel help ful well before i say ur answer i am goin to ask u this is the thw whole code?

and i want to know the purpose of this program(if u don't want to don't tell me)

well heres your problem your what you call string is not a string its a char
i really don't know what char stands for but my guess is (character) and character is only one part and to use a string u got to add the file string
#include <string>

and when u want to make a string u do like this

string the_entered_string;

so then when u want to add the string u only have to write
the_entered_string

but i know what u want to do

when u do this line

cin.getline (str, 100);

u forgot an important part the part which i was thought is the printing process
it should go like this
cin.getline (str, 100, '\n')
so that what they write or what u write is now printable (not printable like to print in a paper but in the program u can see it)

well i hope that helps
p.s. just want to say it can go more than 100 so u can make the string more than 100 characters

well i hope that info helped

Reply With Quote
  #3  
Old August 25th, 2005, 04:48 AM
Inspectah Inspectah is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 4 Inspectah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 57 sec
Reputation Power: 0
Hi bloodlust.

I know I could use the string.h header but I want to make my own string class. The std string class is just an array of char anyway.

I'm writing an organiser that has a schedule, address book and some sort of daily journal. Its just a basic dos program I'm doing for practice.

Here is the whole code (so far) so you can see where I am using my string class. I haven't written the driver program yet only the classes.

Code:
#include "cLinkedList.cpp"
#include <vector>
#include <iostream>
using namespace std;

class cString
{
     public:
            cString(){}
            ~cString(){}
            void Create(char[]);
            void Show() const;
     private:
             vector<char> v;
             int length;
};

void cString::Create(char array[])
{
     int i;
     for (i=0; i<=(sizeof(array)/sizeof(char)); i++)
         v.push_back(array[i]);
     length = i;
}

void cString::Show() const
{
     for (int i=0; i<length; i++)
         cout << v[i];
}

class cTime
{};


class cStore
{
      public:
             cStore(cString* file):fileName(file){}
             ~cStore(){delete fileName;}
      protected:
                virtual void Save(){}
                virtual void Open(){}
                cString* GetFileName() const {return fileName;}
      private:
              cString* fileName;
};

class cOrganiser : public cStore
{
      public:
             cOrganiser(cString* user, cString* file, cTime day,    cTime month, cTime year)
             :userName(user), cStore(file), currentDay(day), currentMonth(month), currentYear(year){}

             cOrganiser(cOrganiser* pObject)
             :cStore(pObject->GetFileName()), currentDay(pObject->currentDay),
             currentMonth(pObject->currentMonth), currentYear(pObject->currentYear){}
             
             ~cOrganiser(){delete userName;}
             
             cTime GetCurrentDay() const {return currentDay;}
             cTime GetCurrentMonth() const {return currentMonth;}
             cTime GetCurrentYear() const {return currentYear;}
             
      private:
              cTime currentDay;
              cTime currentMonth;
              cTime currentYear;
              cString* userName;
};

class cSchedule : public cOrganiser
{
      public:
             cSchedule(cOrganiser* pObject, cTime day, cTime month, cTime year)
             :cOrganiser(pObject), itsDay(day), itsMonth(month), itsYear(year){}
             
             cSchedule(cSchedule* pObject, cOrganiser* pBaseObject)
             :cOrganiser(pBaseObject), 
             itsDay(pObject->itsDay), itsMonth(pObject->itsMonth), itsYear(pObject->itsYear){}
             
             ~cSchedule(){}
             
             cTime GetDay() const {return itsDay;}
             cTime GetMonth() const {return itsMonth;}
             cTime GetYear( )const {return itsYear;}
      protected:
                cTime itsDay;
                cTime itsMonth;
                cTime itsYear;
};

class cScheduleEntry : public cSchedule
{
      public:
             cScheduleEntry(cOrganiser* pBaseObject, cSchedule* pObject, cTime from, cTime to, cString* pData)
             :cSchedule(pObject, pBaseObject), timeFrom(from), timeTo(to), entry(pData){}
             
             ~cScheduleEntry(){delete entry;}
             
             cTime GetTimeFrom() const {return timeFrom;}
             cTime GetTimeTo() const {return timeTo;}
      private:
              cTime timeFrom;
              cTime timeTo;
              cString* entry;
};


int main()
{
    cString string;
    {
     char str[100];
     cout << "Enter string: ";
     cin.getline(str,100);
     string.Create(str);
    }
    string.Show();
    cin.get();
}


I tried what you suggested but it didn't work. Perhaps I need to clear the buffer. I think I read that somewhere. Not sure how to do that though.

Reply With Quote
  #4  
Old August 25th, 2005, 03:39 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
Inspectah
Well here goes more questions.
First what compiler are you using?
Do you have the cLinkedlist.cpp and if its so important for the program show it to me, but if it is not do not post.
Also I copy source code and ran it and i see what you mean any only one word shows, that is what you want to fix right?
Cause when I enter a string only 5 characters show.
Also I was lookning at source code, and I have to say you might be more advance than in C++ cause I do not know what this part is for
Code:
 void cString::Create(char array[])
 {
 	 int i;
 	 for (i=0; i<=(sizeof(array)/sizeof(char)); i++)
 		 v.push_back(array[i]);
 	 length = i;
 }
 


I know its in the public section and I think I know why is it there, is it so when some one puts the string that will be it?
Also I do not know whats with the int i, from all the programs I wrote I have also never used the protected function but my guess its to protected in whatever is inside of that section.
But I do understand the other stuff which is get the information of the date and time which I may say thanks cause I did not knew that could have been possibly and I will be using this code to make my programs keep track of time and date.
But as in for your question the only thing related to your question for buffer problems is this
[code]
fseek(stdin, 0L, SEEK_END);
[code]
some guy here showed me that, also do you know about the strcmp thingy cause last time I wrote a string program I used but I deleted the program the same day you I saw this, cause I also used the cin.getline and then after I did the strcmp.
Well I know this is not good information but I hope so finish your program.

Reply With Quote
  #5  
Old August 25th, 2005, 03:42 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
oh yeah and my guess the problem is somewhere in these classes
Code:
 void cString::Create(char array[])
 {
 	 int i;
 	 for (i=0; i<=(sizeof(array)/sizeof(char)); i++)
 		 v.push_back(array[i]);
 	 length = i;
 }
 
 void cString::Show() const
 {
 	 for (int i=0; i<length; i++)
 		 cout << v[i];
 }
 

Reply With Quote
  #6  
Old August 25th, 2005, 04:48 PM
Inspectah Inspectah is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 4 Inspectah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 57 sec
Reputation Power: 0
Thanks for your replys bloodlust. You said that it only displayed 5 characters which I didn't pick up on since I was testing it with the word 'Hello'.

Anyway I fixed it now, here's what I got now.

Code:
class cString
{
     public:
            cString(){}
            ~cString(){}
            void Create(char[], int);
            void Show() const;
     private:
             vector<char> v;
             int length;
};

void cString::Create(char array[], int len)
{
     int i;
     for (i=0; i<len; i++)
         v.push_back(array[i]);
     length = len;
}

void cString::Show() const
{
     for (int i=0; i<length; i++)
         cout << v[i];
}

int main()
{
    cString string;
    {
     char str[100];
     cout << "Enter string: ";
     cin.getline(str,99);
     int i;
     for (i=0; str[i]!=NULL; i++){;}
     string.Create(str,i);
    }
    string.Show();
}


It works really well now. And btw that cTime class is something I'm creating, I haven't even written it yet. Just put it there so you can see what I was refering too. Don't bother using it cause it won't work.

The cLinkedList.cpp is a template class that I made for storing data. Didn't mean to put that in. Just ignore it.

You said you don't know what 'int i' is. Thats pretty basic, just used for declaring a variable. And protected allows for derived classes to access member functions.

I appreciate your help, not sure if I would of noticed it only displaying 5 characters.

Reply With Quote
  #7  
Old August 25th, 2005, 08:52 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
to make things short i know what int i is but i do not know you you used an integer thats what i was asking for y were u using an integer then i found out y, cause of the length, or thats what i assume.
lol of course i know what int i means i just did not know y u had an int to be doing the job

Reply With Quote
  #8  
Old August 25th, 2005, 09:15 PM
Inspectah Inspectah is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Posts: 4 Inspectah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 40 m 57 sec
Reputation Power: 0
Oh Good. I was worried.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > cin.getline help


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

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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





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