counting hyphens/dashes
Oct 15, 2013 at 9:37pm UTC
I am trying to create a code where you input a social security number (with dashes) and it tells you whether it's legal or not. My problem is that i need my code to be able to count the dashes, but it's not, and im not sure how to fix it.
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <iostream>
#include <string>
using namespace std;
#define GOOD_SOCIAL_SECURITY_NUMBER 0
#define MISSING_DASH -1
#define MISSING_SECOND_DASH -2
#define TOO_MANY_DASHES -3
#define DASH_IN_WRONG_POSITION -4
#define MISSING_DIGITS_BETWEEN_DASHES -5
int checkDashes(string ss, size_t &dashOne, size_t &dashTwo)
{
int character = 0, count = 0;
for (int i = 0; i < ss.length(); i ++)
{
if (ss[character] == '-' )
{
count ++;
if (count == 1) dashOne = character + 1;
else if (count == 2) dashTwo = character + 1;
}
character ++;
}
return (0);
}
int main()
{
string ss;
size_t dashOne = 0, dashTwo = 0;
int numberOfDashes = 0;
cout << "Please input SS number." << endl;
cin >> ss;
cout << "You said " << ss << "." << endl;
numberOfDashes = checkDashes(ss, dashOne, dashTwo);
cout << "Number of dashes is : " << numberOfDashes << endl;
cout << "The first dash is at character number " << dashOne << "." << endl;
cout << "The second dash is at character number " << dashTwo << "." << endl;
if (numberOfDashes > 2)
{cout << "Error: Too many dashes." << endl;
}
if (numberOfDashes == 1)
{cout << "Error: Missing a dash." << endl;
}
if (numberOfDashes == 0)
{cout << "Error: No Dashes." << endl;
}
if (dashOne != 4 && dashTwo != 7)
{cout << "Error: Dashes in wrong position." << endl;
}
system("pause" );
if (numberOfDashes > 2)
{return (-3);
}
if (numberOfDashes == 1)
{return (-2);
}
if (numberOfDashes == 0)
{return (-1);
}
if (dashOne != 4 && dashTwo != 7)
{return (-4);
}
system("pause" );
}
Oct 15, 2013 at 10:20pm UTC
Ask yourself: What is the return value of the checkDashes()?
Topic archived. No new replies allowed.