how to go about my home work v3

Discussion in 'OT Technology' started by flynfrog, Mar 31, 2003.

  1. flynfrog

    flynfrog Cool isnt Cheap

    Joined:
    Jul 16, 2001
    Messages:
    6,621
    Likes Received:
    6
    Location:
    Utah
    You have been hired to write a C++ program which will read a file of telephone numbers, convert them to numeric and write the converted phone numbers out to another file. The new file will only have numbers with no dashes or spaces.



    For Example:

    In: Out:



    BU8-1823 2881823

    Auto Car 2886227

    1-quick-stops 17842578677



    For grading purposes you may either assume the input file will be called phone.txt, or ask the user what file to use. PLEASE DO NOT HARD CODE A DIFFERENT INPUT FILE NAME.


    what is the best way to enter the file in so i can do this im planing on using a loop to go from digit to digit then out put it but on teh out put how do i tell it when to strat a new line thanks in advance
     
  2. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    I'm not sure what you are asking...

    I would create a hash that is referenced by letters A-Z, each letter would correspond to a number. So when you would give the hash %myHash(A) it would return 2 or whatever number A corresponds to in your case. %myHash(U) would return 8.

    As far as using the input file, just use the first argument of the program as the input file name. That seems easiest, but it looks like the problem statement says you can assume the file is called "phone.txt" and leave it at that.

    Hope this helps.
     
  3. flynfrog

    flynfrog Cool isnt Cheap

    Joined:
    Jul 16, 2001
    Messages:
    6,621
    Likes Received:
    6
    Location:
    Utah
    i figgured my some else if statemtns to asighn the number to the char i jsut dont knwo how to make it out put ot the file corectly so that its formated
     
  4. flynfrog

    flynfrog Cool isnt Cheap

    Joined:
    Jul 16, 2001
    Messages:
    6,621
    Likes Received:
    6
    Location:
    Utah
    also im not sure how to make it move form on cahr to the next

    im a newb
     
  5. flynfrog

    flynfrog Cool isnt Cheap

    Joined:
    Jul 16, 2001
    Messages:
    6,621
    Likes Received:
    6
    Location:
    Utah
    this is cpp btw
     
  6. flynfrog

    flynfrog Cool isnt Cheap

    Joined:
    Jul 16, 2001
    Messages:
    6,621
    Likes Received:
    6
    Location:
    Utah
    THis is what i ahve so far
    this is the input file
    http://www.cs.iastate.edu/~cs207/spring03/hw/phone.txt

    now i jsut need help debugging
    Code:
    //RYAN ELLIS
    //rellis
    //Mike
    //E2
    //Phone number
    //this program converts phone numbers form a file and outputs them into another file
    
    #include <iostream.h>
    #include <math.h>
    #include <fstream.h>
    
    int main (void)
    {
    ifstream inputfile;
    ofstream outputfile
     while !inputfile.eof
      {
       inputfile.open("phone.txt");
       outputfile.open("outfile.txt")
       inputfile.get(ch)
        if(ch == 1)
          {
          outputfile << 1;
          }
         	 else if (ch == 2||ch == a||ch == b||ch == c||ch == A||ch == B||ch == C)
           {
           outputfile << 2;
           }
            else if(ch == 3||ch == d||ch == e||ch == f||ch == D||ch == E||ch == F)
             {
             outputfile << 3;
             }
              else if (ch == 4||ch == g||ch == h||ch == i||ch == G||ch == H||ch == I)
               {
                 outputfile << 4;
                }
                else if (ch == 5||ch == j||ch == k||ch == l||ch == J||ch == K||ch == L)
                 {
                  outputfile << 5;
                  }
                  else if (ch == 6||ch == m||ch == n||ch == o||ch == M||ch == N||ch == O)
                     {
                      outputfile << 6;
                     }
                      else if (ch == 7||ch == p||ch == q||ch == r||ch == s||ch == P||ch == Q||ch == R||ch == S)
                       {
                       outputfile << 7;
                       }
                       else if (ch == 8||ch == t||ch == u||ch == v||ch == T||ch == U||ch == V)
                        {
                         outputfile << 8;
                        }
                         else if (ch == 9||ch == w||ch == x||ch == y||ch == z||ch == W||ch == V||ch == X ||ch == Y||ch == Z)
                          {
                           outputfile << 9;
                          }
                           else if(ch == 0)
                            {
                            outputfile << 0
                            }
       outputfile.close()
       inputfile.close()
    } return 0;
    
     
  7. flynfrog

    flynfrog Cool isnt Cheap

    Joined:
    Jul 16, 2001
    Messages:
    6,621
    Likes Received:
    6
    Location:
    Utah
    Code:
    //RYAN ELLIS
    //rellis
    //Mike
    //E2
    //Phone number
    //this program converts phone numbers form a file and outputs them into another file
    
    #include <iostream.h>
    #include <fstream.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    #define FILE_ERROR -1
    //#define inputfile (phone.txt)
    //#define outputfile (out.txt)
    int main (void)
    {
    char ch;
    ifstream inputfile;
    ofstream outputfile;
     if ( inputfile.fail() )
    	{
    		cerr << "Could not open input file: " << inputfile<< endl;
          exit(-1);
       }
       inputfile.open("phone.txt");
       outputfile.open("out.txt");
    
     ch = inputfile.peek();
     while (isspace (ch) && !inputfile.eof)
    
      {
    
       inputfile.get(ch);
        if(ch == 1)
          {
          outputfile << 1;
          }
         	 else if (ch == 2 || ch == 'a' || ch == 'b'|| ch == 'c' || ch == 'A' || ch == 'B' || ch == 'C')
           {
           outputfile << 2;
           }
            else if(ch == 3 || ch == 'd' || ch == 'e' || ch == 'f' || ch == 'D' || ch == 'E' || ch == 'F')
             {
             outputfile << 3;
             }
              else if (ch == 4 || ch == 'g' || ch == 'h' || ch == 'i' || ch == 'G' || ch == 'H '|| ch == 'I')
               {
                 outputfile << 4;
                }
                else if (ch == 5 || ch == 'j' || ch == 'k' || ch == 'l' || ch == 'J' || ch == 'K' || ch == 'L')
                 {
                  outputfile << 5;
                  }
                  else if (ch == 6 || ch == 'm'|| ch == 'n' || ch == 'o' || ch == 'M' || ch == 'N' || ch == 'O')
                     {
                      outputfile << 6;
                     }
                      else if (ch == 7 || ch == 'p' || ch == 'q' || ch == 'r' || ch == 's' || ch == 'P' || ch == 'Q'|| ch == 'R' || ch == 'S')
                       {
                       outputfile << 7;
                       }
                       else if (ch == 8 || ch == 't' || ch == 'u' || ch == 'v' || ch == 'T' || ch == 'U' || ch == 'V')
                        {
                         outputfile << 8;
                        }
                         else if (ch == 9 || ch == 'w' || ch == 'x' || ch == 'y' || ch == 'z' || ch == 'W' || ch == 'V' || ch == 'X' || ch == 'Y' || ch == 'Z')
                          {
                           outputfile << 9;
                          }
                           else if(ch == 0)
                            {
                            outputfile << 0;
                            }
       outputfile.close();
       inputfile.close();
    } return 0;
    
    
    im calling the file wrong some how
     
  8. flynfrog

    flynfrog Cool isnt Cheap

    Joined:
    Jul 16, 2001
    Messages:
    6,621
    Likes Received:
    6
    Location:
    Utah
    Code:
    //RYAN ELLIS
    //rellis
    //Mike
    //E2
    //Phone number
    //this program converts phone numbers form a file and outputs them into another file
    
    #include <iostream.h>
    #include <fstream.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    //#define FILE_ERROR -1
    //#define inputfile (phone.txt)
    //#define outputfile (out.txt)
    int main (void)
    {
    char ch;
    ifstream inputfile;
    ofstream outputfile;
    
     inputfile.open("phone.txt");
     outputfile.open("out.txt");
     if ( inputfile.fail() )
    	{
    	  	cerr << "Could not open input file: " << inputfile<< endl;
          exit(-1);
       }
    
    
     ch = inputfile.peek();
     while (isspace (ch) && !inputfile.eof());
    
      {
    
       inputfile.get(ch);
        if(ch == 1)
          {
          outputfile << 1;
          }
         	 else if (ch == 2 || ch == 'a' || ch == 'b'|| ch == 'c' || ch == 'A' || ch == 'B' || ch == 'C')
           {
           outputfile << 2;
           }
            else if(ch == 3 || ch == 'd' || ch == 'e' || ch == 'f' || ch == 'D' || ch == 'E' || ch == 'F')
             {
             outputfile << 3;
             }
              else if (ch == 4 || ch == 'g' || ch == 'h' || ch == 'i' || ch == 'G' || ch == 'H'|| ch == 'I')
               {
                 outputfile << 4;
                }
                else if (ch == 5 || ch == 'j' || ch == 'k' || ch == 'l' || ch == 'J' || ch == 'K' || ch == 'L')
                 {
                  outputfile << 5;
                  }
                  else if (ch == 6 || ch == 'm'|| ch == 'n' || ch == 'o' || ch == 'M' || ch == 'N' || ch == 'O')
                     {
                      outputfile << 6;
                     }
                      else if (ch == 7 || ch == 'p' || ch == 'q' || ch == 'r' || ch == 's' || ch == 'P' || ch == 'Q'|| ch == 'R' || ch == 'S')
                       {
                       outputfile << 7;
                       }
                       else if (ch == 8 || ch == 't' || ch == 'u' || ch == 'v' || ch == 'T' || ch == 'U' || ch == 'V')
                        {
                         outputfile << 8;
                        }
                         else if (ch == 9 || ch == 'w' || ch == 'x' || ch == 'y' || ch == 'z' || ch == 'W' || ch == 'V' || ch == 'X' || ch == 'Y' || ch == 'Z')
                          {
                           outputfile << 9;
                          }
                           else if(ch == 0)
                            {
                            outputfile << 0;
                            }
        }
    
       outputfile.close();
       inputfile.close();
    
     return 0;
     }
    
    
    it compiles now but it only does the first number what is stopping it form going on
     
  9. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    I'm not sure what you're trying to accomplish with the peek(ch) and testing for isspace(ch), but all you need for your main loop is while (!inputfile.eof())...

    Also, you're making a mistake in your if statements when checking for numerical characters. You need to put single quotes around the number since you're reading characters not values (if (ch == '1') not if (ch == 1)).

    You're also not accounting for the newline. When you read a newline character, you should output a newline character so your numbers don't run on in one big line.

    Here's a tip: you can consolidate the check for '0', '1', and '\n' with one if statement by doing this...

    if (ch == '0' || ch == '1' || ch == '\n')
    {
    outputfile << ch;
    }
     

Share This Page