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!

Recommended Answers

All 15 Replies

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.

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

#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;
}

It works also. But ideally its not a good idea to have too many points of return from one function.

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.

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.

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.

commented: So much better XD +4

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?

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:

if (num == num1)
{ 
   // code
}
else if (num == num2)
{
     // code
}
// more else if statements
else
{
    // code
}

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

Oops. I ****ed it on that last section of code. Now it seems to be working and this is my final code: I am going to look it over a little before I submit it for good.

#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;
		}

		else if (num == num2)
		{
			cout << "The color is: " << name2 << endl;
		}

		else if (num == num3)
		{
			cout << "The color is: " << name3 << endl;
		}

		else if (num == num4)
		{
			cout << "The color is: " << name4 << endl;
		}

		else if (num == num5)
		{
			cout << "The color is: " << name5 << endl;
		}

		else if (num == num6)
		{
			cout << "The color is: " << name6 << endl;
		}

		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 color is: " << num1 << endl;
		}

		else if (name == name2)
		{
			cout << "The color is: " << num2 << endl;
		}

		else if (name == name3)
		{
			cout << "The color is: " << num3 << endl;
		}

		else if (name == name4)
		{
			cout << "The color is: " << num4 << endl;
		}

		else if (name == name5)
		{
			cout << "The color is: " << num5 << endl; 
		}

		else if (name == name6)
		{
			cout << "The color is: " << num6 << endl;
		}
		else 
		{
			cout << "Can't find that number. Goodbye!" << endl;
		}
	
	}					

	if (menu != 1 && menu != 2)
	{
		cout << "Please select an option for the menu" << endl;
		return 1;
	}

	return 0;
}

My computer screen just made some really weird font, so hopefully this is posting right.


Your code is below.

if (menu == 1)
	{
		cout << "What is the number?" << endl;
		cin >> num;

	else if (num == num1)
		{
			cout << "The color is: " << name1 << endl;
		}

		else if (num == num2)
		{
			cout << "The color is: " << name2 << endl;
		}
// more code

I had this in mind:

if (menu == 1)
{
     if (num == num1)
     {
          // code
     }
     else if (num == num2)
     {
          // code
     }
     else if (num == num3)
     {
          // code
     }
     // more else if statements
     else
     {
          // code
     }
}

Oops. I ****ed it on that last section of code. Now it seems to be working and this is my final code: I am going to look it over a little before I submit it for good.

#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;
		}

		else if (num == num2)
		{
			cout << "The color is: " << name2 << endl;
		}

		else if (num == num3)
		{
			cout << "The color is: " << name3 << endl;
		}

		else if (num == num4)
		{
			cout << "The color is: " << name4 << endl;
		}

		else if (num == num5)
		{
			cout << "The color is: " << name5 << endl;
		}

		else if (num == num6)
		{
			cout << "The color is: " << name6 << endl;
		}

		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 color is: " << num1 << endl;
		}

		else if (name == name2)
		{
			cout << "The color is: " << num2 << endl;
		}

		else if (name == name3)
		{
			cout << "The color is: " << num3 << endl;
		}

		else if (name == name4)
		{
			cout << "The color is: " << num4 << endl;
		}

		else if (name == name5)
		{
			cout << "The color is: " << num5 << endl; 
		}

		else if (name == name6)
		{
			cout << "The color is: " << num6 << endl;
		}
		else 
		{
			cout << "Can't find that number. Goodbye!" << endl;
		}
	
	}					

	if (menu != 1 && menu != 2)
	{
		cout << "Please select an option for the menu" << endl;
		return 1;
	}

	return 0;
}

Yes, that's what I had in mind, though I haven't run it. Didn't see your edit before my last post.

Hey, yeah that was the coding error I was talking about. I fixed it and it seems to be working, I am going to record the problem as solved. Thanks a bunch.

+ rep

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.