Input buffer help transferring between Dos > Unix

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

Join Date: Sep 2008
Posts: 30
Reputation: MylesDBaker is an unknown quantity at this point 
Solved Threads: 1
MylesDBaker MylesDBaker is offline Offline
Light Poster

Input buffer help transferring between Dos > Unix

 
0
  #1
Sep 23rd, 2008
Hello everyone.

I am trying to create a program that reads these values / strings from a file:

170 Lipstick
250 Orange Crush
350 Chickadee
450 Green Grass
550 Monaco
750 Toffee Crunch

Then I am supposed to create a menu that prompts the user to provide either the number or name, then my program provides the corresponding number / name.

My problem is this: It works locally on my system, which is running DOS, but does not operate properly on the Unix based upload site. I think it has something to do with the transition from input buffer to input buffer. Something concerning form feed and return. So I have been messing around with cin.ignore(10,'/n'); where '/n' is the new line character from the form feed.

If anyone can solve this problem I would appreciate the help. My code is as follows:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9. int num1, num2, num3, num4, num5, num6, num, menu;
  10. string name1, name2, name3, name4, name5, name6, name;
  11. ifstream infile;
  12.  
  13. infile.open("colors.txt");
  14.  
  15. infile >> num1;
  16. infile.ignore();
  17. getline(infile, name1);
  18.  
  19. infile >> num2;
  20. infile.ignore();
  21. getline(infile, name2);
  22.  
  23. infile >> num3;
  24. infile.ignore();
  25. getline(infile, name3);
  26.  
  27. infile >> num4;
  28. infile.ignore();
  29. getline(infile, name4);
  30.  
  31. infile >> num5;
  32. infile.ignore();
  33. getline(infile, name5);
  34.  
  35. infile >> num6;
  36. infile.ignore();
  37. getline(infile, name6);
  38.  
  39. infile.close();
  40.  
  41. cout << "1. Number" << endl;
  42. cout << "2. Name" << endl;
  43. cin >> menu;
  44.  
  45. if (menu == 1)
  46. {
  47. cout << "What is the number?" << endl;
  48. cin >> num;
  49.  
  50. if (num == num1)
  51. {
  52. cout << "The color is: " << name1 << endl;
  53. }
  54.  
  55. if (num == num2)
  56. {
  57. cout << "The color is: " << name2 << endl;
  58. }
  59.  
  60. if (num == num3)
  61. {
  62. cout << "The color is: " << name3 << endl;
  63. }
  64.  
  65. if (num == num4)
  66. {
  67. cout << "The color is: " << name4 << endl;
  68. }
  69.  
  70. if (num == num5)
  71. {
  72. cout << "The color is: " << name5 << endl;
  73. }
  74.  
  75. if (num == num6)
  76. {
  77. cout << "The color is: " << name6 << endl;
  78. }
  79.  
  80. }
  81.  
  82. if (menu == 2)
  83. {
  84. cout << "What is the name?" << endl;
  85. cin.ignore(20, '\n');
  86. getline(cin, name);
  87.  
  88. if (name == name1)
  89. {
  90. cout << "The number is: " << num1 << endl;
  91. }
  92.  
  93. if (name == name2)
  94. {
  95. cout << "The number is: " << num2 << endl;
  96. }
  97.  
  98. if (name == name3)
  99. {
  100. cout << "The number is: " << num3 << endl;
  101. }
  102.  
  103. if (name == name4)
  104. {
  105. cout << "The number is: " << num4 << endl;
  106. }
  107.  
  108. if (name == name5)
  109. {
  110. cout << "The number is: " << num5 << endl;
  111. }
  112.  
  113. if (name == name6)
  114. {
  115. cout << "The number is: " << num6 << endl;
  116. }
  117.  
  118. }
  119.  
  120. return 0;
  121. }

I am still working at it because I need to redirect the user in the event of an incorrect menu choice.

P.S. Ignore the tabs between IF statements, I don't think I need those and I am about to get rid of them .

