Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Justaddwater

One last easy (I HOPE) question RE: C++

This topic is 5479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK I have now finished the program to take a 10 digit number and check lots of problems, and if the number is a valid code then accept it. One problem... The code can accept '-' but they do not count toward your total numbers, as my code is written now they do count. Could anyone point me in a direction to easily remedy this? This is homework- so just a hint would be awesome!

#include <fstream>
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>


using namespace std;

void dirs();

void main ()
{
	string code;
	int dashSum = 0;
	char ok = 'y';
	int valid = 0;
	int menu;
	int redo = 0;

	
	
	while (ok == 'y')//main loop

	{
		do
	{
	cout << setw(40) << "+Main Menu+\n";
	cout << setw(48) << "________________________\n";
	cout <<endl<<endl<<endl;
	cout << "Please choose from the following options,\n\n";
	cout << "(1) Read Directions.\n";
	cout << "(2) Enter Book Code.\n\n";
	cout << "Enter choice (1) or (2): ";
	cin >> menu;
	system ("CLS");
	}
	while ((menu > 2) || (menu < 1));//checks input-resets screen with bad input

	
	switch(menu)
	{
	case 1: dirs();
		break;
	case 2: 
		break;
	
	}
	
	do
	{
	cout << "Enter code: ";
	cin >> code;
	
	
	if (code.length() > 10)
	{	
		cout << "INVALID. " << code << " has more then ten characters\n";
		valid = 1;
	}
	if (code.length() < 10)
	{	
		cout << "INVALID. " << code << " has less then ten characters\n";
		valid = 1;
	}
	if (valid == 0)
	{
	for (int j= 0; j < 9; j++)//making sure first 9 are numbers

	{	if (code[j] == '-')
	continue;
		if (!isdigit(code[j]))
	{	
			cout << "INVALID. ("<< code[j] << ") First Nine must be numbers not letters.\n";
			valid =1;
	}

	}
	}
	

if (valid == 0)
{
	if (!isdigit(code[9]))//checking last character for a 'x'

	{
		switch (code[9])
		{
		case 'x':
			break;
		case 'X':
			break;
		case 1:
			break;
		default:
			{
				cout << "INVALID. Last character (" << code[9] << ") must be a number or an 'X'\n";
				valid = 1;
			}
		}
	}
}

	if (valid == 0)
	{

	if (code[0] == '-')//checking first for -

	{
		cout << "INVALID (" << code[0] << ") cannot start with dash\n";
		valid = 1;
	}
	if (code [9] == '-')//checking last for -

	{
		cout << "INVALID (" << code[9] << ") Cannot end with dash\n";
		valid = 1;
	}
	}
	if (valid == 0)
	{
	for (char x = 0; x < 10; x++)
	{		if (code[x] == '-')
			dashSum++;
	}
	code[0]++;
	
	switch (dashSum)//checking 0 or 3 dashes

	{
	case 0: break;
	case 3: break;
	default: 
		{
			cout << "INVALID. you have (" << dashSum << ") dashes, you must have either zero or three\n";
			valid = 1;
		}
		break;
	}
	}
	if (valid == 0)
	{
	for (int x= 0; x < 10; x++)
	{
	int dashCount = 0;
	if (code[x+1] == '-')
		dashCount++;
	if (code[x+2] == '-')
		dashCount++;
	if (dashCount == 2)
	{
		cout << "INVALID. You cannot have two dashes together\n";
		valid = 1;
	}
dashCount = 0;
	}
	}

	if (valid == 0)
	{
		cout << "VALID book code entered...\n\n"; 
		cout << "\t\tWould you like to enter another code? (Y)es, (N)o";
		cin >> ok;
		redo = 0;
		system ("CLS");
		
	}
	else 
	{
		cout << "Please correct above error(s)\n\n\n";
		redo = 1;
	}
	
	dashSum = 0;
	valid = 0;
	}
	while (redo == 1);

}


	system ("pause");



}
void dirs()
{
	system ("CLS");
	cout << "____________________________________________________________________\n";
	cout << "____________________________________________________________________\n";
	cout << "The book code must be ten digits.\n";
	
	cout << "The first nine digits must be decimal digits (0...9).\n";
	cout << "The tenth can be either a decimal digit or the letter X.\n";
	cout << "Three single dashes may be between any of the characters.\n";
	cout << "But no two dashes together.\n";
	cout << "Three dashes or NO dashes.\n";
	cout << "It also cannot begin or end with a dash.\n";
	cout << "____________________________________________________________________\n";
	cout << "____________________________________________________________________\n";
	system ("pause");
	system ("CLS");
}

	



EDIT: Changed spelling in code [edited by - justaddwater on November 17, 2003 4:59:38 PM] [edited by - justaddwater on November 17, 2003 5:01:01 PM]

Share this post


Link to post
Share on other sites
Advertisement
About all I can suggest is you keep your own string length variable, and calculate it after the code is entered. Just iterate through the string and check each character to see if it''s a dash, if it is, then move on to the next. if it''s not, then increase the length by one.

for example


int length = 0;

for (int i = 0; i < codestring.length(); ++i)
{
if (codestring != ''-'')
{
length++;
}
}


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!