What is wrong with this code?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2005
Posts: 21
Reputation: compshooter is an unknown quantity at this point 
Solved Threads: 0
compshooter's Avatar
compshooter compshooter is offline Offline
Newbie Poster

What is wrong with this code?

 
1
  #1
Sep 25th, 2006
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...

  1. 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?





  1. #include <iostream>
  2. #include <fstream>
  3. #include <conio.h>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int n=0, z=0; //declare variables to count the amount of input
  8. int main ()
  9. {
  10. char ifname[150]; //declare input file name array
  11. char ofname[150]; //declare output file name array
  12. char key[13];
  13. char c;
  14. char intxt[256];
  15. char tmptxt[5];
  16. ifstream is; //declare ifstream to is
  17. ofstream os; //declare ofstream to os
  18.  
  19. cout<<endl<< "Enter the path of the input file: "; //enter filename and path cin<<ifname[0]<<endl;
  20. cin.get(ifname, 150); //grab filename from user
  21. is.open(ifname); //open file
  22. cout<<endl;
  23.  
  24. cout<<endl<< "Enter the path of the output file: "; //enter filename and path
  25. cin.get(ofname, 150); //grab filename from user
  26. os.open(ofname); //open file
  27. cout<<endl;
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 223
Reputation: Anonymusius is on a distinguished road 
Solved Threads: 10
Anonymusius's Avatar
Anonymusius Anonymusius is offline Offline
Posting Whiz in Training

Re: What is wrong with this code?

 
1
  #2
Sep 25th, 2006
It could be that you step over it because you still have the enter pressed from the first question. try doing something like:
  1. while(kbhit()){}
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)
Last edited by Anonymusius; Sep 25th, 2006 at 10:50 am. Reason: Forgot last bracket
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 21
Reputation: compshooter is an unknown quantity at this point 
Solved Threads: 0
compshooter's Avatar
compshooter compshooter is offline Offline
Newbie Poster

Re: What is wrong with this code?

 
0
  #3
Sep 25th, 2006
Thanks, but it didn't work. Any other ideas?
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 21
Reputation: compshooter is an unknown quantity at this point 
Solved Threads: 0
compshooter's Avatar
compshooter compshooter is offline Offline
Newbie Poster

Re: What is wrong with this code?

 
0
  #4
Sep 25th, 2006
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...
  1. #include <iostream>
  2. #include <fstream>
  3. #include <conio.h>
  4. #include <string>
  5. using namespace std;
  6.  
  7. int n=0, z=0; //declare variables to count the amount of input
  8. int main ()
  9. {
  10. char ifname[150]; //declare input file name array
  11. char ofname[150]; //declare output file name array
  12. char key[13];
  13. char c;
  14. char intxt[256];
  15. char tmptxt[5];
  16. ifstream is; //declare ifstream to is
  17. ofstream os; //declare ofstream to os
  18.  
  19. cout<<endl<< "Enter the path of the input file: "; //enter filename and path cin<<ifname[0]<<endl;
  20. cin.get(ifname, 150); //grab filename from user
  21. is.open(ifname); //open file
  22. cout<<endl;
  23.  
  24.  
  25. cout<<endl<< "Enter the path of the output file: "; //enter filename and path
  26. cin.get(ofname, 150); //grab filename from user
  27. os.open(ofname); //open file
  28. cout<<endl;
  29. //
  30. //*******************get key
  31. //
  32. for (int j=0; j<13; j++)
  33. {
  34. c=is.get();
  35. key[j]=c;
  36. }
  37. // cout<<"This is the key: ";
  38. for (int k=0; k<12; k++)
  39. cout<<key[k];
  40. // cout<<endl;
  41. is.clear(); //clear flags in ifstream
  42. is.seekg (13, ios::beg); //set pointer to second line of input file
  43. //
  44. //*********************find out how long the message is
  45. //
  46. for (int j=0; j<75; j++)
  47. {
  48. c=is.get();
  49. if (c!=';')
  50. intxt[j]=c;
  51. if (c==';')
  52. break;
  53. n++;
  54. z=j;
  55. }
  56. /* cout<<"n= "<<n<<endl;
  57.   cout<<"This is the message ";
  58.   for (int l=0; l<n; l++)
  59.   cout<<intxt[l];
  60.   cout<<endl; */
  61. //
  62. //***************************pad the message
  63. //
  64. if (n<5)
  65. {
  66. do
  67. {
  68. z++;
  69. intxt[z]='x';
  70. n++;
  71. }while(n<5);
  72. }
  73. else if (n<10)
  74. {
  75. do
  76. {
  77. z++;
  78. intxt[z]='x';
  79. n++;
  80. }while(n<10);
  81. }
  82. else if (n<15)
  83. {
  84. do
  85. {
  86. z++;
  87. intxt[z]='x';
  88. n++;
  89. }while(n<15);
  90. }
  91. else if (n<20)
  92. {
  93. do
  94. {
  95. z++;
  96. intxt[z]='x';
  97. n++;
  98. }while(n<20);
  99. }
  100. else if (n<25)
  101. {
  102. do
  103. {
  104. z++;
  105. intxt[z]='x';
  106. n++;
  107. }while(n<25);
  108. }
  109. else if (n<30)
  110. {
  111. do
  112. {
  113. z++;
  114. intxt[z]='x';
  115. n++;
  116. }while(n<30);
  117. }
  118. else if (n<35)
  119. {
  120. do
  121. {
  122. z++;
  123. intxt[z]='x';
  124. n++;
  125. }while(n<35);
  126. }
  127. else if (n<40)
  128. {
  129. do
  130. {
  131. z++;
  132. intxt[z]='x';
  133. n++;
  134. }while(n<40);
  135. }
  136. //
  137. //**************************ENCRYPT / DCRYPT
  138. //
  139. for (int r=0; r<n; r=r+5)
  140. {
  141. for (int p=0, s=r; p<5, s<r+5; p++, s++)
  142. tmptxt[p]=intxt[s];
  143. for (int p=1; p<10; p=p+2)
  144. {
  145. if (key[p]=='0')
  146. {
  147. cout<<tmptxt[0];
  148. os<<tmptxt[0];
  149. }
  150. if (key[p]=='1')
  151. {
  152. cout<<tmptxt[1];
  153. os<<tmptxt[1];
  154. }
  155. if (key[p]=='2')
  156. {
  157. cout<<tmptxt[2];
  158. os<<tmptxt[2];
  159. }
  160. if (key[p]=='3')
  161. {
  162. cout<<tmptxt[3];
  163. os<<tmptxt[3];
  164. }
  165. if (key[p]=='4')
  166. {
  167. cout<<tmptxt[4];
  168. os<<tmptxt[4];
  169. }
  170. }
  171. }
  172. cout<<';'; //add the end of file character as required
  173. getch(); //pause
  174. return 0;
  175. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: What is wrong with this code?

 
1
  #5
Sep 25th, 2006
Did you tried cin.ignore after cin.get(ifname, 150); ?
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 84
Reputation: rinoa04 is on a distinguished road 
Solved Threads: 4
rinoa04's Avatar
rinoa04 rinoa04 is offline Offline
Junior Poster in Training

Re: What is wrong with this code?

 
1
  #6
Sep 25th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 21
Reputation: compshooter is an unknown quantity at this point 
Solved Threads: 0
compshooter's Avatar
compshooter compshooter is offline Offline
Newbie Poster

Re: What is wrong with this code?

 
0
  #7
Sep 25th, 2006
AWESOME!

cin.ignore(); after the first cin.get() worked! I was going crazy. Thanks a lot!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC