3 Replies - 240 Views - Last Post: 23 March 2012 - 05:15 PM Rate Topic: -----

#1 gamerssz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 12

I could use advice on a if statement and getline

Posted 23 March 2012 - 04:47 PM

Ok pretty much want a hypen inserted after every third letter entered because the program is suppose to convert letters into digits. Also I know I need to use the iomanip header to use a getline(cin, letter) in this case and wanted to know if im on the right track and hints would be useful. Thanks for all input and advice im still learning and have been at it for a couple of months now.





#include <iostream>
#include <iomanip>


using namespace std;

int main()
{
	// Remember to use getline(cin, letter) to limit.
	char letter;
	

	cout << "This program will convert upper and lower case letters into digits to a telephone " << endl;

	cout << "To end the program enter # "<< endl;
		
	cout << "Enter a letter: ";
	cin >> letter;
	cout << endl;
	
	
	while (letter != '#')
	{
		cout << "The letter you entered is: "
			<< letter << endl;

		cout << "The corresponding telephone "
			<< "digit is: ";
			
		if (letter >= 'A' && letter <= 'Z' || letter >= 'a' && letter <= 'z')
				switch(letter)	
			
					{
		case 'A':
		case 'a':
		case 'B':
		case 'b':
		case 'C':
		case 'c':
			cout << 2 << endl;
				break;
		case 'D':
		case 'd':
		case 'E':
		case 'e':
		case 'F':
		case 'f':
			cout << 3 << "-" << endl;
			    break;
		case 'G':
		case 'g':
		case 'H':
		case 'h':
		case 'I':
		case 'i':
			cout << 4 << endl;
				break;
		case 'J':
		case 'j':
		case 'K':
		case 'k':
		case 'L':
		case 'l':
			cout << 5 << endl;
				break;
		case 'M':
		case 'm':
		case 'N':
		case 'n':
		case 'O':
		case 'o':
			cout << 6 << endl;
				break;
		case 'P':
		case 'p':
		case 'Q':
		case 'q':
		case 'R':
		case 'r':
		case 'S':
		case 's':	
			cout << 7 << endl;
		case 'T':
		case 't':
		case 'U':
		case 'u':
		case 'V':
		case 'v':
			cout << 8 << endl;
				break;
		case 'W':
		case 'w':
		case 'X':
		case 'x':
		case 'Y':
		case 'y':
		case 'Z':
		case 'z':
			cout << 9 << endl;
				break;
			}
	
			else 
				cout << "Invalid output. " << endl;

	cout << " Continue by pressing another letter." << endl;
	
	cout << "To stop the program remember to input #." << endl;

	cout << "Enter a letter: ";
	cin >> letter;
	cout << endl;
	}
		
	
	return 0;
}


:code:

This post has been edited by jimblumberg: 23 March 2012 - 04:53 PM
Reason for edit:: Added missing Code Tags, Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: I could use advice on a if statement and getline

#2 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 60
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: I could use advice on a if statement and getline

Posted 23 March 2012 - 05:00 PM

For your problem, I have one piece of advice first/
Instead of that long switch statement, you could do something like this:

if((letter >= 'A' && letter <= 'C') || ((letter >= 'a' && letter <= 'c')){
//do stuff
}



also, instead of getline, you can also just use cin

#include <iostream>
#include <string>
using namespace std;

int main(){
        char temp;
        cin >> temp;
        cout << "you entered " << temp << endl;
}



This is a good first effort, keep at it!

This post has been edited by raspinudo: 23 March 2012 - 05:00 PM

Was This Post Helpful? 1
  • +
  • -

#3 gamerssz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-March 12

Re: I could use advice on a if statement and getline

Posted 23 March 2012 - 05:06 PM

That was helpful I didn't know about temp's yet but being ahead can't hurt too much and i've got the program to work thanks.
Was This Post Helpful? 0
  • +
  • -

#4 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 60
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: I could use advice on a if statement and getline

Posted 23 March 2012 - 05:15 PM

Temp is just a variable name I used, which stood for "temporary" because I only needed to print the variable and then re use it.

BTW if you found my post helpful, be sure to +1 it
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1