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:
#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!