Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,572 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,671 people online right now. Registration is fast and FREE... Join Now!




Changing the exit of my program.

 
Reply to this topicStart new topic

Changing the exit of my program., Changing from exit to the enter key.

gator6688
29 Oct, 2007 - 06:29 AM
Post #1

D.I.C Head
**

Joined: 23 Sep, 2007
Posts: 84


My Contributions
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?

CODE
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;
}

User is offlineProfile CardPM
+Quote Post

MorphiusFaydal
RE: Changing The Exit Of My Program.
29 Oct, 2007 - 08:27 AM
Post #2

D.I.C Lover
Group Icon

Joined: 12 May, 2005
Posts: 1,213



Thanked: 15 times
Expert In: Hardware, Networking

My Contributions
Try...
CODE

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

User is offlineProfile CardPM
+Quote Post

gator6688
RE: Changing The Exit Of My Program.
30 Oct, 2007 - 12:16 PM
Post #3

D.I.C Head
**

Joined: 23 Sep, 2007
Posts: 84


My Contributions
I tried "\n" and it did not work. Any other suggestions?
User is offlineProfile CardPM
+Quote Post

Hyper_Eye
RE: Changing The Exit Of My Program.
30 Oct, 2007 - 12:26 PM
Post #4

D.I.C Head
**

Joined: 13 Sep, 2007
Posts: 72



Thanked: 6 times
My Contributions
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 Oct, 2007 - 12:27 PM
User is offlineProfile CardPM
+Quote Post

gator6688
RE: Changing The Exit Of My Program.
30 Oct, 2007 - 12:32 PM
Post #5

D.I.C Head
**

Joined: 23 Sep, 2007
Posts: 84


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Changing The Exit Of My Program.
30 Oct, 2007 - 01:01 PM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
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:
CODE
#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
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:25PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month