Simple problem for you guys - school proj

This is a discussion on Simple problem for you guys - school proj within the C++ Programming forums, part of the General Programming Boards category; Hello, I am having trouble finishing this code, It converts whatever seven letters you type into the corresponding numbers on ...

  1. #1
    ryancsutton
    Guest

    Simple problem for you guys - school proj

    Hello, I am having trouble finishing this code, It converts whatever seven letters you type into the corresponding numbers on a phone. example A = "2" D = "3" G="4" ect. So It prompts a user for seven letter and it then outputs the phone number from those numbers. Everything works fine except I can't figure out how to put a dash after the third number. My output looks like this-- 5558888 I need it to output 555-8888. I also need it to print out a "#" if a lowercase letter is inputed. Thanks in advance for any help. Here is my code....
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	int dum = 0;
    
    	string input;
    	string bleh;
    	string str2 = "ABC", str3 = "DEF", str4 = "GHI",
    str5 = "JKL";
    	string str6 = "MNO", str7 = "PQRS", str8 = "TUV",
    str9 = "WXYZ";
    
    
    	cout << "Please enter a Seven letter word, all uppercase ";
    	getline(cin, input);
    
    	if (input.length() !=7)
    	{
    		cout << "The word you typed in is not seven characters in length";
    	return 1;
    	}
    
    
    	while (dum <= 6)
    	{
    	bleh = input.substr(dum,1);
    
    	if (str2.find(bleh) != string::npos)
    	{
    	cout << "2";
    
    	}
    	else if (str3.find(bleh) != string::npos)
    	{
    	cout << "3";
    
    	}
    
    	else if (str4.find(bleh) != string::npos)
    	{
    	cout << "4";
    	}
    	else if (str5.find(bleh) != string::npos)
    	{
    	cout << "5";
    	}
    	else if (str6.find(bleh) != string::npos)
    	{
    	cout << "6";
    	}
    	else if (str7.find(bleh) != string::npos)
    	{
    	cout << "7";
    	}
    	else if (str8.find(bleh) != string::npos)
    	{
    	cout << "8";
    	}
    	else if (str9.find(bleh) != string::npos)
    	{
    	cout << "9";
    	}
    
    	dum++;
    
    	
    }
    	return 0;
    }
    [edit]Code tags added by Hammer. Please use them in future.

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    u just need a cout after the third number like this:

    cout <<"-";

    that would go after the third and before the 4th

  3. #3
    ryancsutton
    Guest

    Unhappy ?

    I'm not following you, put it after the third number? I know that is what I want to do, however the third number changes every time so how do I tell it to put a dash in the 4th space?

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    After the if else statements that check what number to put, and before the dum++ add these lines:

    Code:
    if(dum == 2){
    
    cout << "-";
    
    }
    So that will output a "-" when dum is equal to 2, which is the third number.

  5. #5
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    in your output of the third number u want it to be

    cout<<"number here"<<"-";

    even if your number chnages the "-" will always be there with it....that more clear?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Welcome to the world of parsing! ha. My personal favorite pastime...

    Code:
    
    char *ToTele(char input[]){
    int i, j;
    int length = strlen(input);
    char temp[length+1];
    
     for(i = 0, j = 0; i < length; i++, j++) {
      if(i == 3) {
        temp[j] = '-';
        j++;
        continue;
        }
      if(input[i] >= 'a' && input[i] <= 'z') {
        temp[j] = '#'; 
        }
      else {
      switch(input[i]) {
      case 'A': case 'B': case 'C':
      temp[j] = '1';
      break;
      //...continue like so...
      default:
       i++;
       continue;
      }
     }
     temp[j] = 0;
    
     return strcpy(input, temp);
    }
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    ryancsutton
    Guest
    ahhh thank you I only had one "=" in my dum == 2 line, thanks

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Sebastiani, your code is not very portable.

    Code:
    int length = strlen(input);
    char temp[length+1];
    should be rewritten using new
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple C code problem
    By neo28 in forum C Programming
    Replies: 24
    Last Post: 05-16-2009, 10:48 AM
  2. Simple Variable Problem
    By Cthulhu in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2005, 03:07 PM
  3. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 05:41 PM
  4. Simple boolean problem
    By larry in forum C++ Programming
    Replies: 9
    Last Post: 10-13-2001, 08:49 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21