Help with figuring out my sentinel value!

So, unfortunately I am back again seeking the expertise of you all on this forum! I have a program that I have written that is suppose to prompt the user to enter a string value and it has to be in 000-00 format. If it is/is not in the right format I prompt the user accordingly. My problem is figuring out how to end my loop with a sentinel value. I know it something easy but I'm struggling to figure it out. Btw, I know that my proceed variable is not in the loop and that's why it will not end with a negative number. My problem is figuring how to or where to put the code. I tried a do while loop and had no success. Thanks for any help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;

int main()
{
	//Declare variables
	string digits		= "";
	int numChars		=0;
	int proceed			=0;

	//Get input from user
	cout << "Please enter 5 digits in a 000-00 format " << endl;
	cout << "(negative number to end progam): ";
	getline(cin, digits);

	while (proceed >= 0) 
	{
		//determine # of characters
		numChars = digits.length();

		if (numChars == 6)
		
			if (digits.find("-", 3))
				cout << "That is the correct format! " << endl;
			//end if
		//end if 
		
		if (numChars == 5)
			cout << "That is not the correct format. " << endl;

		//Option to continue
		cout << "\nPlease enter 5 digits in a 000-00 format " << endl;
		cout << "(negative number to end progam): ";
		getline(cin, digits);
	} //end while

	system("pause");
	return 0;
}
Could someone give me some advice, please!!! I am new to C++ and am trying to learn as much as I can but it is getting harder and harder as I proceed. Thanks in advance!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>
using namespace std;

bool formatIsGood (const std::string& digits) {
	if (digits.length() != 6 || digits.find('-', 3) == string::npos)
		return false;
	return true;
}

int main()
{
	//Declare variables
	string digits		= "";
	int numChars		=0;
	int proceed			=0;

	//Get input from user
	cout << "Please enter 5 digits in a 000-00 format " << endl;
	cout << "(negative number to end progam): ";
	getline(cin, digits);

	while (proceed >= 0) 
	{
		if (formatIsGood(digits))
			break;
		cout << "\nPlease enter 5 digits in a 000-00 format " << endl;
		cout << "(negative number to end progam): ";
		getline(cin, digits);
	}
	
	cout << "Format is good." << endl;
	cin.get();
	return 0;
} 

But this is not complete actually. Did you want to check that each digit is numerical?
Last edited on
This one checks that each character (except for the hyphen) is actually a digit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <list>
using namespace std;

bool formatIsGood (const std::string& digits) {
	if (digits.length() != 6 || digits.find('-', 3) == string::npos)
		return false;
	static const std::list<int> digitPositions {0,1,2,4,5};
	for (int i: digitPositions)
		if (!isdigit (digits.at(i)))
			return false;
	return true;
}

int main()
{
	//Declare variables
	string digits		= "";
	int numChars		=0;
	int proceed			=0;

	//Get input from user
	cout << "Please enter 5 digits in a 000-00 format " << endl;
	cout << "(negative number to end progam): ";
	getline(cin, digits);

	while (proceed >= 0) 
	{
		if (formatIsGood(digits))
			break;
		cout << "\nPlease enter 5 digits in a 000-00 format " << endl;
		cout << "(negative number to end progam): ";
		getline(cin, digits);
	}
	
	cout << "Format is good." << endl;
	cin.get();
	return 0;
} 
Last edited on
Hello, prestokeys, thank you for the response! No I am just needing the user to enter a 000-00 format and my program makes sure it is that format. If it is in the correct format then I prompt "That is the correct format!" and if not I prompt "That is not the correct format.". I have fixed the code to where a sentinel value does end the program but now my issue is that the user could enter 6 digits without the hyphen and the program prompts a "Correct format" output. The program will also keep looping if the user enters anything more than 6 or anything less than 5 digits. I know it has something to do with the way I am coding the if statements. My updated code is below. Thanks in advance for any help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int main()
{
	//Declare variables
	string digits		= "";
	int numChars		=0;

	//Get input from user
	cout << "Please enter 5 digits in a 000-00 format " << endl;
	cout << "(negative number to end progam): ";
	getline(cin, digits);

	while (digits != "-1") 
	{
		//determine # of characters
		if (digits.length() == 6)

			//determine if format is correct 
			if (digits.find("-", 3))
				cout << "\nThat is the correct format! " << endl;
			//end if
		//end if 
		
		//determine if format is not correct 
		if (digits.length() == 5)
			cout << "\nThat is not the correct format. " << endl;

		//Option to continue
		cout << "\nPlease enter 5 digits in a 000-00 format " << endl;
		cout << "(negative number to end progam): ";
		getline(cin, digits);
	}//end while

	system("pause");
	return 0;
}
Hi,

