Functions not running/passing values in way intended

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
*/

// STUDENT CODE BEGINS
string enterPhoneNumber()
{
string phoneNumber;
cout << "Enter a phone number: ";
cin >> phoneNumber;
return phoneNumber;

}


// STUDENT CODE ENDS


//
// processing functions
//


/* 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
*/

// CODE BEGINS
string formatNumber(string phoneNumber)
{
if (phoneNumber.length() == 8)
{
phoneNumber.insert(1,"-");
phoneNumber.insert(5,"-");
}
else if (phoneNumber.length() == 11)
{
phoneNumber.insert(5,"-(");
phoneNumber.insert(6, ")-");
phoneNumber.insert(11,"-");
}
return phoneNumber;
}


// CODE ENDS



//
// output functions
//

/* Function: displayResults() ***************************************
* parameters: status : int (values 0-3)
* phoneNumber : string
* return: none: void
*
* TODO: code the displayResults() function below this comment
*/

// CODE BEGINS

void displayResults(int status, string phoneNumber)
{
if ( status == 0)
{
cout << " Formatted number is: " << phoneNumber;
}
else if( status > 0)
{
cout << " Invalid phone number: " << phoneNumber;
cout << " Your call cannot be completed because: ";
switch (status)
{
case 1:
cout << " A phone number must have 8 or 11 characters.";
cout << " Invalid phone number: " << phoneNumber;
break;

case 2:
cout << " A phone number must begin with a 1.";
cout << " Invalid phone number: "<<phoneNumber;
break;

case 3:
cout << " A phone number must contain only digits 0-9.";
cout << " Invalid phone number: "<< phoneNumber;
break;
}
}
else
{
cout << " Please try again ";
}
}




// CODE ENDS


//-------------------------------------------------------------------
// Utility Functions
//-------------------------------------------------------------------


/* Function: printHeading() **************************************
* return: void
*
* TODO: replace "<your name here>" with your name like
* "Joe Blough" <- remove < and > also!
*/
void printHeading()
{
/* declarations *************************************************/
// if any

/* statements ***************************************************/


// print program and programmer information
cout << endl;

cout << "Project/program name: U01_Telephone" << endl;
cout << "by" << endl;
cout << "Joe Blough" << endl;

cout << endl;

// 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;


cout << endl << endl;

}



Last edited on
what do you get if you do this in main?


phoneNumber = enterPhoneNumber ();
cout << phoneNumber << endl;
status = isValid(phoneNumber);
cout << status << endl;

I don't see anything on the surface.

I would ask why '1' is not in your isvalid list of digits, though?
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 )
{

status= 1;
}
Marking this as solved! void displayResults(int status, string phoneNumber); is an illegal function call!
Last edited on
Topic archived. No new replies allowed.