Hello, I am a beginner to C, and this is my first post on this forum. Any guidance would be great. I am having an issue where I call my function enterPhoneNumber () and then return phoneNumber entered by the user. After returning the phoneNumber I need to pass it into the isValid() function and return yet another value. One of the many issues I am having is getting the isValid function to run and passing phoneNumber into it.
[code]
#include <iostream>
#include <string> // string class library
#include <iomanip> // setw(), fixed, showpoint, setprecision, left, right
using namespace std;
//CODE BEGINS
int main ()
{
void printHeading(); // Function prototype for printHeading
string enterPhoneNumber(); // function prototype for entering a number
string formatNumber(string phoneNumber); // prototype for function formatting phone number
void displayResults(int status, string phoneNumber); // prototype for function displaying results
int isValid(string phoneNumber); // function prototype for validating length of number entered
string phoneNumber;
int status;
string choice;
printHeading(); //call function to print the heading
do
{
phoneNumber = enterPhoneNumber (); // call function returning phone number string
status = isValid(phoneNumber);
phoneNumber = formatNumber(phoneNumber);
void displayResults(int status, string phoneNumber);
cout << " More phone numbers? Enter either 'y' for yes or 'n' for no";
cin >> choice;
cout << endl;
}
while (choice == "y" || choice == "Y");
cout << " End of program.";
return 0;
}
//CODE ENDS
//
// input functions
//
/* Function: enterPhoneNumber() ************************************
* parameters: none
* return: phoneNumber : string
*
* Comments
* -prompt & get a telephone number
*
* TODO: code the enterPhoneNumber() function below this comment
* 1. declare phone number string variable
* 2. prompt user to enter a phone number/get users input
* 3. return phone number entered
*/
/* Function: isValid() *******************************************
* parameters: phoneNumber : string
* return: status: int
* return a status of 0 if all is OK
* return a status of 1 if length is not 8 or 11
* return a status of 2 if first character not a 1
* return a status of 3 if number is not all digits 0-9
* Style Note: only one 'return' statement allowed per function
* TODO: code the isValid() function below this comment
*/
// CODE BEGINS
int isValid ( string phoneNumber)
{
int status;
if (phoneNumber.length() != 8 && phoneNumber.length() != 11 )
{
status= 1;
}
else if ( phoneNumber.at(0) != '1')
{
status = 2;
}
else if ( phoneNumber.find_first_not_of("02345789",0))
{
status = 3;
}
else
{
status = 0;
}
return status;
}
// CODE ENDS
/* Function: formatNumber() *****************************************
* parameters: phoneNumber : string (valid, unformatted phone number)
* return: phoneNumber: string
* return an 8 digit phone number formatted like: 1-555-6789
* return an 11 digit phone number formatted like: 1-(234)-555-6789
*
* TODO: code the formatNumber() function below this comment
*/
/* Function: printHeading() **************************************
* return: void
*
* TODO: replace "<your name here>" with your name like
* "Joe Blough" <- remove < and > also!
*/
void printHeading()
{
/* declarations *************************************************/
// if any
// program title
cout << "Telephone Number Verification Program";
cout << endl << endl;
// program introduction and directions
cout << "\nWelcome to the Telephone Number Verification program. ";
cout << "\nWhen prompted, please enter either an 8 digit telephone number ";
cout << "\nor an 11 digit telephone number. Valid telephone numbers ";
cout << "\nwill always begin with 1, always have 8 digits or 11 digits, and ";
cout << "\ncontain only the digits 0-9." ;
cout << endl;
I did as you said and i entered "1234"
output was as follows:
1234
1
i then entered a "correct number" and entered 12345678912
output was:
12345678912
0
also I have 1 in my isvalid list.
int isValid ( string phoneNumber)
{
int status;
if (phoneNumber.length() != 8 && phoneNumber.length() != 11 )
{