Try this:


1
2
3
4
if(digits.length() == 6 && digits.find("-", 3))
{
  cout<<"\nThat is the correct format! "<<endl;
}


Perhaps that will fix the first problem. As for the second problem, where the program loops if you enter more than six or less than five, isn't that what you want? If the correct format is 000-00, then that means, having 4 digits or more than six digits is incorrect. So that would mean your program is functioning correctly?

If you want your program to force the user to only enter six characters (hyphen included), then that means the code for the second if statement can be written as:

1
2
3
4
else 
{
  cout<<"That is not the correct format. "<<endl;
}


In addition, why not use a Boolean variable called 'correctInput' and the moment the user inputs a correct input, set the flag variable to true causing the while loop to end after that iteration? Then, you can rewrite the while loop to be:

while(digits != "-1" || correctInput == false)

I hope this helps,
Joe
Last edited on
Hi, Joe, thank you for your input! There was really no need for the 2nd and 3rd if statements so thank you for pointing that out! So the program does work as designed except for one last flaw!!! If the user inputs 6 digits only, with no hyhpen, it prompts a "Correct format" output??? I cannot figure this one out. I'm not understanding why it is doing that because in the first if statement it clearly states that digits has to contain 6 characters AND have a hyphen in subcript 3!?! Here is my updated code. Thank you all so much for your input thus far! You all have been a huge help in helping me better understand the code. Again, thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main()
{
	//Declare variables
	string digits		= "";
	
	//Get input from user
	cout << "Please enter 5 digits in a 000-00 format " << endl;
	cout << "(negative number to end progam): ";
	getline(cin, digits);

	while (digits != "-1") 
	{
		//Determine if format is correct
		if (digits.length() == 6 && digits.find("-", 3))
				cout << "\nThat is the correct format! " << endl; 
		
		//Format is not correct 
		else
			cout << "\nThat is not the correct format. " << endl;

		//Option to continue
		cout << "\nPlease enter 5 digits in a 000-00 format " << endl;
		cout << "(negative number to end progam): ";
		getline(cin, digits);
	}//end while

	system("pause");
	return 0;
}
I don't see why you continue to struggle when my solution works. Did you try to compile mine? If 6 digits with no hyphen is inputted, it prompts the user to input again. My solution also follows procedural decomposition. The algorithm you are trying to work out may be needed elsewhere somewhere along the line (e.g. the input is asked again somewhere else), so you should write a separate function like I did. And I'm pretty sure later on you will want to make sure the digits are actually numerical.

Also, the function find (const string& str, size_t pos) you are using returns string::npos (which I used in my code) when str is not found from position pos to end of string. This does not translate to false (0) because it is -1. Hence the inadvertant true value return you are getting. Use digits.find("-", 3) != string::npos if you insist on sticking with your method, or use digits.at(3) == '-'
Last edited on
If you are looking for a really simple solution, do this:

Change the: digits.find("-", 3) to: digits[3] == '-'

Try it and it should work.

Joe (sparkprogrammer@gmail.com)
Try using this instead:

digits[3] == '-'

It should work.

Joe - sparkprogrammer@gmail.com
Thank you prestokeys and Joe, I will keep working on the program and try both of your suggestions. As prestokeys pointed out earlier I will have to make sure that the user does input numerical data and not letters. That will be another task.
Joe, thank you! That did the trick, so digits[3] == '-' is saying that there must be a hyphen in subscript 3 of digits?? Also, any ideas on how to make sure the user is inputting ONLY numerical data and nothing else? Thank you in advance for any advice!
Last edited on
Use isdigit, like in my solution above.
prestokeys, is it possible to do it without using boolean variables and just use string functions??
If you want to avoid using STL libraries and use only string functions (which is still using STL), then define a separate function to act as the isdigit function:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool isdigit_by_smk385 (char c) {
	static const std::string numeric = "0123456789";
	return numeric.find (c) != std::string::npos;
}

//  Now apply the above function to every character in digits, except position 3

static const std::list<int> digitPositions {0,1,2,4,5};
	for (int i: digitPositions)
		if (!isdigit_by_smk385 (digits[i]))
        {cout << "\nThat is not the correct format. " << endl;
        return false;
}

Last edited on
Ok thanks, I really appreciate your input, prestokeys! Will give it a try!
Topic archived. No new replies allowed.