![]() |
|
#1
|
|||
|
|||
struct problemhi,
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:
Last edited by LuciWiz : 07-Nov-2005 at 06:45.
Reason: Please insert your C++ code between [c++] & [/c++] tags
|
|
#2
|
|||
|
|||
Re: struct problemQuote:
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:
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:
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
|
||||
|
||||
Re: struct problemHi 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:
The other way is to calculate by using a loop. here is a sample code: CPP / C++ / C Code:
Now, on to the program: First, #include <string.h> must be replaced by CPP / C++ / C Code:
Then, instead of getting the length from the user, you can modify the readString function like this: CPP / C++ / C Code:
and you can modify your display function like this: CPP / C++ / C Code:
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:
CPP / C++ / C Code:
Summarising the program is: CPP / C++ / C Code:
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
|
|||
|
|||
Re: struct problemthank You Dave once again You saved my life.
|
|
#5
|
|||
|
|||
Re: struct problemthank 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 GIDBlog
A Week in Kuwait by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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