I am working on some homework right now and the program has to return two specified values depending on the key value the user enters. I only had to write the bottom portion of the program as the top portion was given to me as a shell.

It runs but is not returning the correct values, can anyone give me any insight as to why it is not returning the correct vaues? I am not sure if I am understanding completely how structs pass values and such...

Thanks

#include <iostream.h>

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

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

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

    // Read in a series of keys and output the corresponding tones.

    cout << endl << "Enter key pressed (0-9, *, or #): ";
    cin >> inputKey;
    keyFreqs = keyToTones(inputKey);
    cout << "Tones produced at " << keyFreqs.rowTone << " and "
         << keyFreqs.colTone << " Hz" << endl;
    system("pause");
	return 0;
}

//--------------------------------------------------------------------
// Insert your keyToTones function here.
//--------------------------------------------------------------------

PhoneTones keyToTones ( char key )
{
    PhoneTones Freqs;

    switch (key)
    {
      case '1': case '2': case '3': Freqs.rowTone = 697; break;
      case '4': case '5': case '6': Freqs.rowTone = 770; break;
      case '7': case '8': case '9': Freqs.rowTone = 852; break;
      case '*': case '0': case '#': Freqs.rowTone = 941; break;
    }
    
    switch (key)
    {
    case '1': case '4': case '7': case '*': Freqs.colTone = 1209; break;
    case '2': case '5': case '8': case '0': Freqs.colTone = 1336; break;
    case '3': case '6': case '9': case '#': Freqs.colTone = 1477; break;
    }
}

Hey,

A couple of questions, what compiler are you using and how in the world does this run?

I can see two changes that you have to make off the bat, first replace

#include<iostream.h> with just #include<iostream>

no one uses .h anymore its been made redundent unless your compiler is very old in which case download Visual C++.

The second problem is that you need to include

using namespace std;

after declaring the headers.

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

// Function prototype

PhoneTones keyToTones ( char key );

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

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

    // Read in a series of keys and output the corresponding tones.

    cout << endl << "Enter key pressed (0-9, *, or #): ";
    cin >> inputKey;
    keyFreqs = keyToTones(inputKey);
    cout << "Tones produced at " << keyFreqs.rowTone << " and "
         << keyFreqs.colTone << " Hz" << endl;
    system("pause");
	return 0;
}

//--------------------------------------------------------------------
// Insert your keyToTones function here.
//--------------------------------------------------------------------

PhoneTones keyToTones ( char key )
{
    PhoneTones Freqs;

    switch (key)
    {
      case '1': case '2': case '3': Freqs.rowTone = 697; break;
      case '4': case '5': case '6': Freqs.rowTone = 770; break;
      case '7': case '8': case '9': Freqs.rowTone = 852; break;
      case '*': case '0': case '#': Freqs.rowTone = 941; break;
    }
    
    switch (key)
    {
    case '1': case '4': case '7': case '*': Freqs.colTone = 1209; break;
    case '2': case '5': case '8': case '0': Freqs.colTone = 1336; break;
    case '3': case '6': case '9': case '#': Freqs.colTone = 1477; break;
    }
}

Notice that in your function you declare a PhoneTones Freqs local to that function, but it isn't used anywhere else.

Why not pass your keyFreqs by reference to the function and modify it?

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

// Function prototype

void keyToTones ( char, PhoneTones& );

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

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

    // Read in a series of keys and output the corresponding tones.

    cout << endl << "Enter key pressed (0-9, *, or #): ";
    cin >> inputKey;
    keyToTones(inputKey, keyFreqs);
    cout << "Tones produced at " << keyFreqs.rowTone << " and "
         << keyFreqs.colTone << " Hz" << endl;
    system("pause");
	return 0;
}

//--------------------------------------------------------------------
// Insert your keyToTones function here.
//--------------------------------------------------------------------

void keyToTones ( char key, PhoneTones& Freqs )
{
    switch (key)
    {
      case '1': case '2': case '3': Freqs.rowTone = 697; break;
      case '4': case '5': case '6': Freqs.rowTone = 770; break;
      case '7': case '8': case '9': Freqs.rowTone = 852; break;
      case '*': case '0': case '#': Freqs.rowTone = 941; break;
    }
    
    switch (key)
    {
    case '1': case '4': case '7': case '*': Freqs.colTone = 1209; break;
    case '2': case '5': case '8': case '0': Freqs.colTone = 1336; break;
    case '3': case '6': case '9': case '#': Freqs.colTone = 1477; break;
    }
}

I might be a little off on parameter syntax, but the idea is to simply pass your keyFreqs by reference and modify its members in the function.

Assuming you are unallowed to do this, you should not forget to return the copy of the value you modified in your function

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

// Function prototype

PhoneTones keyToTones ( char key );

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

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

    // Read in a series of keys and output the corresponding tones.

    cout << endl << "Enter key pressed (0-9, *, or #): ";
    cin >> inputKey;
    keyFreqs = keyToTones(inputKey);
    cout << "Tones produced at " << keyFreqs.rowTone << " and "
         << keyFreqs.colTone << " Hz" << endl;
    system("pause");
	return 0;
}

//--------------------------------------------------------------------
// Insert your keyToTones function here.
//--------------------------------------------------------------------

PhoneTones keyToTones ( char key )
{
    PhoneTones Freqs;

    switch (key)
    {
      case '1': case '2': case '3': Freqs.rowTone = 697; break;
      case '4': case '5': case '6': Freqs.rowTone = 770; break;
      case '7': case '8': case '9': Freqs.rowTone = 852; break;
      case '*': case '0': case '#': Freqs.rowTone = 941; break;
    }
    
    switch (key)
    {
    case '1': case '4': case '7': case '*': Freqs.colTone = 1209; break;
    case '2': case '5': case '8': case '0': Freqs.colTone = 1336; break;
    case '3': case '6': case '9': case '#': Freqs.colTone = 1477; break;
    }

    return Freqs;
}

It is old -- I am using bloodshed since that is all I have loaded on my computer right now.

@intrade

I did try a pass by reference in the function and it did not work

Scratch that, InTrade both of your solutions worked. I was missing one thing in the pass by reference, and I didn't see the third solution with simply returning Freqs.

Thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.