943,752 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1587
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 23rd, 2008
0

Input buffer help transferring between Dos > Unix

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

C++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 1
Light Poster
MylesDBaker is offline Offline
36 posts
since Sep 2008
Sep 23rd, 2008
0

Re: Input buffer help transferring between Dos > Unix

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 23rd, 2008
0

Re: Input buffer help transferring between Dos > Unix

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
MylesDBaker is offline Offline
36 posts
since Sep 2008
Sep 23rd, 2008
0

Re: Input buffer help transferring between Dos > Unix

Got it. Can you run this and see if this works?

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 1
Light Poster
MylesDBaker is offline Offline
36 posts
since Sep 2008
Sep 23rd, 2008
0

Re: Input buffer help transferring between Dos > Unix

It works also. But ideally its not a good idea to have too many points of return from one function.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Sep 23rd, 2008
0

Re: Input buffer help transferring between Dos > Unix

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
MylesDBaker is offline Offline
36 posts
since Sep 2008
Sep 23rd, 2008
1

Re: Input buffer help transferring between Dos > Unix

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.

C++ Syntax (Toggle Plain Text)
  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"
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,373 posts
since Jan 2008
Sep 23rd, 2008
0

Re: Input buffer help transferring between Dos > Unix

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?
Reputation Points: 10
Solved Threads: 1
Light Poster
MylesDBaker is offline Offline
36 posts
since Sep 2008
Sep 23rd, 2008
0

Re: Input buffer help transferring between Dos > Unix

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:

C++ Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,373 posts
since Jan 2008
Sep 23rd, 2008
0

Re: Input buffer help transferring between Dos > Unix

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
Reputation Points: 10
Solved Threads: 1
Light Poster
MylesDBaker is offline Offline
36 posts
since Sep 2008

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: Need help with a C++ looping program
Next Thread in C++ Forum Timeline: Stacks help please





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


Follow us on Twitter


© 2011 DaniWeb® LLC