12 Replies - 977 Views - Last Post: 31 January 2012 - 04:14 PM Rate Topic: -----

#1 blkl06282003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 12

Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 09:50 AM

#include <iostream>
#include <string>
#include <iomanip> 
using namespace std;

//Prototypes
int ReadDials (char &d1, char &d2, char &d3, char &d4, char &d5, char &d6, char &d7, char &d8);
int ToDigit (char &d);
void AcknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8);

	char d = 0;
   
int main() 
{    
	char d1, d2, d3, d4, d5, d6, d7, d8;    
	int ReturnValue = 0;     
	while (ReturnValue != -5)
	{        ReturnValue = ReadDials(d1,d2,d3,d4,d5,d6,d7,d8);
	switch(ReturnValue)        
	{       case -1: cout <<"ERROR- An invalid character was entered." << endl; break;
			case -2: cout <<"ERROR- Phone number can't begin with 0." << endl; break;
			case -3: cout <<"ERROR- Phone number can't begin with 555." << endl; break;
			case -4: cout <<"ERROR- Hyphen is not in the correct posistion." << endl; break;
			default: AcknowledgeCall(d1,d2,d3,d4,d5,d6,d7,d8);
	}
	}    
	return 0;
}   
int ReadDials (char & d1, char & d2, char & d3, char & d4, char & d5, char & d6, char & d7, char & d8)
{
	cout << "Enter a phone number: ";
	cin >> d1;
	if (d1 == 'Q' || d1 == 'q')
		return -5;
	cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8;
	int result = ToDigit(d);

	int ToDigit(d);
	if (d1 == 0)
		return -2;
	if (d1 == 5 && d2 == 5 && d3 == 5)
		return -3;
	if (d4 != '-')
		return -4;
	if (result == -1)
		return -1;
		else
		return 0;
}
int ToDigit (char & d)
{
	toupper(d);
	switch(d)
	{        case 'A': case 'B': case 'C':
				d = '2'; return 0; break;
			case 'D': case 'E': case'F':
				d = '3'; return 0; break;
			case 'G': case 'H': case 'I':
				d = '4'; return 0; break;
			case 'J': case 'K': case 'L':
				d = '5'; return 0; break;
			case 'M': case 'N': case 'O':
				d = '6'; return 0; break;
			case 'P': case 'Q': case 'R': case 'S':
				d = '7'; return 0; break;
			case 'T': case 'U': case 'V':
				d = '8'; return 0; break;
			case 'W': case 'X': case 'Y': case 'Z':
				d = '9'; return 0; break;
	}
}
void AcknowledgeCall(char d1, char d2, char d3, char d4, char d5, char d6, char d7, char d8)
	{
		cout << "Phone Number Dialed: " << d1 << d2 << d3 << d4 << d5 << d6 << d7 << d8 << endl;
	}


:code:

This post has been edited by jimblumberg: 29 January 2012 - 09:51 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with return value -1 and -3. It will not output correctly

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 09:53 AM

Do you have a problem with this code? Or perhaps a question?

Always ask questions in the body of the post not in the title. What return values are you talking about?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 09:53 AM

What does correctly mean to you?

This is really, really vague. The harder you make it for us to even understand the problem the less likely someone can help you.

What are you expecting? Under what conditions is it wrong? What does right and wrong even mean to this program?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 10:00 AM

I think the OP is talking about a variable named ReturnValue

