954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

What is wrong with this code?

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;
compshooter
Newbie Poster
21 posts since Feb 2005
Reputation Points: 18
Solved Threads: 0
 

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)

Anonymusius
Posting Whiz in Training
238 posts since Aug 2006
Reputation Points: 129
Solved Threads: 11
 

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

compshooter
Newbie Poster
21 posts since Feb 2005
Reputation Points: 18
Solved Threads: 0
 

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;
}
compshooter
Newbie Poster
21 posts since Feb 2005
Reputation Points: 18
Solved Threads: 0
 

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

andor
Posting Whiz in Training
276 posts since Jun 2005
Reputation Points: 251
Solved Threads: 29
 

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.

rinoa04
Junior Poster in Training
84 posts since Sep 2006
Reputation Points: 52
Solved Threads: 4
 

AWESOME!

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

compshooter
Newbie Poster
21 posts since Feb 2005
Reputation Points: 18
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You