I have tried to structure my program but im stuck at this point. I have a file that contains phone phrases on each line (1-900-WINNING) is one of the phrases, if i get more digits than a phone number requires i must ignore the remainder. This data is coming from a file. Im just not understanding it, maybe i shouldn't read my data in as a string? . I am suppose to use a switch statement in my solution. I was thinking about doing a myFile.get(x) but i just dont understand how to put that into a string. Sometimes i understand it by seeing it done. please help.

the output should look like this
1-800-Free-Willy -> 1-800-373-3945

heres my program so far

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

int main()
{
ifstream inFile;
string phrase;
char x;

inFile.open("abc.txt");

inFile >> phrase;

while(inFile)
{

inFile.get(x);
cout << setw(17) << phrase << "-> ";
cout << endl;


if (( x >= 'A' && x <= 'Z') || ( x >= 'a' && x <= 'z') || ( x >= 0 && x <= 9 ))
{
switch (x)
{
case 'A':
case 'a':
case 'B':
case 'b':
case 'C':
case 'c':
case '2':
cout << "2";
break;

case 'D':
case 'd':
case 'E':
case 'e':
case 'F':
case 'f':
case '3':
cout << "3";
break;

case 'G':
case 'g':
case 'H':
case 'h':
case 'I':
case 'i':
case '4':
cout << "4";
break;

case 'J':
case 'j':
case 'K':
case 'k':
case 'L':
case 'l':
case '5':
cout << "5";
break;

case 'M':
case 'm':
case 'N':
case 'n':
case 'O':
case 'o':
case '6':
cout << "6";
break;

case 'P':
case 'p':
case 'Q':
case 'q':
case 'R':
case 'r':
case 'S':
case 's':
case '7':
cout << "7";
break;

case 'T':
case 't':
case 'U':
case 'u':
case 'V':
case 'v':
case '8':
cout << "8";
break;

case 'W':
case 'w':
case 'X':
case 'x':
case 'Y':
case 'y':
case 'Z':
case 'z':
case '9':
cout << "9";
break;

}



}

}



system("pause");
return 0;
}

How are the numbers stored in the file? One per line? If that is the case you might want to use the getline function for your while loop control

string number;
// open file
while(getline(inFile, number))
{
    nuber.resize(14); // gets rid of any extra letters
    for (size_t i = 0; i < number.size)
    {
        switch (number[i])
        {
            //...
        }
    }
}
//...

You probably want to add a case for "-" so that it is also outputted.

yes one per line

i also dont understand what number.resize(14) does. i dont think i've learned that yet.
this is what ive been hinted but i dont know how to put it into code

Don't read in a string. Do initialize new_str = "";

Read in a character using .get and out put it.
figure out what the digit should be.
new_str = new_str + character_digit ('0', '1', etc)

Repeat.

When you get to ' ', output new_string.

resize is a string function that will change the size of the string to the size passed to it. I used this to change the size to 14 so that any extra digits in the phone number would be erased. Basically if you had 1-800-Free-Willy it would change it to 1-800-Free-Wil.

Personally I would read the file one line at a time to a string and convert that string to a c-string ( char array ). A char array would be ideal for this.

After you read phrase and output it you throw it away and start reading the file again.

Instead, just look at each character in phrase and use each character in your switch. You also need to loop on the characters in phrase, not inputting to the end of the file.

ok I have my code working but i have a couple problems to be sorted out, hopefully you guys can help. The first problem is a phone number is suppose to only be a certain length but some file are longer than what a real phone number is, if you count all the char including the 1 and - it should be 14 characters long, but some numbers in the file dont have a 1 or any -. I cannot for the life of me figure out to make it work. And the second problem is I currently have my - reading out in the spot they are read in, but i want to make it nice format and have a - like this 734-878-4876, for every output. I have what the output looks like commented out at the bottom of my code

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

