4 Replies - 442 Views - Last Post: 03 October 2012 - 03:06 PM Rate Topic: -----

#1 Dus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 12

Question: how to let the loop stops when entering $

Posted 02 October 2012 - 03:33 PM

Hi

First, I am a c++ bigener and tried to solve this assignment in several ways but couldn't figure out how to let it stops when I enter $
The assignment : "a C++ function that receives a single letter and prints out the corresponding digit on the telephone. Write a program that consists of a main function and the function described above. Read in letters in the main function and call the function to print a corresponding message. It should repeat to do so until a symbol $ is read."

#include<iostream>
using namespace std;
int main()
{
    char letter;
    int n;
    for(n=1;n!='$';n++)
    {
    cout<<"Enter a letter and get its corresponding digit on the phone";
    cin>>letter;
    switch(letter)
    {
        case 'A':
        case 'B':
        case 'C':
            cout<<"2 corresponds with "<<letter<<"\n";
            break;
        case 'D':
        case 'E':
        case 'F':
            cout<<"3 corresponds with"<<letter<<"\n";
            break;
        case 'G':
        case 'H':
        case 'I':
            cout<<"4 corresponds with "<<letter<<"\n";
            break;
        case 'J':
        case 'K':
        case 'L':
            cout<<"5 corresponds with "<<letter<<"\n";
            break;
        case 'M':
        case 'N':
        case 'O':
            cout<<"6 corresponds with "<<letter<<"\n";
            break;
        case 'P':
        case 'R':
        case 'S':
            cout<<"7 corresponds with "<<letter<<"\n";
            break;
        case 'T':
        case 'U':
        case 'V':
            cout<<"8 corresponds with "<<letter<<"\n";
            break;
        case 'W':
        case 'X':
        case 'Y':
            cout<<"9 corresponds with "<<letter<<"\n";
            break;
        case 'Q':
        case 'Z':
            cout<<"No digit corresponds to this letter"<<"\n";
            break;
        default:
            cout<<"Invalid charecter, try again\n";
    }
        
    }
}


Please I want to know what are my mistakes, I know my loop is wrong but couldn't think about any other solution.
My professor commented "you should write a program that consists of a main function and another function." I thought that I wrote two functions :/

Is This A Good Question/Topic? 0
  • +

Replies To: Question: how to let the loop stops when entering $

#2 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 86
  • View blog
  • Posts: 189
  • Joined: 23-September 12

Re: Question: how to let the loop stops when entering $

Posted 02 October 2012 - 04:11 PM

Hello:

This looks more like a scenario for a while loop. Also, you should probably convert any lowercase characters you receive into uppercase. Check out the standard header cctype for more information on how to do this--it has a who suite of functions for classifying characters.

The 'other function' would probably be one that prompts the user for a character, reads it, performs any case-conversion necessary, and then returns it.

I would give it a signature like this:

char
prompt(char &c)
{
}



This is a little weird (passing the character out as a return and by reference), but it allows you to do something like this:

while( prompt(letter) != '$' ) { /* code */ }



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1396
  • View blog
  • Posts: 4,859
  • Joined: 19-February 09

Re: Question: how to let the loop stops when entering $

Posted 02 October 2012 - 07:40 PM

The central part of the for loop expression is the 'while' condition to continue or not. The variable letter will contain the '$' character.

The letter variable should be initialized before using. Then use letter in the for statement.

05	    char letter = '*';
06	    int n;
07	    for(n=1;letter!='$';n++)


Was This Post Helpful? 1
  • +
  • -

#4 Dus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 27-September 12

Re: Question: how to let the loop stops when entering $

Posted 03 October 2012 - 01:52 PM

thank you guys for your time, you really helped me
Was This Post Helpful? 0
  • +
  • -

#5 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1396
  • View blog
  • Posts: 4,859
  • Joined: 19-February 09

Re: Question: how to let the loop stops when entering $

Posted 03 October 2012 - 03:06 PM

You don't have a separate function.

Functions (I)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1