I am trying to grab an input file and outfile file from the user. I can grab the input file no problem, but Dev C++ just steps right over the following line...

cin.get(ofname, 150);          //grab filename from user

Here is some more of the code. Does anyone have any idea what I'm doing wrong?

#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
using namespace std;

int n=0, z=0;                   //declare variables to count the amount of input
int main () 
{
  char ifname[150];              //declare input file name array
  char ofname[150];              //declare output file name array
  char key[13];
  char c;
  char intxt[256];
  char tmptxt[5];                      
  ifstream is;                  //declare ifstream to is
  ofstream os;                  //declare ofstream to os
  
  cout<<endl<< "Enter the path of the input file: ";  //enter filename and path  cin<<ifname[0]<<endl;
  cin.get(ifname, 150);          //grab filename from user
  is.open(ifname);               //open file
  cout<<endl;
  
  cout<<endl<< "Enter the path of the output file: ";  //enter filename and path
  cin.get(ofname, 150);          //grab filename from user
  os.open(ofname);               //open file
  cout<<endl;

It could be that you step over it because you still have the enter pressed from the first question. try doing something like:

while(kbhit()){}

to wait for it to unpress enter (this function might require the header conio.h, but I'm not sure :P). At least make some code to give you time to release enter after the first question(sometimes might adding another cin.get(); work)

Thanks, but it didn't work. Any other ideas?:sad:

Here is my full code if anyone feels inclined to try it. The code is supposed to accept input via a file, encrypt it and then output the results on the screen and in a file.

The input file contains a key and a message like this...
(2,1,0,3,4)
thisclassisboring;
...the first line is the key and the second is the message.

I can't seem to be able to allow the user to enter the "output" file name.

Here is the code...

#include <iostream>
#include <fstream>
#include <conio.h>
#include <string>
using namespace std;

int n=0, z=0;                   //declare variables to count the amount of input
int main () 
{
  char ifname[150];              //declare input file name array
  char ofname[150];              //declare output file name array
  char key[13];
  char c;
  char intxt[256];
  char tmptxt[5];                      
  ifstream is;                  //declare ifstream to is
  ofstream os;                  //declare ofstream to os
  
  cout<<endl<< "Enter the path of the input file: ";  //enter filename and path  cin<<ifname[0]<<endl;
  cin.get(ifname, 150);          //grab filename from user
  is.open(ifname);               //open file
  cout<<endl;
 
    
  cout<<endl<< "Enter the path of the output file: ";  //enter filename and path
  cin.get(ofname, 150);          //grab filename from user
  os.open(ofname);               //open file
  cout<<endl;
//
//*******************get key
//
  for (int j=0; j<13; j++)
    {
        c=is.get();  
        key[j]=c;
    }       
//  cout<<"This is the key:  ";
  for (int k=0; k<12; k++)
    cout<<key[k];
//    cout<<endl;
    is.clear();                   //clear flags in ifstream
    is.seekg (13, ios::beg);      //set pointer to second line of input file
//
//*********************find out how long the message is
//
    for (int j=0; j<75; j++)
    {
        c=is.get();
        if (c!=';')
          intxt[j]=c;
        if (c==';')
          break;
        n++;
        z=j;
    } 
/*    cout<<"n=  "<<n<<endl;
    cout<<"This is the message  ";
    for (int l=0; l<n; l++)
        cout<<intxt[l]; 
    cout<<endl;   */
//
//***************************pad the message
//
if (n<5)
{
    do
    {
        z++;
        intxt[z]='x';
        n++;        
    }while(n<5);  
}
else if (n<10)
{
    do
    {
        z++;
        intxt[z]='x';
        n++;        
    }while(n<10);  
}
else if (n<15)
{
    do
    {
        z++;
        intxt[z]='x';
        n++;        
    }while(n<15);  
} 
else if (n<20)
{
    do
    {
        z++;
        intxt[z]='x';
        n++;        
    }while(n<20);  
}
else if (n<25)
{
    do
    {
        z++;
        intxt[z]='x';
        n++;        
    }while(n<25);  
}  
else if (n<30)
{
    do
    {
        z++;
        intxt[z]='x';
        n++;        
    }while(n<30);  
}
else if (n<35)
{
    do
    {
        z++;
        intxt[z]='x';
        n++;       
    }while(n<35);  
}   
else if (n<40)
{
    do
    {
        z++;
        intxt[z]='x';
        n++;       
    }while(n<40);  
} 
//
//**************************ENCRYPT / DCRYPT
//
for (int r=0; r<n; r=r+5)
   {
    for (int p=0, s=r; p<5, s<r+5; p++, s++)
      tmptxt[p]=intxt[s];    
      for (int p=1; p<10; p=p+2)
      {
          if (key[p]=='0')
          {
            cout<<tmptxt[0];
            os<<tmptxt[0];
          }    
          if (key[p]=='1')
          {
            cout<<tmptxt[1];
            os<<tmptxt[1];
          }    
          if (key[p]=='2')
          {
            cout<<tmptxt[2];
            os<<tmptxt[2];
          }    
          if (key[p]=='3')
          {
            cout<<tmptxt[3];
            os<<tmptxt[3];
          }    
          if (key[p]=='4')
          {
            cout<<tmptxt[4];
            os<<tmptxt[4];
          }    
      }    
    }         
  cout<<';';                  //add the end of file character as required 
  getch();                    //pause
  return 0;
}

Did you tried cin.ignore after cin.get(ifname, 150); ?

The second line cin.get() is step over are most probably due to the error that always arise when you use cin.get() more than once. You can try to solve the problem by inserting cin.ignore(); in between the coding consist of first cin.get() and the second cin.get(). cin.ignore() is use to terminate characters or buffer of input. You can search for the cin.ignore() in the Internet to uderstand more about the usage.

Hope it helps.

AWESOME!

cin.ignore(); after the first cin.get() worked! I was going crazy. Thanks a lot!

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.