![]() |
|
#1
|
|||
|
|||
Strings tripping me up once moreIt's been a while. The past month or two I've managed to get my programs done on my own fairly quickly. We stopped messing with unfamiliar code and went into logical issues. Binary trees and so on. Logic problems never bugged me.
But, once again for one of my programs I need to use Strings in a way that the current implementation from our previous labs do not seem to support. Basically I have a structure of information, First name, last name, etc. And I want to use a string to take in a street address (program is an address book). C style strings work fine for everything else, but when attempting to input to a character array as I did with the others, the spaces in the address (i.e. "555 Fake Street") are cutting off later inputs. That or filling them in, detecting the spaces as Null and considering it a different string. No requirement on which type of string to you, but my browsing and tinkering has come up empty. Strings always seem to be my weakness, I never got any grounding in them. I did a search, couldn't find one, but if there's a basic thread that will get me set could I get directed to it? Tried to find one, failed. And if not, any responses are appreciated. Much appreciated. |
|
#2
|
||||
|
||||
Re: Strings tripping me up once moreYou can use cin.getline to read strings with spaces.
For example, consider this sample program: Using character arrays: CPP / C++ / C Code:
But usage of character arrays is not recommended in C++, we can switch to string. Here is a sample code: CPP / C++ / C Code:
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. |
|
#3
|
||||
|
||||
Re: Strings tripping me up once moreSince you didn't post code, all we can assume.
You seem to be using C with scanf(), or C++ with cin. Read this info. If you're using cin the information is the same. While you're at it, read the Guidelines too. __________________
Age is unimportant -- except in cheese |
|
#4
|
|||
|
|||
Re: Strings tripping me up once moreI originally thought I just didn't understand the basics of strings, why I didn't post my code. But it finally came to me to copy the code taking in the string from my program into it's own small one and run it. Since it works there, but not in this one, something else must be going on. The problem is in my add and update options. Look to the runmenu method near the bottom of the program.
On a side note, I know I've been told before that the .h at the end of includes is archaic and to switch, but for the compiler I've been using I don't seem to have a choice. Says it can't find them otherwise. I did switch the void main we were given to int main. Sorry if the spacing is off. CPP / C++ / C Code:
|
|
#5
|
||||
|
||||
Re: Strings tripping me up once moreQuote:
I see you're also using cin to read a single character. Use cin.getline() here too. When you read one character the ENTER is left in the buffer for the next cin. You should probably use cin.getline() for all inputs. If this isn't the problem, be specific. Details are important. Also, looking at the function you mention: CPP / C++ / C Code:
CPP / C++ / C Code:
Quote:
Check out Borland as one possibility, a lot of people here use Bloodshed DevC. Technically, as long as you are programming in Standard C/C++ any compiler should work. If yours is too old, it should be replaces so you can use Standard C/C++. Quote:
__________________
Age is unimportant -- except in cheese |
|
#6
|
|||
|
|||
Re: Strings tripping me up once moreYeah, I had the same thoughts about that method. But we're given basic frameworks to use for a lot of his programs, sort of the "In the real world you have to work with other people's usually flawed code" deal. So the basic structure, method arguments, etc are to be left alone according to our instructions.
But yes, converting all my cins to getlines did the trick. It just took a little tinkering to get it going right. Unfamiliar code for me. Have one odd bug where my menu repeats an extra time using certain options, but I can get that. Just a logic problem. |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Declaring an array of strings dynamically (C Language) | Hyuga | C Programming Language | 5 | 03-Aug-2005 09:18 |
| need help - questions about strings | Benayoun | C Programming Language | 6 | 24-Jan-2005 02:15 |
| array of pointers to strings | mirizar | C++ Forum | 5 | 21-Jan-2005 10:24 |
| C++ style strings and STL | dexter | C++ Forum | 14 | 04-Jan-2005 07:46 |
| I am reviewing Arrays and need help converting some strings to arrays | jenmaz | C Programming Language | 22 | 22-Nov-2004 23:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The