Thank You!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Input buffer help transferring between Dos > Unix

 
0
  #2
Sep 23rd, 2008
Firstly the line feed or the new line character is '\n' not '/n'.

By default most records in a file on Windows are terminated by the CRLF (\r\n) ie, the carriage return and line feed.

On *nix, its just the LF (line feed character) '\n'.

If you are uploading both your data file and your program, then make sure that you do an ASCII ftp, so that it automatically adjusts the line termination character.

I ran your code on Linux and it works fine.
Last edited by stilllearning; Sep 23rd, 2008 at 8:17 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 30
Reputation: MylesDBaker is an unknown quantity at this point 
Solved Threads: 1
MylesDBaker MylesDBaker is offline Offline
Light Poster

Re: Input buffer help transferring between Dos > Unix

 
0
  #3
Sep 23rd, 2008
I know it is '\n', I made a keying error.

I have tried some of the suggestions on this thread: http://www.daniweb.com/forums/thread90228.html

Nothing seems to work for me.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 30
Reputation: MylesDBaker is an unknown quantity at this point 
Solved Threads: 1
MylesDBaker MylesDBaker is offline Offline
Light Poster

Re: Input buffer help transferring between Dos > Unix

 
0
  #4
Sep 23rd, 2008
Got it. Can you run this and see if this works?

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9. int num1, num2, num3, num4, num5, num6, num, menu;
  10. string name1, name2, name3, name4, name5, name6, name;
  11. ifstream infile;
  12.  
  13. infile.open("colors.txt");
  14.  
  15. infile >> num1;
  16. infile.ignore();
  17. getline(infile, name1);
  18.  
  19. infile >> num2;
  20. infile.ignore();
  21. getline(infile, name2);
  22.  
  23. infile >> num3;
  24. infile.ignore();
  25. getline(infile, name3);
  26.  
  27. infile >> num4;
  28. infile.ignore();
  29. getline(infile, name4);
  30.  
  31. infile >> num5;
  32. infile.ignore();
  33. getline(infile, name5);
  34.  
  35. infile >> num6;
  36. infile.ignore();
  37. getline(infile, name6);
  38.  
  39. infile.close();
  40.  
  41. cout << "1. Number" << endl;
  42. cout << "2. Name" << endl;
  43. cin >> menu;
  44.  
  45. if (menu == 1)
  46. {
  47. cout << "What is the number?" << endl;
  48. cin >> num;
  49.  
  50. if (num == num1)
  51. {
  52. cout << "The color is: " << name1 << endl;
  53. return 0;
  54. }
  55.  
  56. if (num == num2)
  57. {
  58. cout << "The color is: " << name2 << endl;
  59. return 0;
  60. }
  61.  
  62. if (num == num3)
  63. {
  64. cout << "The color is: " << name3 << endl;
  65. return 0;
  66. }
  67.  
  68. if (num == num4)
  69. {
  70. cout << "The color is: " << name4 << endl;
  71. return 0;
  72. }
  73.  
  74. if (num == num5)
  75. {
  76. cout << "The color is: " << name5 << endl;
  77. return 0;
  78. }
  79.  
  80. if (num == num6)
  81. {
  82. cout << "The color is: " << name6 << endl;
  83. return 0;
  84. }
  85.  
  86. else
  87. {
  88. cout << "Can't find that color. Goodbye!" << endl;
  89. return 0;
  90. }
  91.  
  92. }
  93.  
  94. if (menu == 2)
  95.  
  96. {
  97. cout << "What is the name?" << endl;
  98. cin.ignore(20, '\n');
  99. getline(cin, name);
  100.  
  101. if (name == name1)
  102. {
  103. cout << "The number is: " << num1 << endl;
  104. return 0;
  105. }
  106.  
  107. if (name == name2)
  108. {
  109. cout << "The number is: " << num2 << endl;
  110. return 0;
  111. }
  112.  
  113. if (name == name3)
  114. {
  115. cout << "The number is: " << num3 << endl;
  116. return 0;
  117. }
  118.  
  119. if (name == name4)
  120. {
  121. cout << "The number is: " << num4 << endl;
  122. return 0;
  123. }
  124.  
  125. if (name == name5)
  126. {
  127. cout << "The number is: " << num5 << endl;
  128. return 0;
  129. }
  130.  
  131. if (name == name6)
  132. {
  133. cout << "The number is: " << num6 << endl;
  134. return 0;
  135. }
  136. else
  137. {
  138. cout << "Can't find that number. Goodbye!" << endl;
  139. }
  140.  
  141. }
  142.  
  143. if (menu != 1 && menu != 2)
  144. {
  145. cout << "Please select an option for the menu" << endl;
  146. }
  147.  
  148. return 0;
  149. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Input buffer help transferring between Dos > Unix

 
