GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Nov-2005, 06:23
autome2005 autome2005 is offline
New Member
 
Join Date: Oct 2005
Posts: 5
autome2005 is on a distinguished road

struct problem


hi,

i have to manipulate two strings and i have to use a structure. The problem that i am having is that i can't read the second string. I don't know what is wrong with this code. Can anyone help.

thanks.

here is a sample of my code.

CPP / C++ / C Code:
#include <iostream>
#include <string.h>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <iomanip>

using namespace std;

struct text {
       char *strLine;
       int strLength;
       void readString();
};

void text::readString(){
     char textTemp[100];
     
     cout << "\n\nenter string: ";
     cin.getline(textTemp, 100);
     cout << "\nenter length: ";
     cin >> strLength;
     
     strLine = new char[strLength + 1];
     strLine[0] = 0;
     strcpy(strLine, textTemp);
}

void display(text & text){
     cout << "\nYour string: " << text.strLine << endl;
}


int main(){
    text myString, myString2;
    
    myString.readString();
    display(myString); 
    myString2.readString();
    display(myString2);
    
    delete [] myString.strLine;
    delete [] myString2.strLine;
    
    return 0;
}
Last edited by LuciWiz : 07-Nov-2005 at 06:45. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 07-Nov-2005, 06:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,648
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: struct problem


Quote:
Originally Posted by autome2005
hi,

i have to manipulate two strings and i have to use a structure. The problem that i am having is that i can't read the second string.

getline() reads chars from the stream buffer up, to but not including, the terminating character. The terminating character is a newline. So the first call to getline reads your first string but leaves the '\n' in the stream buffer.

The next call for input (with "cin >>" ) is for a number. Now, for reading numbers with >>, leading whitespace is ignored, so the program gets the newline from the stream buffer and ignores it and waits until it sees something numerical in the stream buffer. In other words it waits for user input.

When the user enters a number, the >> operator gets the numerical digits from the stream buffer and stops taking items from the stream buffer when it sees something that is not part of a number (the newline in this case). In other words the newline is still in the stream buffer.

Now for your second string: the getline() sees the newline left over from the last "cin>>" and returns with no characters and leaves the newline in the stream buffer. The program continues and gets the number from the user. That's why string 2 is always empty.

One easy way to make sure the '\n' is cleared after reading the number is to put something like something like this in your readString() function:

CPP / C++ / C Code:
void text::readString()
{
     char textTemp[100];
     
     cout << "\n\nenter string: ";
     cin.getline(textTemp, 100);
     cout << "\nenter length: ";
     cin >> strLength;

     cin.ignore(100, '\n'); // clear newline from buffer for next getline()
     
     strLine = new char[strLength + 1];
     strLine[0] = 0;
     strcpy(strLine, textTemp);
}

Actually, this is not perfect, but it's a way to get started.

A better way is to use something like this:
CPP / C++ / C Code:
     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

You have to #include <limits.h> for this.

This and other interesting things can be found here: Parashift C++ FAQ


Regards,

Dave
Last edited by davekw7x : 07-Nov-2005 at 08:26.
  #3  
Old 07-Nov-2005, 07:08
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: struct problem


Hi autome,

There is no need to get the length of the string from the user.
You can calculate it by yourself.

There are two ways.
One is by using the strlen function.
The other way is to calculate the length by using a loop.

Using strlen function:
CPP / C++ / C Code:
    char *strLine = "Hello there";
    int strLength;
 
    strLength = strlen(strLine);
    
    cout << "The length of " << strLine << " is " << strLength << endl;

The other way is to calculate by using a loop.
here is a sample code:
CPP / C++ / C Code:
    char *strLine = "Hello there";
    int strLength = 0;
 
    for( ; strLine[ strLength ] ; strLength++ )
    ;
    
    cout<<"The length of "<<strLine<<" is "<<strLength<<endl;

Now, on to the program:

First, #include <string.h> must be replaced by
CPP / C++ / C Code:
#include <cstring>
because in C++, string.h is a depreciated header file.

Then, instead of getting the length from the user, you can modify the readString function like this:
CPP / C++ / C Code:
void text::readString()
{
     char textTemp[100];

     cout << "\n\nenter string: ";
     cin.getline(textTemp, 100);

     strLength = strlen(textTemp);

     strLine = new char[strLength + 1];
     strcpy(strLine, textTemp);

}

and you can modify your display function like this:
CPP / C++ / C Code:
void display(text mystring)
{
     cout << "\nYour string: " << mystring.strLine << endl;
}
because there is no need for &
and dont use variable names same as the struct name. So, vary your name like mystring.

One more way to improve your program:
Instead of havind display as a separate function, you can have a function in the struct itself.
so, what we have to do now add a function in the text declaration ,like this:
CPP / C++ / C Code:
struct text
{
       char *strLine;        //string
       int strLength;        //string length
       void readString();    //function to read the string
       void display();       //function to display the string 
};
and make the display function like this:
CPP / C++ / C Code:
void text::display()
{
     cout << "\nYour string: " << strLine << endl;
}

Summarising the program is:
CPP / C++ / C Code:
#include <iostream>
#include <cstring>

using namespace std;

struct text
{
       char *strLine;        //string
       int strLength;        //string length
       void readString();    //function to read the string
       void display();       //function to display the string 
};

void text::readString()
{
     char textTemp[100];

     cout << "\n\nenter string: ";
     cin.getline(textTemp, 100);

     strLength = strlen(textTemp);

     strLine = new char[strLength + 1];
     strcpy(strLine, textTemp);
}

void text::display()
{
     cout << "\nYour string: " << strLine << endl;
}


int main()
{
    text myString, myString2;     //two text objects

    myString.readString();        //get the input
    myString.display();           //display the string
    myString2.readString();
    myString2.display();

    delete [] myString.strLine;
    delete [] myString2.strLine;

    return 0;
}


Please insert your c++ code between [noparse][c++] and [/c++] [noparse] tags.
Dont forget to read the [url="http://www.gidforums.com/t-5566.html"]Guidelines[/url].
Thank you.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #4  
Old 07-Nov-2005, 08:11
autome2005 autome2005 is offline
New Member
 
Join Date: Oct 2005
Posts: 5
autome2005 is on a distinguished road

Re: struct problem


thank You Dave once again You saved my life.
  #5  
Old 07-Nov-2005, 08:14
autome2005 autome2005 is offline
New Member
 
Join Date: Oct 2005
Posts: 5
autome2005 is on a distinguished road

Re: struct problem


thank You Dave once again You saved my life.

and thanks Paramesh for your comments, yes i know about the length functions but in my project i have to ask a user for sting length.

automne2005
 
 

Recent GIDBlogA Week in Kuwait by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2D arrays:dynamic allocation and freeing bravetanveer C Programming Language 48 27-Nov-2007 15:55
Urgent ! Pls Help Me ! mycashmoney C Programming Language 4 01-Jul-2006 22:49
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41
Problem with typedef struct, cannot malloc(sizeof(struct element)); unless at the top Danny C Programming Language 2 11-Sep-2005 01:56
typedef struct problem grizli C Programming Language 8 19-Jun-2004 16:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:32.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.