int main()
{
	ifstream inFile;
	string phrase = "";
	char x;					//Will read a character off of the the string phrase

	inFile.open("mp4.txt");

	while(!inFile.eof())
	{	
		getline(inFile, phrase);
		cout << left << setw(17) << phrase << "-> ";
				
		for(int i = 0; i < phrase.length(); i++)
		{
			x = phrase.at(i);
			
				switch (x)
				{
					case '-':
					cout << "-";
					break;

					case '1':
					cout << 1;
					break;

					case 'A':
					case 'a':
					case 'B':
					case 'b':
					case 'C':
					case 'c':
					case '2':
					cout << "2";
					break;

					case 'D':
					case 'd':
					case 'E':
					case 'e':
					case 'F':
					case 'f':
					case '3':
					cout << "3";
					break;

					case 'G':
					case 'g':
					case 'H':
					case 'h':
					case 'I':
					case 'i':
					case '4':
					cout << "4";
					break;

					case 'J':
					case 'j':
					case 'K':
					case 'k':
					case 'L':
					case 'l':
					case '5':
					cout << "5";
					break;

					case 'M':
					case 'm':
					case 'N':
					case 'n':
					case 'O':
					case 'o':
					case '6':
					cout << "6";
					break;

					case 'P':
					case 'p':
					case 'Q':
					case 'q':
					case 'R':
					case 'r':
					case 'S':
					case 's':
					case '7':
					cout << "7";
					break;

					case 'T':
					case 't':
					case 'U':
					case 'u':
					case 'V':
					case 'v':
					case '8':
					cout << "8";
					break;

					case 'W':
					case 'w':
					case 'X':
					case 'x':
					case 'Y':
					case 'y':
					case 'Z':
					case 'z':
					case '9':
					cout << "9";
					break;

					case '0':
						cout << "0";
					break;
				}
		}
	cout << endl;
	}

	cout << endl;

	system("pause");
	return 0;
}

//1-888-DOG-BITE   -> 1-888-364-2483
//FAST-CASH-NOW    -> 3278-2274-669
//1-800-Free-Willy -> 1-800-3733-94559
//1-900-WINNING    -> 1-900-9466464
//1-800-IAMALOSER  -> 1-800-426256737
//TALKWITHJOE      -> 82559484563
//1-GOBLUE2010     -> 1-4625832010
//CHOCOLATE-4-U    -> 246265283-4-8
//
//Press any key to continue . . .

You have to look at the phone number character by character and add or remove characters to put it in the format you want. One suggestion is to

start at the end of the number
-- make sure there are 4 digits
-- make sure there's a -
-- then 3 digits
-- another hyphen
etc...

ok how would i format it so they all have the same amount of characters a regular phone number has (with or without a 1) wouldn't I have to figure that out first then put in a "-". because some of the strings ending character should be cut off if their too long.

i have tried phrase.resize() and that didnt do what i want as some numbers have 1- and some dont so it throws everything off

ok how would i format it so they all have the same amount of characters a regular phone number has (with or without a 1) wouldn't I have to figure that out first then put in a "-". because some of the strings ending character should be cut off if their too long.

You need to think about it. Sit at a desk with pencil and paper and do it.
Write down the steps you take to get the first phone number in the proper format.
Do the next phone number following the steps, adding more steps as you need.
When your steps can handle all the possibilities, put those steps into code.

i have tried phrase.resize() and that didnt do what i want as some numbers have 1- and some dont so it throws everything off

So? Since we didn't suggest it because we knew it wouldn't work... :icon_wink:

i sat down for 2 hours today with a tutor and couldnt get it to work. I was telling you what works and what doesnt, so maybe you could suggest something else. Im not an expert this is an INTRO class. And btw and when you say "we" didnt suggest it, what do you mean because if you look at the the second post in this thread i can clearly see someone posted .resize(), so maybe suggest something that does work. I came on this site for help and you surely didnt give me any. If i knew how to do it i wouldnt be on here asking

I was telling you what works and what doesnt, so maybe you could suggest something else.

So doing the process by hand and understanding the steps didn't work at all in the 20 minutes from my post to yours. Maybe more time would have helped.

Im not an expert this is an INTRO class.

Duh. I think we know that, otherwise you wouldn't have a problem with an easy task. I'm not being snide, this is an easy task and anyone that can't do it is obviously a beginner and hasn't yet grasped the techniques. I was there once, too.

And btw and when you say "we" didnt suggest it, what do you mean because if you look at the the second post in this thread i can clearly see someone posted .resize(),

Sorry, for some reason I didn't remember reading that post three days ago. And since I knew it was bad advice, I surely blanked it out.

so maybe suggest something that does work. I came on this site for help and you surely didnt give me any.

Actually, I have. You just didn't get it. Yet. So here's another try:

1) Copy all digits (and only the digits) from the phone number into another temp string.
2) Test the length of the temp string. Is it OK? If not, do whatever you're supposed to do.
3) Clear the original string.
4) Is the first character in temp a 1? If so, move it back to original and add a '-'. If not, load "1-" instead.
5) Move the next 3 digits and add '-'
6) Move the next 3 digits and add '-'
7) Move the next 4 digits
Done.

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.