Changing the exit of my program.

Changing from exit to the enter key.

Page 1 of 1

5 Replies - 971 Views - Last Post: 30 October 2007 - 02:01 PM Rate Topic: -----

#1 gator6688  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 23-September 07

Changing the exit of my program.

Post icon  Posted 29 October 2007 - 07:29 AM

I want to change this program so that it ends when I hit the enter key instead of typing Exit. How could I go about doing that?

int main()
{
	 string str;
	 int count=0;

	 cout << "Enter the string: ";
	 while(str!="Exit")
	 {count++;
	 getline(cin, str);
	 }
	 cout << "The number of lines entered is: " <<count<<endl;

	 return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Changing the exit of my program.

#2 MorphiusFaydal  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 43
  • View blog
  • Posts: 1,376
  • Joined: 12-May 05

Re: Changing the exit of my program.

Posted 29 October 2007 - 09:27 AM

Try...
while ( str != "\n" ) {
	// code
}


Was This Post Helpful? 0
  • +
  • -

#3 gator6688  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 23-September 07

Re: Changing the exit of my program.

Posted 30 October 2007 - 01:16 PM

I tried "\n" and it did not work. Any other suggestions?
Was This Post Helpful? 0
  • +
  • -

#4 Hyper_Eye  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: Changing the exit of my program.

Posted 30 October 2007 - 01:26 PM

A carriage return would be a '\r'. You might want to check for that. If the only input is the enter key then you could use a single 'char' instead and then test to see if it is equal to 13 which is the decimal representation of an ASCII carriage return.

Also realize that '\r' and '\n' is a single character not two.

This post has been edited by Hyper_Eye: 30 October 2007 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 gator6688  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 84
  • Joined: 23-September 07

Re: Changing the exit of my program.

Posted 30 October 2007 - 01:32 PM

Well I am not sure what all that carriage return stuff is but I need to type in a string that is ended when I hit the enter key. Then I need it to count the number of lines. I have everything set up and it works right except I cannot make it end with the enter key.
Was This Post Helpful? 0
  • +
  • -

#6 jjhaag  Icon User is offline

  • me editor am smartastic
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,789
  • Joined: 18-September 07

Re: Changing the exit of my program.

Posted 30 October 2007 - 02:01 PM

Unfortunately, getline discards the character used as a delimiter - which defaults to '\n' when one is not specifically provided like this getline ( cin, str, '\n');. So just hitting enter will not work, because getline tosses out the '\n' at the end of the string, even if it is the only character that has been read.

You may want to try something like this:
#include <iostream>

using namespace std;

int main() {
	string str;
	int count=0;
	cout << "Enter the string: ";
	do {
		getline(cin, str);
		count++;
	} while (!str.empty());

	count--;
	cout << "The number of lines entered is: "<<count<<endl;

	return 0;
}


This code works as follows. The input is received in a do-while loop. When the user types in a string and hits enter, the line is counted. It then tests whether the string is empty using the empty() method, which returns true if the string has no characters in it. If the user typed something before hitting enter, the string will not be empty, so the loop continues. However, when the user just hits enter, the '\n' is discarded, so the string is now empty, and the loop exits. The counter will still increment if the user just hits enter, so there is a decrement step included after the loop, to get rid of this extra line.

And the above won't work using just a while loop, since the string starts out empty - the test condition of the while loop would be false, so it would never execute.

Hope that helps,

-jjh
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1