0
  #5
Sep 23rd, 2008
It works also. But ideally its not a good idea to have too many points of return from one function.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 30
Reputation: MylesDBaker is an unknown quantity at this point 
Solved Threads: 1
MylesDBaker MylesDBaker is offline Offline
Light Poster

Re: Input buffer help transferring between Dos > Unix

 
0
  #6
Sep 23rd, 2008
I thought so too. I tried removing them, but the program would not work without having them there. Is there something I can do to avoid using them? Without them I get the else statement after every output.
Last edited by MylesDBaker; Sep 23rd, 2008 at 8:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Input buffer help transferring between Dos > Unix

 
1
  #7
Sep 23rd, 2008
Originally Posted by MylesDBaker View Post
I thought so too. I tried removing them, but the program would not work without having them there. Is there something I can do to avoid using them? Without them I get the else statement after every output.
You can change your if statements to a switch statement when you are comparing integers and make the else code the default case, though that will not work when comparing strings since you can't use a switch on a string since string isn't an ordinal type. A better solution would be to have an array called nums and an array called names, then loop through those arrays since you are basically doing the same thing over and over 6 times. That will cut down on the amount of code significantly.

  1. string names[6];
  2. int nums[6];
  3. string name;
  4. int num;
  5.  
  6. for (int i = 0; i < 6; i++)
  7. {
  8. infile >> nums[i];
  9. infile.ignore();
  10. getline(infile, names[i]);
  11. }


Have the code where you compare values inside of a loop also.
Last edited by VernonDozier; Sep 23rd, 2008 at 10:55 pm. Reason: typo: changed "catch" to "can't"
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 30
Reputation: MylesDBaker is an unknown quantity at this point 
Solved Threads: 1
MylesDBaker MylesDBaker is offline Offline
Light Poster

Re: Input buffer help transferring between Dos > Unix

 
0
  #8
Sep 23rd, 2008
Thanks for the thought, but our class has not progressed that far yet.

I could do so in this manner, but I am trying to learn to master the methods we are learning at the moment rather than branching out to different, more efficient ways to code. If I use conditional statements, then do I need the return 0; statement?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Input buffer help transferring between Dos > Unix

 
0
  #9
Sep 23rd, 2008
Originally Posted by MylesDBaker View Post
Thanks for the thought, but our class has not progressed that far yet.

I could do so in this manner, but I am trying to learn to master the methods we are learning at the moment rather than branching out to different, more efficient ways to code. If I use conditional statements, then do I need the return 0; statement?
Well, you need some way to make sure you don't execute the else branch if any of the preceding conditions are true. You can keep the return 0 where it is or you can change all the if tests into an if - else if -else format:

  1. if (num == num1)
  2. {
  3. // code
  4. }
  5. else if (num == num2)
  6. {
  7. // code
  8. }
  9. // more else if statements
  10. else
  11. {
  12. // code
  13. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 30
Reputation: MylesDBaker is an unknown quantity at this point 
Solved Threads: 1
MylesDBaker MylesDBaker is offline Offline
Light Poster

Re: Input buffer help transferring between Dos > Unix

 
0
  #10
Sep 23rd, 2008
I'll try that in a few minutes, but I think this is more along the lines of what my professor wanted us to do.

Thanks
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC