Thread: using user-defined function, do/while loop, to print name

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    using user-defined function, do/while loop, to print name

    Hi

    What's the wrong with the code? When I enter 'n' it exits fine. But when I enter 'y' or any other character the program goes crazy. Please have look at the outputs at the bottom.

    Code:
    // print your name n times
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    void prntname(int dummy1, string dummy2);
    
    int main()
    
    {
        string name; int n; char ch;
    
        do
        {
            cout << "enter your name: ";
            getline (cin, name);
            cout << "how many times you want to print?: "; cin >> n;
            prntname(n, name);
            cout << endl;
            cout << "do you want to repeat?: "; cin >> ch;
        }
        while (ch != 'n');
    
        cout << endl;
    
        system("pause");
    
        return 0;
    }
    
    //---------------------------------------------------------
    // function definition for void prntname(int dummy, string dummy)
    
    void prntname(int dummy1, string dummy2)
    
    {
        for (int j=1; j<=dummy1; j++)
        {
            cout << dummy2 << endl;
        }
    }
    //------------------------------------------------------------
    Output when I enter 'n'. The program exits fine:
    Code:
    enter your name: jackson heights
    how many times you want to print?: 10
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    
    do you want to repeat?: n
    
    Press any key to continue . . .
    Output when I enter 'y' or any other character. The program goes crazy:
    Code:
    enter your name: jackson heights
    how many times you want to print?: 10
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    jackson heights
    
    do you want to repeat?: y
    enter your name: how many times you want to print?:
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    'cin >> ch' has left a newline in the input stream, therefore when 'getline' is called, it terminates immediately.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Sipher View Post
    'cin >> ch' has left a newline in the input stream, therefore when 'getline' is called, it terminates immediately.
    Thanks, Sipher.

    It's the first time I have used getline. I don't know what 'precautions' what needs to take when working with getline. How do I correct that erroneous behavior? Could you please help me? Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jackson6612 View Post
    Thanks, Sipher.

    It's the first time I have used getline. I don't know what 'precautions' what needs to take when working with getline. How do I correct that erroneous behavior? Could you please help me? Thank you.
    It's not getline() that's your issue. It's about datatypes and I/O in general.

    'n' is a char.
    'y' is a char.
    '\n' (the newline) is a char.

    When you use that program, you do not just hit 'n' or 'y'. You hit 'n' or 'y', then ENTER, which is '\n', the newline. The 'n' or 'y' gets put into ch.

    What do you think happens to the '\n'? It doesn't disappear. It's still there, in the stdin buffer. So the next cin >> ch grabs it.
    Last edited by MK27; 05-07-2011 at 05:53 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by MK27 View Post
    It's not getline() that's your issue. It's about datatypes and I/O in general.

    'n' is a char.
    'y' is a char.
    '\n' (the newline) is a char.

    When you use that program, you do not just hit 'n' or 'y'. You hit 'n' or 'y', then ENTER, which is '\n', the newline. The 'n' or 'y' gets put into ch.

    What do you think happens to the '\n'? It doesn't disappear. It's still there, in the stdin buffer. So the next cin >> ch grabs it.
    Thanks, MK27.

    I understand it now. But how do I empty that buffer...? Could you please tell me? Thank you.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    std::cin.flush()
    If I remember correctly. just call flush() at the beginning of your loop.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Thanks. Unfortunately it isn't working. I have used these combinations: std::cin.flush(), cin.flush(), std::cin.clear(), cin.clear(). But none worked. Please help me.


    Code:
    // print your name n times
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    using namespace std;
    
    void prntname(int dummy1, string dummy2);
    
    int main()
    
    {
        string name; int n; char ch;
    
        do
        {
            cin.flush();
            cout << "enter your name: ";
            getline (cin, name);
            cout << "how many times you want to print?: "; cin >> n;
            prntname(n, name);
            cout << endl;
            cout << "do you want to repeat?: "; cin >> ch;
        }
        while (ch != 'n');
    
        cout << endl;
    
        system("pause");
    
        return 0;
    }
    
    //---------------------------------------------------------
    // function definition for void prntname(int dummy, string dummy)
    
    void prntname(int dummy1, string dummy2)
    
    {
        for (int j=1; j<=dummy1; j++)
        {
            cout << dummy2 << endl;
        }
    }
    //--------------------------------------------------------------
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  8. #8
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    try cin.ignore(MAX_INT,'\n') ; or INT_MAX
    You ended that sentence with a preposition...Bastard!

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    19
    you can clear the buffer in cin before input just like this :
    Code:
    // print your name n times
    
    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    #include <string>
    using namespace std;
    
    void prntname(int dummy1, string dummy2);
    
    int main()
    
    {
    	string name; int n; char ch;
    
    	do
    	{
    		cin.sync(); 
    		cout << "enter your name: ";
    		getline (cin, name);
    		cout << "how many times you want to print?: ";
    		cin >> n;
    		prntname(n, name);
    		cout << endl;
    		cout << "do you want to repeat?: ";
    		cin >> ch;
    	}
    	while (ch != 'n' && ch);
    
    	cout << endl;
    
    	system("pause");
    
    	return 0;
    }
    void prntname(int dummy1, string dummy2)
    
    {
    	for (int j=1; j<=dummy1; j++)
    	{
    		cout << dummy2 << endl;
    	}
    }
    the test is ok! you can have a test !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. User defined cos function
    By zfite in forum C Programming
    Replies: 11
    Last Post: 04-03-2011, 02:40 AM
  2. Replies: 9
    Last Post: 10-19-2009, 04:46 PM
  3. Squareroot User Defined Function
    By Air in forum C Programming
    Replies: 2
    Last Post: 01-25-2009, 05:21 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Calling a user-defined function in C++
    By brianptodd in forum C++ Programming
    Replies: 3
    Last Post: 10-08-2002, 12:09 PM