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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mwhit74
    New Member
    • Jul 2008
    • 10

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

    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.
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    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.

    Comment

    • newb16
      Contributor
      • Jul 2008
      • 687

      #3
      structures can be returned by value
      Code:
      struct phoneTones foobar()
      {
        struct phoneTones foo;
        ....
        foo.a=1;
        foo.b=2;
        return foo;
      }

      Comment

      • mwhit74
        New Member
        • Jul 2008
        • 10

        #4
        Originally posted by newb16
        structures can be returned by value
        Code:
        struct phoneTones foobar()
        {
          struct phoneTones foo;
          ....
          foo.a=1;
          foo.b=2;
          return foo;
        }
        i will give this a shot and see if it works. thanks!

        Comment

        • mwhit74
          New Member
          • Jul 2008
          • 10

          #5
          Originally posted by newb16
          structures can be returned by value
          Code:
          struct phoneTones foobar()
          {
            struct phoneTones foo;
            ....
            foo.a=1;
            foo.b=2;
            return foo;
          }
          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 #):

          Comment

          • boxfish
            Recognized Expert Contributor
            • Mar 2008
            • 469

            #6
            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.

            Comment

            • mwhit74
              New Member
              • Jul 2008
              • 10

              #7
              Originally posted by boxfish
              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

              Comment

              Working...