Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 19, 2005, 07:31 AM   #1
mmmdreg
macrumors 65816
 
mmmdreg's Avatar
 
Join Date: Apr 2002
Location: Sydney, Australia
Send a message via AIM to mmmdreg Send a message via MSN to mmmdreg
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++;
		}
	}
The idea is that if there is one blank line, "finish" is incremented. If another, it goes to 2 and the while loop ends. If anything else is entered, it should reset to 0.

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.
mmmdreg is offline   0 Reply With Quote
Old Mar 19, 2005, 08:29 AM   #2
hcuar
macrumors 65816
 
hcuar's Avatar
 
Join Date: Jul 2004
Location: Dallas
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
hcuar is offline   0 Reply With Quote
Old Mar 19, 2005, 09:11 AM   #3
mmmdreg
Thread Starter
macrumors 65816
 
mmmdreg's Avatar
 
Join Date: Apr 2002
Location: Sydney, Australia
Send a message via AIM to mmmdreg Send a message via MSN to mmmdreg
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.
mmmdreg is offline   0 Reply With Quote
Old Mar 19, 2005, 10:20 AM   #4
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
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.
gekko513 is offline   0 Reply With Quote
Old Mar 19, 2005, 10:34 AM   #5
mmmdreg
Thread Starter
macrumors 65816
 
mmmdreg's Avatar
 
Join Date: Apr 2002
Location: Sydney, Australia
Send a message via AIM to mmmdreg Send a message via MSN to mmmdreg
Quote:
Originally Posted by gekko513
try this

if (tempInput != '\n' && tempInput != '\r') {
...
} else if (tempInput == '\n') {
...
}
thanks for your idea. Unfortunately it didn't work =(
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
mmmdreg is offline   0 Reply With Quote
Old Mar 20, 2005, 03:04 AM   #6
mmmdreg
Thread Starter
macrumors 65816
 
mmmdreg's Avatar
 
Join Date: Apr 2002
Location: Sydney, Australia
Send a message via AIM to mmmdreg Send a message via MSN to mmmdreg
The problem turns out to be an xcode bug. Compiling and running the executable from terminal.app makes it run fine.
mmmdreg is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB 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
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.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC