Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,128 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,035 people online right now. Registration is fast and FREE... Join Now!




reading from input file and converting roman numerals to decimal and v

 
Reply to this topicStart new topic

reading from input file and converting roman numerals to decimal and v, need help!!

complexity
post 7 Oct, 2008 - 09:52 AM
Post #1


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 6

Basically i am new to C++ and i've got a project which requires me to prompt user to enter a filename and to read and convert the input from roman numeral to decimal number and vice versa.
i have problems getting my programme to allow me to enter my filename again after showing the file cannot be found sentence.And i didn't know how to start with my conversion of roman numeral to decimal number and vice verse.
could someone please help me?

here's what i have done up to now
CODE

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;


int main()
{
    ifstream myfile;
    string filename;
    string a;
    int b=1;
    
    
    cout << "Please enter filename: ";
    cin >> filename;
    
    myfile.open(filename.c_str());


    
    if (!myfile.good())
    {
        cout << "File cannot be found" << endl;
        system("pause");
        
        b++;
    }


    
    if (b<1)
    {
    cout << "Please enter filename again: ";
    system ("pause");
    cin >> filename;
  
    
    myfile.open(filename.c_str());
    return 0;
    }



    while(!myfile.eof())
    {
        // use getline to read entire line  
        getline(myfile, a);
        
        cout << a << endl;
    }
    
    myfile.close();    
    system("pause");
    return 0;
}

User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 7 Oct, 2008 - 10:53 PM
Post #2


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


You might be expecting something like this.

cpp

do
{
cout << "Please enter filename: ";
cin >> filename;
myfile.open(filename.c_str());
if (!myfile.good())
{
cout << "File cannot be found" << endl;
}
}while(!myfile.good());


I can't tell you anything about conversion. Please tell us what you have tried on conversion so far .
There might be a snippet on this topic in snippets section. you can try and search that.

I hope this will help you. smile.gif
User is offlineProfile CardPM

Go to the top of the page

complexity
post 8 Oct, 2008 - 01:47 AM
Post #3


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 6

QUOTE(AmitTheInfinity @ 7 Oct, 2008 - 11:53 PM) *

You might be expecting something like this.

cpp

do
{
cout << "Please enter filename: ";
cin >> filename;
myfile.open(filename.c_str());
if (!myfile.good())
{
cout << "File cannot be found" << endl;
}
}while(!myfile.good());


I can't tell you anything about conversion. Please tell us what you have tried on conversion so far .
There might be a snippet on this topic in snippets section. you can try and search that.

I hope this will help you. smile.gif


hey thx for ur help!! and really sry for the double posting.
this is what i needed but there seems to be a problem when i run it. it seems that i cant get out of the loop even when i enter then correct filename the 2nd time and so on..
this is my code after adjustment..
CODE

#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;


int main()
{
    ifstream myfile;
    string filename;
    string a;
    


do
{
    cout << "Please enter filename: ";
    cin >> filename;    
    myfile.open(filename.c_str());    
    if (!myfile.good())
    {
        cout << "File cannot be found" << endl;
    }        
}while(!myfile.good());
    


    while(!myfile.eof())
    {
        // use getline to read entire line  
        getline(myfile, a);
        
        cout << a << endl;
    }
    
    myfile.close();    
    system("pause");
    return 0;
}
    
User is offlineProfile CardPM

Go to the top of the page

David W
post 8 Oct, 2008 - 07:28 PM
Post #4


D.I.C Regular

Group Icon
Joined: 20 Sep, 2008
Posts: 306



Thanked 15 times

Dream Kudos: 250
My Contributions


Here is a 'shell' to get you started then ...

Hint: You could use the alpha to integer function call ...

atoi( nStr.substr(i,1).c_str() )

<Note: the .c_str() at the end coverts the C++ sub-string to a C string
that is required for the 'atoi( cStr)' function call ... the atoi(cStr) call
returns an 'int'>

to get a single integer digit from a string of digits in string 'nStr' at index 'i'

CODE

