Thread: Getting back into the swing.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Is there any way I can accomplish this without an error?

    Code:
    void edit(employee a)
    {
    	cout << "What would you like to change?" << endl 
    		 << "1. Name" << endl << "2. Payrate" << endl;
    	string input;
    	getline(cin, input);
    	if (input == 1)
    	{
    		getline(cin, a.name);
    	}
    	else if (input == 2)
    	{
    		getline(cin, a.payrate);
    	}
    	else
    	{
    		cout << "Please enter a valid selection, 1 or 2." << endl;
    	}
    }
    I can't find a good tutorial on the basic string on the website, at least not for what I'm attempting to do, is this even possible?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    I guess I could avoid the problem by using an integer as input, but i'd have to add more code for error checking and whatnot, and that doesn't seem good, one data type is easier to work with, right?
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Reading about stringstreams, don't give me the answer yet!

    By George I think I've got it!
    Code:
    void edit(employee a)
    {
    	string input;
    	int selection;
    	cout << "What would you like to change?" << endl 
    		 << "1. Name" << endl << "2. Payrate" << endl;
    
    	getline(cin, input);
    	stringstream(input) >> selection;
    	if (selection == 1)
    	{
    		getline(cin, a.name);
    	}
    	else if (selection == 2)
    	{
    		getline(cin, a.payrate);
    	}
    	else
    	{
    		cout << "Please enter a valid selection, 1 or 2." << endl;
    	}
    }
    Last edited by Shamino; 01-18-2012 at 07:32 PM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Who designed Java swing metal look&feel
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-22-2008, 10:08 AM
  2. Basics help, getting back into the swing...
    By Shamino in forum C++ Programming
    Replies: 8
    Last Post: 05-17-2007, 12:27 AM
  3. java/swing gui
    By xddxogm3 in forum Tech Board
    Replies: 4
    Last Post: 07-12-2004, 06:46 PM
  4. What is Swing?
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 03-18-2002, 05:05 PM