473,503 Members | 3,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple programming error need help fixing, just a 'programming block'

10 New Member
here is the code:

// CS 200 Lab 11 Problem 3 - phone.cpp
// Test program for the keyToTones function.

#include <iostream>
using namespace::std;

// Definition of the PhoneTones struct

struct phoneTones
{
int rowTone, // Frequencies of the tones generated by a key press
colTone;
};

// Function prototype

phoneTones keyToTones ( char key );

//--------------------------------------------------------------------

void main()
{
char inputKey; // Input key
phoneTones keyFreqs; // Frequencies of the corresponding tones

// Read in a series of keys and output the corresponding tones.
for ( int i = 1; i <= 12 ; i++)
{
cout << endl << "Enter key pressed (0-9, *, or #): ";
cin >> inputKey;
keyFreqs = keyToTones(inputKey);
cout << "Tones produced at " << keyFreqs.rowTone << " and "
<< keyFreqs.colTone << " Hz" << endl;
}
}

//--------------------------------------------------------------------
// Insert your keyToTones function here.
phoneTones keyToTones ( char key )
{
//int rowTone, colTone;
//phoneTones rowTone, colTone;

switch ( key )
{
case '1': case '2': case '3': rowTone = 697;
break;

case '4': case '5': case '6': rowTone = 770;
break;

case '7': case '8': case '9': rowTone = 852;
break;

case '*': case '0': case '#': rowTone = 941;
break;
}

switch( key )
{
case '1': case '4': case '7': case '*': colTone = 1209;
break;
case '2': case '5': case '8': case '0': colTone = 1336;
break;
case '3': case '6': case '9': case '#': colTone = 1447;
break;
}
// what do i need to return to make this work?
// return colTone, rowTone

}


//--------------------------------------------------------------------

i need to know what to return to the calling function. the things i have tried are in the program under "what do i need to return to make this work?". and how do i need to define those in the function is my main problem returning should be the easy part.
Jul 19 '08 #1
6 1878
boxfish
469 Recognized Expert Contributor
To return two values like that, you could either modify variables passed to the function by reference, or make a dynamic array of them and return that.
Jul 19 '08 #2
newb16
687 Contributor
structures can be returned by value
Expand|Select|Wrap|Line Numbers
  1. struct phoneTones foobar()
  2. {
  3.   struct phoneTones foo;
  4.   ....
  5.   foo.a=1;
  6.   foo.b=2;
  7.   return foo;
  8. }
  9.  
Jul 19 '08 #3
mwhit74
10 New Member
structures can be returned by value
Expand|Select|Wrap|Line Numbers
  1. struct phoneTones foobar()
  2. {
  3.   struct phoneTones foo;
  4.   ....
  5.   foo.a=1;
  6.   foo.b=2;
  7.   return foo;
  8. }
  9.  
i will give this a shot and see if it works. thanks!
Jul 19 '08 #4
mwhit74
10 New Member
structures can be returned by value
Expand|Select|Wrap|Line Numbers
  1. struct phoneTones foobar()
  2. {
  3.   struct phoneTones foo;
  4.   ....
  5.   foo.a=1;
  6.   foo.b=2;
  7.   return foo;
  8. }
  9.  
ok i tried this and i kinda worked here is the code:// CS 200 Lab 11 Problem 3 - phone.cpp
// Test program for the keyToTones function.

#include <iostream>
using namespace::std;

// Definition of the PhoneTones struct

struct phoneTones
{
int rowTone, // Frequencies of the tones generated by a key press
colTone;
};

// Function prototype

phoneTones keyToTones ( char key );

//--------------------------------------------------------------------

void main()
{
char inputKey; // Input key
phoneTones keyFreqs; // Frequencies of the corresponding tones

// Read in a series of keys and output the corresponding tones.
for ( int i = 1; i <= 12 ; i++)
{
cout << endl << "Enter key pressed (0-9, *, or #): ";
cin >> inputKey;
keyFreqs = keyToTones(inputKey);
cout << "Tones produced at " << keyFreqs.rowTone << " and "
<< keyFreqs.colTone << " Hz" << endl;
}
}

//--------------------------------------------------------------------
// Insert your keyToTones function here.
phoneTones keyToTones ( char key )
{
phoneTones call;

switch ( key )
{
case '1': case '2': case '3': call.rowTone = 697;
break;

case '4': case '5': case '6': call.rowTone = 770;
break;

case '7': case '8': case '9': call.rowTone = 852;
break;

case '*': case '0': case '#': call.rowTone = 941;
break;
}

switch( key )
{
case '1': case '4': case '7': case '*': call.colTone = 1209;
break;
case '2': case '5': case '8': case '0': call.colTone = 1336;
break;
case '3': case '6': case '9': case '#': call.colTone = 1447;
break;

return call;
}

}


//--------------------------------------------------------------------

and here is the output when excuted:

Enter key pressed (0-9, *, or #): 5
Tones produced at 2 and 18 Hz

Enter key pressed (0-9, *, or #):
Jul 19 '08 #5
boxfish
469 Recognized Expert Contributor
Hi, one thing that seems very wrong with your code is that the return statement is inside of the switch statement. Try fixing that.
Hope this helps.
Jul 19 '08 #6
mwhit74
10 New Member
Hi, one thing that seems very wrong with your code is that the return statement is inside of the switch statement. Try fixing that.
Hope this helps.
thanks so much that was it, it works perfectly now, i knew it had to be something small
Jul 19 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

27
2341
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
8
5094
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
12
1418
by: Mark | last post by:
Apologies for the lengthy posting.... In a previous posting:...
6
9347
by: sathyashrayan | last post by:
Dear group, Following is a exercise from a book called "Oreilly's practical C programming". I just wanted to do a couple of C programming exercise. I do have K and R book, but let me try some...
7
1753
by: Trickynick1001 | last post by:
Hi, a newbie here. I don't have a real firm grasp on the idea of Javascript, as I'm used to programming in Qbasic and C. I'm not used to OOP. Anyway, I really don't have any idea what the...
5
4573
by: Mike | last post by:
Hello All, Please, if anyone can point me to the problem, I'd sure appreciate it! I am very new to VB programming and not a programmer to begin with. This is part of a Visual Basic 2005 Express...
56
5668
by: valentin tihomirov | last post by:
{ int i = 2; } int i = 1; There is no 'i' defined in the 'parent' context from the moment of declaration on. So what is the problem? They tell us they pursue language simplicity. The rule "do...
9
1883
by: Smithers | last post by:
Please consider this humble method: public void ResetCounters() { m_TotalExceptionsDetected = 0; m_TotalMessagesSent = 0; } Given no further information, would you wrap those two lines in a...
6
28857
kenobewan
by: kenobewan | last post by:
Congratulations! You are one of the few who realise that over 80% of errors are simple and easy to fix. It is important to realise this as it can save a lot of time. Time that could be wasted making...
0
7194
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7070
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.