/*
    Demo of converting DECIMAL NUMERALS to ROMAN NUMERALS ...
    Modified for strings, and to use the MSD, (Most Significant Digit)
    rather than the LSD (Least Significant Digit) method used by Randy.
    Randall Hyde is the author of HLA (High Level Assembly) ... See ...
    http://webster.cs.ucr.edu/AoA/index.html
    http://developers-heaven.net/forum/index.php/topic,46.0.html
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>

#define debugging true

using namespace std;

string convert( int n )
{
    if( n > 3999 ) return "Largest number handled is 3999";
    if( n < 1 ) return "Smallest number handled is 1";
    
    string romanNum[] =
    { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
    char buffer[20];
    itoa( n, buffer, 10 ); // convert integer to C string char's
    string nStr = string(buffer); // convert C string to C++ string
    string rm =""; // to hold the Roman Number
    int msd;
    for( int i=0; i<nStr.length(); ++i )
    {
        // Extract the NEXT Most Significant Digit (MSD)
        //  from the number 'n' ... as a string now, i.e. 'nStr'

        //  msd =  ???? ...................................
        
        // Here is the trick, (from Randy of HLA), that makes this work ...
        // 'Multiply' the Roman Number by Ten.  This is achieved by
        // swapping the characters in the char string 's1' below
        // with their corresponding character in char string 's2' below ...
        char s1[] = "IVXLCDM";
        char s2[] = "XLCDM**";
        int j, k;
        for( j=0; j< rm.length(); j++ )
        {
            for( k=0; k<7; k++ )
            {
                if( rm[j] == s1[k] )
                {
                    rm[j] = s2[k];
                    break;
                }
            }
        }
        // Convert the current MSD to a string
        // and append to the end of "rm".
        rm = rm + romanNum[msd];
    }
   return rm;      
}

void convertAndShow( int n )
{
    cout << "The number " << n << " converted is " << convert(n) << endl;
}

int main()
{
    // get file name and open that file to read numbers
    // and validate that it was opened ok ...
    ifstream myfile;
    string filename;
    
// or for testing via keyboard input ...
#if debugging
    goto testing;
#endif

    do
    {
        cout << "Please enter filename: ";
        myfile.clear();
        cin >> filename;
        cin.sync(); //eat any char's that cin may have missed in in-stream  
        myfile.open( filename.c_str() );    
        if( myfile.fail() )
            cout << "File cannot be found" << endl;    
    }while( myfile.fail() );

    int a;
    while( myfile >> a )
        convertAndShow( a );
        
    myfile.close();

#if debugging  
    testing:
    string message = "Enter number from the keyboard (q to quit) : ";
    cout << message;
    while( cin >> a ) { convertAndShow( a ); cout << message; }
    cin.clear();
    cin.sync();
#endif

    cout << "\nPress 'Enter' to continue ... ";
    cin.get();
}



This post has been edited by David W: 8 Oct, 2008 - 07:53 PM
User is offlineProfile CardPM

Go to the top of the page

complexity
post 10 Oct, 2008 - 08:27 AM
Post #5


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 6

i have made some adjustment and started on my conversion from roman to decimal but the code doesnt seem to work can any 1 help?

CODE

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cctype>

using namespace std;


int main()
{
     ifstream myfile;
     string filename;
     string a;
     char romanType;
     int convert;
  
    

    do
    {
      cout << "Please enter filename: ";
      cin >> filename;    
      myfile.open(filename.c_str());    
      if (!myfile.good())
     {
        cout << "File cannot be found" << endl;
     }        
    }while(!myfile.good());
    

    while (!myfile.eof())
    {
     getline(myfile,a);
     for(int i=0; i<a.length(); i++)
     a[i] = toupper (a[i]);    
/*     for(int i=0; i<a.length(); i++)
    {
     if (!isdigit (a[i]))
  
     cout << a << " invalid " << endl;
        
    else
    {
         cout << a << endl;
    }
    }
*/  
    
     }
     int M, D, C, L, X, V, I, IV, IX, XL, XC, CD, CM;
     char roman;
     romanType::romanType(char &ch)
     {
      M = 1000;
      D = 500;
      C = 100;
      L = 50;
      X = 10;
      V = 5;
      I = 1;
      IV = 4;
      IX = 9;
      XL = 40;
      XC = 90;
      CD = 400;
      CM = 900;

      
      cout << ch << endl;
      
      roman=ch;    
      
      }
       int romanType::convert{
    if (roman == 'M'){
        return 1000;                  
    }else if(roman == 'D'){
        return  500;
    }else if(roman == 'C'){
        return  100;
    }else if(roman == 'L'){
        return  50;
    }else if(roman == 'X'){
        return  10;
    }else if(roman == 'V'){
        return  5;
    }else if(roman == 'I'){
        return  1;
    }else if(roman == 'IV'){
        return  4;
    }else if(roman == 'IX'){
        return  9;
    }else if(roman == 'XL'){
        return  40;
    }else if(roman == 'XC'){
        return  90;
    }else if(roman == 'CD'){
        return  400;
    }else if(roman == 'CM'){
        return  900;
    }
    return 0;  
}
    

    
    myfile.close();    
    system("pause");
    return 0;
}
    
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 11:43AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month