473,652 Members | 3,045 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(inpu tKey);
cout << "Tones produced at " << keyFreqs.rowTon e << " and "
<< keyFreqs.colTon e << " 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 1887
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(inpu tKey);
cout << "Tones produced at " << keyFreqs.rowTon e << " and "
<< keyFreqs.colTon e << " 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
2371
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. It is available at: http://staff.washington.edu/sabbey/py_do Good background on thunks can be found in ref. . Simple Thunks
8
5104
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
12
1425
by: Mark | last post by:
Apologies for the lengthy posting.... In a previous posting: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=3fb3728b%240%2422605%24cc9e4d1f%40news.dial.pipex.com&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D3fb3728b%25240%252422605%2524cc9e4d1f%2540news.dial.pipex.com I asked a question about how to ensure that some "tidying up" code would be run at the end of a Method independently of where...
6
9399
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 simple one. Exercise 4-2: Write a program to print a block E using asterisks (*), where the E has a height of seven characters
7
1770
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 problem is with this code, it just simply won't work properly. Some of the functions aren't done, but the main one gives me a Not a Number message in the text box where the calculations are supposed to come up. I tried to use a parseInt on my stuff,...
5
4589
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 Edition program to control a remote basketball scoreboard display unit. All I'm trying to do is add 5 byte variables and store the result in an integer variable. I added a Try/Catch block to take look at things. This exception occurs only when...
56
5715
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 not define a variable more than once in the same context" is natural, and simplest therefore. All normal languages obey it therefore. Overcomplicating a grammar by injecting more barrieres is a path
9
1895
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 try... catch block?
6
28866
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 unnecessary changes, that in turn can cause further problems. Programming is a humbling experience. An experience that causes one to reflect on human error. One major cause of these errors is syntax, syntax, syntax. We tend not to notice when we...
0
8367
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8811
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4145
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1591
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.