String Problem Errors

I need some help with solving this problem I was assigned. I have some errors right now and am wondering as to why they and how I can solve them. The problem from the book asks: Write a program the prompts the user to enter a social security number in the format of ddd-dd-ddd, where d is the 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

#include <iostream>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	cout << "Enter a SSN";
	string d = input; 

	for (int i = 0; i < d.length(); i++)
	{
		if ((i == 3 || i == 6) && d.charAt(i) == "-")
		{
			continue;
		}
		if (!Character.isDigit(d.charAt(i)))
		{
			cout << (d + "Is an invalid social security number");
		}
		else
		{
			cout << (d + " Is an invalid social securtiy number");
		}

	return 0; 
}
Line 28: You're missing a } terminating your for loop

Line 13: That's not how you read a string.
1
2
  string d;
  cin >> d;  


Line 14: You have a type mismatch. i should be unsigned or size_t.

Line 16,20: std::string has no charAt function. Simply use d[i].

Line 17: d[i] should be compared to a character literal, not a quoted string. i.e. == '-'

Line 21: What is Character? Test should simply be
 
  if (! isdigit(d[i]))

Line 21: isDigit should be isdigit

Lines 23,27: You are declaring the SSN invalid regardless of whether the character is a digit or not.
So I fixed a lot of stuff which was very helpful. However, I am still getting 2 errors on line 21 (i error) and line 29 (else statement error).

Here is my new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	string d;
	cout << "Enter a SSN";
	cin >> d; 

	for (int i; i < d.length(); i++)
	{
		if ((i == 4 || i == 7) && d[i] = "-")
		{
			continue;
		}
		if (!isdigit(d[i]));
		{
			cout << (d + "Is an invalid social security number");
		}
		else
		{
			cout << (d + " Is a valid social security number");
		}
	}
	return 0; 
}
Last edited on
line 7: You still want to initialize i to a value. If you start i at 0, I think you'd still want the - characters to be at positions 3 and 6. See also AbstractionAnon's above comment for line 14.

line 9: The code is using the assignment operator = instead of equality ==. See AbstractionAnon's comment in the post above for line 17 using single quotes '-' instead of double quotes "-".

line 13: Remove the ; at the end of the if condition. The ; terminates the if statement so the compiler thinks the else statement is unattached.


I'm not sure about the logic though. Do you really want an "invalid" or "valid" message to appear for each number in the SSN entered? Or did you mean to evaluate all the characters entered and just output a single invalid/valid message?
No I wanted to evaluate all the characters entered and just output a single invalid or valid message. How would I do that? Right now everything works, but it is outputting valid for every character and actually it won't even display invalid. It only displays valid even if I enter less than or more than 8 characters properly.

Here is my new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	string d;
	cout << "Enter a SSN: ";
	cin >> d; 

	for (int i = 0; i < d.length(); i++)
	{
		if ((i == 3 || i == 6) && d[i] == '-')
		{
			continue;
		}
		if (!isdigit(d[i]))
		{
			cout << (d + " Is an invalid social security number");
		}
		else
		{
			cout << (d + " Is a valid social security number");
		}
	}
	return 0; 
}
salomonthesav wrote:

No I wanted to evaluate all the characters entered and just output a single invalid or valid message. How would I do that?

You could use a flag.

1
2
3
4
5
6
7
8
9
10
11
	bool isValid{ true };

	for (int i = 0; i < d.length(); i++)
	{
		if ((i == 3 || i == 6) && d[i] == '-') continue;
		if ( !isdigit(d[i]) )
		{
			isValid = false;
			break;
		}
	}


edit: minor mistake
Last edited on
Hmmm. I tried that, but it doesn't give me any output then.

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
#include <iostream>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

int main()
{
	string d;
	cout << "Enter a SSN: ";
	cin >> d;
	bool isValid{ true };

	for (int i = 0; i < d.length(); i++)
	{
		if ((i == 3 || i == 6) && d[i] == '-')
		{
			isValid = false;
			cout << (d + " Is a valid social security number");
			break;
		}
		if (!isdigit(d[i]))
		{
			isValid = false;
			cout << (d + " Is an invalid social security number");
			break;
		}
		return 0;
	}
}
Move the return 0; outside your loop, otherwise the program will exit after the first iteration.

Your code will still print out every iteration. Move the output to outside the loop and depending on the state of isValid, output the corresponding string.

Check my previous post for updated code.
Okay I moved the output outside however, when I enter the following sequence: 23-23-5435, it outputs "Is an invalid social security number" 3 times instead of just once.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	string d;
	cout << "Enter a SSN: ";
	cin >> d;
	bool isValid{ true };

	for (int i = 0; i < d.length(); i++)
	{
		if ((i == 3 || i == 6) && d[i] == '-')
		{
			continue;
		}
		if (!isdigit(d[i]))
		{
			isValid = false;
			cout << (d + " Is an invalid social security number");
			break;
		}
	}
	cout << (d + " Is a valid social security number");
	return 0;
}
closed account (48T7M4Gy)
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string d;
    bool isValid = true;
    
    cout << "Enter a SSN: ";
    cin >> d;
    
    for (int i = 0; i < d.length(); i++)
    {
        if ( i == 3 or i == 6)
        {
            if (d[i] != '-')
            {
                isValid = false;
                cout << d[i] << " incorrect\n";
                break;
            }
        }
        else
        {
            if( !isdigit(d[i]) )
            {
                isValid = false;
                cout << d[i] << " incorrect\n";
                break;
            }
        }
    }
    
    cout << d;
    if(isValid == false)
        cout << " is not ";
    else
        cout << " is ";
    
    cout << "a valid social security number\n";
    
    return 0;
}
Enter a SSN: 23-23-2345
- incorrect
23-23-2345 is not a valid social security number


Enter a SSN: 234-23-234
234-23-234 is a valid social security number
 
Exit code: 0 (normal program termination)
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string d;
    bool isValid = true;
    
    cout << "Enter a SSN: ";
    cin >> d;
    
    for (int i = 0; i < d.length(); i++)
    {
        if (d.length() != 10)
        {
            isValid = false;
            cout << " incorrect size\n";
            break;
        }
        
        if ( i == 3 or i == 6)
        {
            if (d[i] != '-')
            {
                isValid = false;
                cout << d[i] << " incorrect\n";
                break;
            }
        }
        else
        {
            if( !isdigit(d[i]) )
            {
                isValid = false;
                cout << d[i] << " incorrect\n";
                break;
            }
        }
    }
    
    cout << d << (isValid ? " is": " is not") << " a valid social security number\n";
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.