| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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();
}
|
|
#2
|
|||
|
|||
|
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 ![]() |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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];
}
|
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
Oh Good. I was worried.
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > cin.getline help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|