944,013 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2122
  • C++ RSS
Sep 25th, 2006
1

What is wrong with this code?

Expand Post »
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...

C++ Syntax (Toggle Plain Text)
  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?





C++ Syntax (Toggle Plain Text)
  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;
Reputation Points: 18
Solved Threads: 0
Newbie Poster
compshooter is offline Offline
21 posts
since Feb 2005
Sep 25th, 2006
1

Re: What is wrong with this code?

It could be that you step over it because you still have the enter pressed from the first question. try doing something like:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 129
Solved Threads: 11
Posting Whiz in Training
Anonymusius is offline Offline
223 posts
since Aug 2006
Sep 25th, 2006
0

Re: What is wrong with this code?

Thanks, but it didn't work. Any other ideas?
Reputation Points: 18
Solved Threads: 0
Newbie Poster
compshooter is offline Offline
21 posts
since Feb 2005
Sep 25th, 2006
0

Re: What is wrong with this code?

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...
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 18
Solved Threads: 0
Newbie Poster
compshooter is offline Offline
21 posts
since Feb 2005
Sep 25th, 2006
1

Re: What is wrong with this code?

Did you tried cin.ignore after cin.get(ifname, 150); ?
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Sep 25th, 2006
1

Re: What is wrong with this code?

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.
Reputation Points: 52
Solved Threads: 4
Junior Poster in Training
rinoa04 is offline Offline
84 posts
since Sep 2006
Sep 25th, 2006
0

Re: What is wrong with this code?

AWESOME!

cin.ignore(); after the first cin.get() worked! I was going crazy. Thanks a lot!
Reputation Points: 18
Solved Threads: 0
Newbie Poster
compshooter is offline Offline
21 posts
since Feb 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: API Help
Next Thread in C++ Forum Timeline: Help me simplify this program please!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC