| | |
What is wrong with this code?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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...
Here is some more of the code. Does anyone have any idea what I'm doing wrong?
C++ Syntax (Toggle Plain Text)
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?
C++ Syntax (Toggle Plain Text)
#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:
to wait for it to unpress enter (this function might require the header conio.h, but I'm not sure
). At least make some code to give you time to release enter after the first question(sometimes might adding another cin.get(); work)
C++ Syntax (Toggle Plain Text)
while(kbhit()){}
). At least make some code to give you time to release enter after the first question(sometimes might adding another cin.get(); work) Last edited by Anonymusius; Sep 25th, 2006 at 10:50 am. Reason: Forgot last bracket
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...
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...
C++ Syntax (Toggle Plain Text)
#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; }
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
Hope it helps.
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.
![]() |
Similar Threads
- Can anyone figure out what is wrong in my program? (C++)
- What did I do wrong? (C++)
- Need help! With changing an equation into code and file opening (C++)
- Please help with login page code behind (ASP.NET)
- please help me!!been trying to trace where ive gone wrong!!! (C)
- Script stores data in wrong MySQL columns (MySQL)
- wrong output (C++)
- What is wrong wtih my css? (JavaScript / DHTML / AJAX)
- merged:nesting loops (C++)
Other Threads in the C++ Forum
- Previous Thread: API Help
- Next Thread: Help me simplify this program please!
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





