I'm trying to write a while loop to terminate a program when asked if you want to enter another name and the answer is no (or a number since I'm using character strings)

My first idea was to check the 0 element of the first name part asked for, but that does not work.

My second idea was to use strlen and as long as strlen for that name part was 1 (not counting the terminator) it would exit...that doesn't work either, it will terminate it, but only after a round of 111, and then entering 1.

Ideas and suggestions are welcome, I've never used a while loop to end repetition of functions with character strings/arrays.

I've heard somethin called a sentinel value, that may be what Im looking for

I'm trying to write a while loop to terminate a program when asked if you want to enter another name and the answer is no (or a number since I'm using character strings)

i think you should just make a condition in the while that if they answer no to the question then it would kick out. this is what i think you should do

#include <iostream.h>
#include <ctype.h>

int main ()
{
char choice;
do
{
//your code, afterwards
cout<<"Would you like to enter another name or number? (Y)es, (N)o"<<endl;
cin>>choice;
choice=tolower(choice);
}
while(choice=='y');
return 0;
}

That one doesn't quite work, the yes answer puts the 'y' in as the last name, and anything other than y exits the function.

I thought there was a plain while loop that might do it, but I'm not sure how to set the condition.

Here's the code that I'm using

#include "nameinp.h"
#include <iostream>
#include <string.h> 
using namespace std;

void main()
{
	const int maxin = 16;
	char lastName[maxin];
	char firstName[maxin]; 
	char midName[maxin];
	
	getlast(lastName, maxin);
	getfirst(firstName, maxin);
	getmid(midName, maxin);
	punchmeinthehead(lastName,firstName, midName);
	
}

That one doesn't quite work, the yes answer puts the 'y' in as the last name, and anything other than y exits the function.

I thought there was a plain while loop that might do it, but I'm not sure how to set the condition.

this is what i meant for you to do,

#include "nameinp.h"
#include <iostream>
#include <string.h> 
#include <ctype.h>
using namespace std;

void main()
{
        char choice;
	const int maxin = 16;
	char lastName[maxin];
	char firstName[maxin]; 
	char midName[maxin];
	[I]do{[/I]
	getlast(lastName, maxin);
	getfirst(firstName, maxin);
	getmid(midName, maxin);
	punchmeinthehead(lastName,firstName, midName);
      [I]  cout<<"Would you like to enter another name or number? (Y)es, (N)o"<<endl;
        cin>>choice;
        choice=tolower(choice);
        }
        while(choice=='y');[/I]
}

Still no workee, it's still interpreting the Y as the last name, and still, if you enter anything but Y, it quits the program. Here's the results

Please enter your last name up to 15 characters
me
Please enter your first name up to 15 characters
me
Please enter your middle name up to 15 characters
me
me me me
Would you like to enter another name or number? (Y)es, (N)o
y
Please enter your last name up to 15 characters
Please enter your first name up to 15 characters

Any other takers? Mr Cool is trying, but we're having issues...lol

Still no workee, it's still interpreting the Y as the last name, and still, if you enter anything but Y, it quits the program. Here's the results

Please enter your last name up to 15 characters
me
Please enter your first name up to 15 characters
me
Please enter your middle name up to 15 characters
me
me me me
Would you like to enter another name or number? (Y)es, (N)o
y
Please enter your last name up to 15 characters
Please enter your first name up to 15 characters

Any other takers? Mr Cool is trying, but we're having issues...lol

Let's not blame Mr. Cool just yet. If your function is keeping that 'y' and interpreting it as the last name, that could be a problem with your function. I'm guessing there is left over stuff in the input stream and that may be causing the problem. Hard to say since you haven't posted your function code. There is a good link at the top of the C++ forum on how to clear the input stream. Are you perchance using "getline"? You may need to clear the input stream. Here's the link:

http://www.daniweb.com/forums/thread90228.html

Regarding the fact that everything but 'y' or 'Y' exits the program, Mr. Cool didn't implement any error-checking to ensure that the input is a 'y' or 'n' (non case sensitive). You'd have to add that in the loop if you want the user to get an error message for typing, say, 'k'. Mr. Cool's idea of the while loop is a good solution. You just need to add the error checking if you want it and flush that input stream in one or more places (depending on the type of bad input you want to catch and the code in your functions).

Also, "void main" is bad. You want "int main".

Oh no disrespect for mr cool, he's genuinely trying to help. Here's the functions

//Input last name
void getlast( char lastName[] , int maxin)
{
	cout << " Please enter your last name up to 15 characters " << endl;
	cin.getline(lastName, maxin, '\n' );
	cin.ignore (cin.rdbuf()->in_avail(), '\n');
	
}


//Input first name
void getfirst( char firstName[] , int maxin)
{
	cout << " Please enter your first name up to 15 characters " << endl;
	cin.getline(firstName, maxin, '\n' );
	cin.ignore (cin.rdbuf()->in_avail(), '\n');
	
}


//Input middle name
void getmid( char midName[] , int maxin)
{
	cout << " Please enter your middle name up to 15 characters " << endl;
	cin.getline(midName, maxin, '\n' );
	cin.ignore (cin.rdbuf()->in_avail(), '\n');
}

void punchmeinthehead(const char* lastName, const char* firstName, const char* midName)
{
	size_t length = strlen(lastName) + strlen(firstName) + strlen(midName) + 3;
	char  *fullName = new char[length];
	strcpy (fullName, lastName);
	strcat (fullName, " ");
	strcat (fullName, firstName);
	strcat (fullName, " ");
	strcat (fullName, midName);
	cout << fullName << endl;
	delete [] fullName;
}

