| | |
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:
Solved Threads: 1
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:
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!
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)
#include <iostream> #include <fstream> #include <string> using namespace std; int main () { int num1, num2, num3, num4, num5, num6, num, menu; string name1, name2, name3, name4, name5, name6, name; ifstream infile; infile.open("colors.txt"); infile >> num1; infile.ignore(); getline(infile, name1); infile >> num2; infile.ignore(); getline(infile, name2); infile >> num3; infile.ignore(); getline(infile, name3); infile >> num4; infile.ignore(); getline(infile, name4); infile >> num5; infile.ignore(); getline(infile, name5); infile >> num6; infile.ignore(); getline(infile, name6); infile.close(); cout << "1. Number" << endl; cout << "2. Name" << endl; cin >> menu; if (menu == 1) { cout << "What is the number?" << endl; cin >> num; if (num == num1) { cout << "The color is: " << name1 << endl; } if (num == num2) { cout << "The color is: " << name2 << endl; } if (num == num3) { cout << "The color is: " << name3 << endl; } if (num == num4) { cout << "The color is: " << name4 << endl; } if (num == num5) { cout << "The color is: " << name5 << endl; } if (num == num6) { cout << "The color is: " << name6 << endl; } } if (menu == 2) { cout << "What is the name?" << endl; cin.ignore(20, '\n'); getline(cin, name); if (name == name1) { cout << "The number is: " << num1 << endl; } if (name == name2) { cout << "The number is: " << num2 << endl; } if (name == name3) { cout << "The number is: " << num3 << endl; } if (name == name4) { cout << "The number is: " << num4 << endl; } if (name == name5) { cout << "The number is: " << num5 << endl; } if (name == name6) { cout << "The number is: " << num6 << endl; } } return 0; }
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!
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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.
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.
•
•
Join Date: Sep 2008
Posts: 30
Reputation:
Solved Threads: 1
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.
I have tried some of the suggestions on this thread: http://www.daniweb.com/forums/thread90228.html
Nothing seems to work for me.
•
•
Join Date: Sep 2008
Posts: 30
Reputation:
Solved Threads: 1
Got it. Can you run this and see if this works?
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; int main () { int num1, num2, num3, num4, num5, num6, num, menu; string name1, name2, name3, name4, name5, name6, name; ifstream infile; infile.open("colors.txt"); infile >> num1; infile.ignore(); getline(infile, name1); infile >> num2; infile.ignore(); getline(infile, name2); infile >> num3; infile.ignore(); getline(infile, name3); infile >> num4; infile.ignore(); getline(infile, name4); infile >> num5; infile.ignore(); getline(infile, name5); infile >> num6; infile.ignore(); getline(infile, name6); infile.close(); cout << "1. Number" << endl; cout << "2. Name" << endl; cin >> menu; if (menu == 1) { cout << "What is the number?" << endl; cin >> num; if (num == num1) { cout << "The color is: " << name1 << endl; return 0; } if (num == num2) { cout << "The color is: " << name2 << endl; return 0; } if (num == num3) { cout << "The color is: " << name3 << endl; return 0; } if (num == num4) { cout << "The color is: " << name4 << endl; return 0; } if (num == num5) { cout << "The color is: " << name5 << endl; return 0; } if (num == num6) { cout << "The color is: " << name6 << endl; return 0; } else { cout << "Can't find that color. Goodbye!" << endl; return 0; } } if (menu == 2) { cout << "What is the name?" << endl; cin.ignore(20, '\n'); getline(cin, name); if (name == name1) { cout << "The number is: " << num1 << endl; return 0; } if (name == name2) { cout << "The number is: " << num2 << endl; return 0; } if (name == name3) { cout << "The number is: " << num3 << endl; return 0; } if (name == name4) { cout << "The number is: " << num4 << endl; return 0; } if (name == name5) { cout << "The number is: " << num5 << endl; return 0; } if (name == name6) { cout << "The number is: " << num6 << endl; return 0; } else { cout << "Can't find that number. Goodbye!" << endl; } } if (menu != 1 && menu != 2) { cout << "Please select an option for the menu" << endl; } return 0; }
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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.
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)
string names[6]; int nums[6]; string name; int num; for (int i = 0; i < 6; i++) { infile >> nums[i]; infile.ignore(); getline(infile, names[i]); }
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"
•
•
Join Date: Sep 2008
Posts: 30
Reputation:
Solved Threads: 1
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?
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?
•
•
Join Date: Jan 2008
Posts: 3,819
Reputation:
Solved Threads: 501
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
if (num == num1) { // code } else if (num == num2) { // code } // more else if statements else { // code }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Need help with a C++ looping program
- Next Thread: Stacks help please
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert 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 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 temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






