|
|
#1 |
|
c++ help!
Hey this is really basic and I don't see why I can't do it but hey. I want something that will read in input until a blank line is entered (ie return is pressed twice).
Code:
string inputText = "";
int finish = 0;
char tempInput;
cout << "Type the text. To end, type a blank line.\n";
while (finish < 2) {
tempInput = getchar();
if (tempInput != '\n') {
finish = 0;
inputText = inputText + tempInput;
}
else //if (tempInput == '\n')
{
finish++;
}
}
Initially, I was trying to use getline but I don't think you can use two characters as thing that terminates it (ie getline(a, b, "2x \n in here)). Apart from that, it worked perfectly. So any help would be appreciated. |
|
|
|
0
|
|
|
#2 |
|
I would guess that there is a hidden character that you aren't expecting being inserted before the '\n' character. If I remember correctly, it's probably the line feed character. To do text input, I'd recommend using cin anyway. You're already using cout, why not stick to C++ streams.
![]() Make sure to declare enough memory for your input as well. As defined, you're only allowing 1 character. You code above could actually go out of bounds pretty easily.
__________________
iMac 27" i5 Mid 2011 | 1TB HD | 16GB RAM iPhone 4 32 Gig |
|
|
|
0
|
|
|
#3 |
|
I thought you can't input a whole lot of stuff using cin.. I've only been doing this for a bit so correct me if I'm wrong.
Last edited by mmmdreg; Mar 19, 2005 at 10:29 AM. |
|
|
|
0
|
|
|
#4 |
|
try this
if (tempInput != '\n' && tempInput != '\r') { ... } else if (tempInput == '\n') { ... } Return sometimes triggers two characters \r\n or \n\r depending on platform. You can use cin to read a lot of stuff including strings and numbers, but cin will just skip whitespace ( \n\r\f\t) so it will not be possible to detect a double return using cin. I don't think the code will go out of bounds because it uses the string class, but I haven't really used the string class, so I'm not 100% sure.
__________________
Someone get Nekkid™, now! Last edited by gekko513; Mar 19, 2005 at 10:24 AM. |
|
|
|
0
|
|
|
#5 | |
|
Quote:
I don't know how to debug properly but from test cout things, it seems that the program keeps going through "finish = 0" and so well keep on looping..even when using both \n and \r.. I tried using getline just then, and the result was the same as the above example with the "finish = 0" commented out. Pretty much, it works if two newlines are inputted together in the first instance of a newline.. but it doesn't work if i type something, newline, type something, then do two newlines.. ie: this works: blah blah blah \n\n but this doesn't: blah blah blah \n blah \n\n |
||
|
|
0
|
|
|
#6 |
|
The problem turns out to be an xcode bug. Compiling and running the executable from terminal.app makes it run fine.
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| iPod nano playback problem? | potbelly1010 | iPod | 1 | Dec 17, 2011 10:14 PM |
| I figuratively punched a hole through my Mac - Help! | motulist | Mac Basics and Help | 4 | Nov 30, 2011 12:58 AM |
| Apple TV: Help me understand tethered jb | spartanrob | Jailbreaks and iOS Hacks | 7 | Nov 21, 2011 12:47 PM |
| IOS 5.0.1 jailbreak help | chantelle310 | Jailbreaks and iOS Hacks | 2 | Nov 19, 2011 11:28 PM |
| Can anyone please help me with my Mail issue? | jbrown | iCloud and Apple services | 0 | Nov 18, 2011 03:51 PM |
All times are GMT -5. The time now is 01:22 AM.









Linear Mode