Now to error check, would that be another if after the cin?

oh..and the void main comes from my instructor....mind telling me why its bad?

silly me, I thought it could be done with a very simple while using one of the strings and string length as the condition, but that didn't work out at all

The below code should help clarify where to put things. I understand you were just kidding with Mr. Cool. Regarding why "void main" is bad, type in "int main vs. void main" or something like that on this site and some good threads come up explaining it. Basically, returning 0 means everything ran without error. Returning anything else means there was a problem.

#include "nameinp.h"
#include <iostream>
#include <string.h> 
#include <ctype.h>
using namespace std;

int main()   // changed from "void main"
{
        char choice;
	const int maxin = 16;
	char lastName[maxin];
	char firstName[maxin]; 
	char midName[maxin];
	do{
	getlast(lastName, maxin);
	getfirst(firstName, maxin);
	getmid(midName, maxin);
	punchmeinthehead(lastName,firstName, midName);
        cout<<"Would you like to enter another name or number? (Y)es, (N)o"<<endl;


        bool goodData = false;  // error checking loop priming
        while (!goodData)         // error checking while loop
        {
             cin>>choice;


             // the following line will clear your input stream
             cin.ignore( numeric_limits<streamsize>::max(), '\n' );


             choice=tolower(choice);

             // code to adjust variable goodData


        } // end of error-checking while loop

        }
        while(choice=='y');  // end of do-while loop

        return 0;   // added this line
}

I knew about the return 0, I thought you were going to clue me in on some big philosophical secret that only experienced coders know about void main vs int main, but yes, I understand the return codes.

Going through this several times with a few folks on here, it seems that while I understand the theory and how most things are supposed to work, structure and syntax are a little lost on me so far. I keep trying though...I hear thats what coders do :)

I'll try this solution and see how it works, thank you vernon

How is that cin.ignore statement supposed to work?

My compiler doesn't like it and tells me it's an undeclared identifier

Does mine not work for that portion of the code? I know it's a bit elaborate, but it's what the instructor gave us to use

hmm, if I substitute my cin.ignore it will compile, but something is wrong somewhere because it doesn't ask for any more name inputs no matter whether you use y, Y, n, or N.

Is it something to do with the bool expression? I tried a bool earlier and it gave me either nothing or infinite looping (of course, that could be the way I wrote it)

tried changing the bool, and that just exits the program as soon as you enter the third name portion.

What's the exact error message? Also, you have some old header types here:

#include "nameinp.h"
#include <iostream>
#include <string.h> 
#include <ctype.h>
using namespace std;

You have "using namespace std", so that's good. You can try changing it to something like:

cin.ignore (100, '\n' ); // I just picked 100 at random

You should try to use modern headers. Remove the ".h" at the end, like this:

#include "nameinp.h"
#include <iostream>
#include <string> 
#include <ctype>
using namespace std;

See if that compiles better. You may have a really old compiler. Check out some of those "void main vs. int main" threads. They're useful and explain better than I can.

I may have been missing a library, I didn't have ctype included. Let me try once more

edit: I'm using microsoft visual studio 2005

Something isn't letting it continue to "do" . It will ask for the y or n, but then it does nothing. I can keep entering stuff from the keyboard, but it doesn't ask me for last name again, nor does it exit

okay, got that, but we're still not working, it won't progress, do we have something in the wrong place?

and here is the error
1>e:\lab5\lab5\testnameinp.cpp(26) : error C2065: 'numeric_limits' : undeclared identifier
just cuz you wanted to see it

bah, nothing I'm doing works, it only makes it worse...lol

<snip>

Something isn't letting it continue to "do" . It will ask for the y or n, but then it does nothing. I can keep entering stuff from the keyboard, but it doesn't ask me for last name again, nor does it exit

The while loop for the yes/no input is infinite - it will just keep eating what you type forever. Nothing makes the loop control variable go to true. Fix it like so:

while (!goodData)         // error checking while loop
        {
            cin>>choice;
            choice=tolower(choice);

            // the following line will clear your input stream
            cin.ignore( 100, '\n' );

            // test choice, set goodData to true if choice is acceptable
            if( choice == 'y' || choice == 'n' )
                goodData = true;
            else
                cout << "I said enter a y or an n, dummy!" << endl;

        } // end of error-checking while loop

That one works great, now to memory management..lol.

Let's see if I an pull this one off

Okay, it all works except for one thing. If I enter something longer than the 15 I allocated for each name it takes off running. How does one error check that?

Would it be another if statement?
If lastName >15
name is to long
exit 0?

Okay, let me post something to work with

void getlast( char lastName[] , int maxin)
{
	cout << " Please enter your last name up to 15 characters " << endl;
	cin.getline(lastName, maxin, '\n' );
	cin.ignore (cin.rdbuf()->in_avail(), '\n');
}

Okay, now if more than 15 characters are entered, it takes off in a running loop. How do I keep them from entering 15 characters?

When getline reads n-1 characters of input, an error condition is set. You must clear the input stream's error flags first, then use the ignore, as in:

void getlast( char lastName[] , int maxin)
{
    cout << " Please enter your last name up to 15 characters " << endl;
    cin.getline(lastName, maxin, '\n' );
    cin.clear();    
    cin.ignore (100, '\n');
}

Learn something new every day.

Isn't that the truth, I didn't even know that would happen until I started testing for erroneous inputs

that works nicely, thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.