switch(ReturnValue)       
    {       case -1: cout <<"ERROR- An invalid character was entered." << endl; break;
            case -2: cout <<"ERROR- Phone number can't begin with 0." << endl; break;
            case -3: cout <<"ERROR- Phone number can't begin with 555." << endl; break;


So in other words, the result from their ReadDials() method isn't returning what they expected.


18 { ReturnValue = ReadDials(d1,d2,d3,d4,d5,d6,d7,d8);

Is there a C++ debugging tutorial for rookies they can be shown? They need to just walk through the method one line at a time and see their problem.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 10:06 AM

Quote

I think the OP is talking about a variable named ReturnValue

Probably, but we really need a more precise question. After all ReturnValue should never evaluate to -1.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 11:22 AM

I agree... As provided ToDigit can only return 0

Then again, this really shouldn't compile and run because ToDigit should throw an exception of "Not all code paths return a value" - As far as the compiler is concerned it is possible the switch statement never finds a match and there is no return in that eventuality.

As near as I see the OP only ever checks the value of zero anyway

11 char d = 0;
36 int result = ToDigit(d);

Near as I can figure this is just riddled with problems; both of logic and execution.
Was This Post Helpful? 0
  • +
  • -

#7 blkl06282003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 12

Re: Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 12:10 PM

I am new to coding -- I cannot get to to return the values when debugging for case 1 and case 3. Here are the instructions for the assignment...

Write a program that simulates the dialing of a phone number.

A user will input an 8-place number, for example: 359-3177 (note that the hyphen is considered a digit).
The rules for entering phone numbers follow.

8 places
It may have numbers, letters, or both.
The phone number cannot begin with 555.
The phone number cannot begin with 0.
The hyphen must be in the 4th position.
No other characters (@#$%^&*()_+=\|/><etc.) are allowed.
If a letter is entered, its output will be a number (check your phone pad).
Enter Q to Quit.

If all of the rules are met, you will output a message to the console that reads like the following.
Phone Number Dialed: UN9-3177 *the number entered

If all of the rules are not met, then you output one of the following error messages to the console.

ERROR - Phone number cannot begin with 555
ERROR - Phone number cannot begin with 0
ERROR - Hyphen is not in the correct position
ERROR - An invalid character was entered

It will then prompt the user to try again.


This should be a lot of fun!
Here are some great things to think about as you begin your program!

Define a function named ReadDials() that reads each digit and letter dialed into 8 separate char variables (DO NOT USE ARRAYS). All the digits are sent back through parameters by reference.

Then, for each digit, the program will use a function named ToDigit(), which receives a single char argument (pass by reference) that may be a number or a letter of one of the digits dialed.

If it is a number, then return 0 by value indicating that it is a valid digit. If the digit is a letter, then the number corresponding to the letter is returned by reference, and return 0 by value indicating that it is a valid digit. Here are the letters associated with each digit.

0 5 J K L
1 6 M N O
2 A B C 7 P Q R S
3 D E F 8 T U V
4 G H I 9 W X Y Z

If the digit entered is not one of the valid digits or one of the valid letters, return 1 by value indicating that you have an invalid digit.

A phone number never begins with a 0, so the program should flag an error if such a number is entered. Make ReadDials() return 2 in this case.

A phone number never begins with 555, so the program should flag an error if such a number is entered. Make ReadDials() return 3 in this case.

A phone number always has a hyphen (-) in the 4th position. Make ReadDials() return 4 in this case (if it doesn't have a hyphen in the 4th position). If a hyphen is in any other position, it is considered an invalid digit.

If the phone number is valid, the main calls the AcknowledgeCall function to write the converted number to the output file.

All the logic of the program should be put in functions that are called from Main(): ReadDials() and AcknowledgeCall().

The ToDigits() function is called from the ReadDials() function and is used to convert each letter entered individually into a digit and to verify that the user has entered a valid phone number. Have the program work for any number of phone numbers.

In the ToDigits() function uses the toupper function to convert any letters entered to uppercase. All the error messages are to be written to the output file from main() based on the return value from the functions.

Continue processing until the user enters a Q.

You will set up the 8 char variables to hold the digits of the phone number in main() and pass the variables to the functions by reference.

Sample Output from the Program

Enter a phone number (Q to quit): 213-2121
Phone Number Dialed: 213-2121

Enter a phone number (Q to quit): asc-dfer
Phone Number Dialed: 272-3337

Enter a phone number (Q to quit): 555-resw
ERROR - Phone number cannot begin with 555

Enter a phone number (Q to quit): 098-8765
ERROR - Phone number cannot begin with 0

Enter a phone number (Q to quit): 12345678
ERROR - Hyphen is not in the correct position

Enter a phone number (Q to quit): @34-*uyt
ERROR - An invalid character was entered

Enter a phone number (Q to quit): Q
Press any key to continue . . .
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 12:17 PM

Reminder to new folks: This is homework and we don't provide completed code for homework. Rather we try to help the student comprehend the concept they are struggling with so they can write their own code. They are being graded on their skill not ours.

View PosttlhIn`toq, on 29 January 2012 - 12:22 PM, said:

I agree... As provided ToDigit can only return 0

Then again, this really shouldn't compile and run because ToDigit should throw an exception of "Not all code paths return a value" - As far as the compiler is concerned it is possible the switch statement never finds a match and there is no return in that eventuality.

As near as I see the OP only ever checks the value of zero anyway

11 char d = 0;
36 int result = ToDigit(d);

Near as I can figure this is just riddled with problems; both of logic and execution.


I'm not sure what to tell you. There are SEVERAL problems with your code. Some are the result of not thinking things through. Some have already been mentioned.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 12:29 PM

Just one example:

18    {        ReturnValue = ReadDials(d1,d2,d3,d4,d5,d6,d7,d8);
19    switch(ReturnValue)       
20    {       case -1: cout <<"ERROR- An invalid character was entered." << endl; break;


ReturnedValue is set from ReadDials() method.

int ReadDials (char & d1, char & d2, char & d3, char & d4, char & d5, char & d6, char & d7, char & d8)
{
    cout << "Enter a phone number: ";
    cin >> d1;
    if (d1 == 'Q' || d1 == 'q')
        return -5;
    cin >> d2 >> d3 >> d4 >> d5 >> d6 >> d7 >> d8;
    int result = ToDigit(d);
 
    int ToDigit(d);
    if (d1 == 0)
        return -2;
    if (d1 == 5 && d2 == 5 && d3 == 5)
        return -3;
    if (d4 != '-')
        return -4;
    if (result == -1)
        return -1;
        else
        return 0;
}



Which can only return -1 if result == -1.
So where does result come from?
int result = ToDigit(d);
result is the returned value of the ToDigit() method.

int ToDigit (char & d)
{
    toupper(d);
    switch(d)
    {        case 'A': case 'B': case 'C':
                d = '2'; return 0; break;
            case 'D': case 'E': case'F':
                d = '3'; return 0; break;
            case 'G': case 'H': case 'I':
                d = '4'; return 0; break;
            case 'J': case 'K': case 'L':
                d = '5'; return 0; break;
            case 'M': case 'N': case 'O':
                d = '6'; return 0; break;
            case 'P': case 'Q': case 'R': case 'S':
                d = '7'; return 0; break;
            case 'T': case 'U': case 'V':
                d = '8'; return 0; break;
            case 'W': case 'X': case 'Y': case 'Z':
                d = '9'; return 0; break;
    }
}



But this method only ever returns 0. It never returns anything else, therefore it is impossible for it to return -1, therefore it is impossible for result to be -1, so it is impossible for your switch construct to ever get a -1 condition.

This isn't tough. You just need to stop, slow down and walk through it methodically. That's 90% of what debugging is.
Was This Post Helpful? 0
  • +
  • -

#10 blkl06282003  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-January 12

Re: Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 12:44 PM

Being new to this, I just could not see the starting point as to why it would not return anythinh other than the one case statement. Thanks for all your help.

I have probably overthought it, which is why I could not see the problem -- which tends to be my biggest problem.
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Need help with return value -1 and -3. It will not output correctly

Posted 29 January 2012 - 01:06 PM

Not uncommon. I have a tutorial on doing homework that is waiting to be released from the Authoring area to the public Tutorials area. It concentrates on how to plan and organize and think these things through.

Once it is released to the public I'll post a link to it for you.
Was This Post Helpful? 1
  • +
  • -

#12 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 125
  • Joined: 24-January 12

Re: Need help with return value -1 and -3. It will not output correctly

Posted 31 January 2012 - 02:09 PM

Quote

Not uncommon. I have a tutorial on doing homework that is waiting to be released from the Authoring area to the public Tutorials area. It concentrates on how to plan and organize and think these things through.


That's what I need! Even though I am not in school and am learning C++ for my own personal growth, I seem to be having trouble thinking the problem through before I start coding which results in a large kludge.

Hopefully there is some advice in there on 'thinking programatically', and pseudo-code.
Was This Post Helpful? 0
  • +
  • -

#13 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: Need help with return value -1 and -3. It will not output correctly

Posted 31 January 2012 - 04:14 PM

View PosttlhIn`toq, on 29 January 2012 - 02:06 PM, said:

Not uncommon. I have a tutorial on doing homework that is waiting to be released from the Authoring area to the public Tutorials area. It concentrates on how to plan and organize and think these things through.

Once it is released to the public I'll post a link to it for you.


This is the 'Help me with my homework' article you wanted.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1