Complete the following assignment in C++: Something is wrong with my code. All o
ID: 3837292 • Letter: C
Question
Complete the following assignment in C++:
Something is wrong with my code. All of the test cases fail. My outputs dont match the required outputs because things arent where they are suppose to be or shouldnt be there at all.
Here is my code:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
// function prototype
string validatePhone(string);
int main()
{
string pno;
string result;
cout << "Enter a phone number in the following format: (XXX) XXX-XXXX" << endl;
getline(cin, pno);
result = validatePhone(pno);
cout << pno << " -> " << result<<endl;;
return 0;
}
// function to validate phone number
string validatePhone(string phone)
{
string result, sub, exchange, area;
string str;
unsigned int i;
if(phone.length() != 14)
return "Input must be exactly 14 characters ";
else{
if(phone.find('(') == string::npos){
return " Missing character: (";
}
if(phone.find(')') == string::npos){
return " Missing character: )";
}
area = phone.substr(phone.find("(")+1, phone.find(")")-1);
cout<<area<<endl;
if(area.length() != 3){
return "Area code must be 3 characters long: " + area ;
}else{
for(i = 0; i < area.length() && area[i]>='0' && area[i]<='9'; i++);
if(i != area.size())
return "Area code may only contain digits: " + area ;
else{
if(phone.find(' ') == string::npos){
return "Missing a space";
}
if(phone.find('-') == string::npos){
return "Missing character: -";
}
exchange = phone.substr(phone.find(" ")+1, phone.find("-")-phone.find(" ")-1);
if(exchange.length() != 3)
return "exchange code must be 3 characters: " + exchange ;
for(i = 0; i < exchange.size() && exchange[i]>='0' && exchange[i]<='9'; i++);
if (i != exchange.size())
return "Exchange may only contain digits: " + exchange ;
sub = phone.substr(phone.find("-")+1, phone.length()-phone.find("-")-1);
if(sub.size() != 4)
return "subscription code must be 4 characters: " + sub ;
for(i = 0; i < sub.size() && sub[i]>='0' && sub[i]<='9'; i++);
if (i != sub.size())
return "Subscriber number may only contain digits: " + sub ;
return "Accepted";
}
}
}
}
__________________________________________________________________________________________________________________
Ask the user for a phone number. Verify the phone number is:
Exactly 14 characters long
Has both an open and closing parenthesis
Has a space
Has a dash
Test Case 1 - Failed
Required Output
Your Program's Output
Test Case 2 - Failed
Required Output
Your Program's Output
Test Case 3 - Failed
Required Output
Your Program's Output
Test Case 4 - Failed
Required Output
Your Program's Output
Test Case 5 - Failed
Required Output
Your Program's Output
Test Case 6 - Failed
Required Output
Your Program's Output
Test Case 7 - Failed
Required Output
Your Program's Output
Test Case 8 - Failed
Required Output
Your Program's Output
Test Case 9 - Failed
Required Output
Your Program's Output
Test Case 10 - Failed
Required Output
Your Program's Output
Test Case 11 - Failed
To prevent hardcoding, this test case is hidden. To pass this case, be sure that all the programming requirements are met.
Test Case 12 - Failed
Required Output
Your Program's Output
Test Case 13 - Failed
Required Output
Your Program's Output
Test Case 14 - Failed
Required Output
Your Program's Output
Test Case 15 - Failed
Required Output
Your Program's Output
Standard Inputabc
Explanation / Answer
Hi, I have fixed the issue.
THere was extra printing statement.
Please let me know in case of any issue.
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
// function prototype
string validatePhone(string);
int main()
{
string pno;
string result;
cout << "Enter a phone number in the following format: (XXX) XXX-XXXX" << endl;
getline(cin, pno);
result = validatePhone(pno);
cout << pno << " -> " << result;;
return 0;
}
// function to validate phone number
string validatePhone(string phone)
{
string result, sub, exchange, area;
string str;
unsigned int i;
if(phone.length() != 14)
return "Input must be exactly 14 characters ";
else{
if(phone.find('(') == string::npos){
return " Missing character: ( ";
}
if(phone.find(')') == string::npos){
return " Missing character: ) ";
}
area = phone.substr(phone.find("(")+1, phone.find(")")-1);
//cout<<area<<endl;
if(area.length() != 3){
return "Area code must be 3 characters long: " + area+" " ;
}else{
for(i = 0; i < area.length() && area[i]>='0' && area[i]<='9'; i++);
if(i != area.size())
return "Area code may only contain digits: " + area +" ";
else{
if(phone.find(' ') == string::npos){
return "Missing a space ";
}
if(phone.find('-') == string::npos){
return "Missing character: - ";
}
exchange = phone.substr(phone.find(" ")+1, phone.find("-")-phone.find(" ")-1);
if(exchange.length() != 3)
return "exchange code must be 3 characters: " + exchange+" " ;
for(i = 0; i < exchange.size() && exchange[i]>='0' && exchange[i]<='9'; i++);
if (i != exchange.size())
return "Exchange may only contain digits: " + exchange+" " ;
sub = phone.substr(phone.find("-")+1, phone.length()-phone.find("-")-1);
if(sub.size() != 4)
return "subscription code must be 4 characters: " + sub+" " ;
for(i = 0; i < sub.size() && sub[i]>='0' && sub[i]<='9'; i++);
if (i != sub.size())
return "Subscriber number may only contain digits: " + sub+" " ;
return "Accepted ";
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.