Thread: getline problem

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    60

    getline problem

    ok, this program has no errors and runs fine. My problem is that the second cal of getline doesn't seem to work. Does anyone know why it looks fine to me. Thanks for your time.

    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    #define PI 3.141592654
    
    using namespace std;
    
    class Column
    {
    public:
    	Column(double l,double h,double w,double e, double i);
    	Column(double l, double r, double e, double i);
    	void print1() const;
    	void print2() const;
    	double get_length();
    	double get_elasticity();
    	double get_radius();
    	double get_height();
    	double get_width();
    	double get_moment_inertia();
    	double calc_gyr_circ();
    	double calc_gyr_rec();
    	double calc_rec_area();
    	double calc_circ_area();
    private:
    	double len;
    	double rad;
    	double hei;
    	double wid;
    	double el;
    	double inertia;
    	double rgyr_rec;
    	double rgyr_circ;
    	double factor;
    	double area1;
    	double area2;
    };
    
    Column::Column(double l,double h,double w,double e, double i)
    {
    	len = l;
    	hei = h;
    	wid = w;
    	el = e;
    	inertia = i;
    }
    
    Column::Column(double l, double r, double e, double i)
    {
    	len = l;
    	rad = r;
    	el = e;
    	inertia = i;
    }
    
    void Column::print1() const
    {
    	cout<<"The following is the data for the Column:"<<endl;
    	cout<<"Length = "<<len<<" mm"<<endl;
    	cout<<"Width = "<<wid<<" mm"<<endl;
    	cout<<"Heigth = "<<hei<<" mm"<<endl;
    	cout<<"Moment of Inertia = "<<inertia<<" m^4"<<endl;
    	cout<<"Cross-Sectional Area = "<<area1<<" m^2"<<endl;
    	cout<<"Radius of Gyration = "<<rgyr_rec<<" m"<<endl;
    	cout<<"Modulus of Elasticity = "<<el<<" GPa"<<endl;
    }
    
    void Column::print2() const
    {
    	cout<<"The following is the data for the Column:"<<endl;
    	cout<<"Length = "<<len<<" mm"<<endl;
    	cout<<"Radius = "<<rad<<" mm"<<endl;
    	cout<<"Cross-Sectional Area = "<<area2<<" m^2"<<endl;
    	cout<<"Radius of Gyration = "<<rgyr_circ<<" m"<<endl;
    	cout<<"Moment of Inertia = "<<inertia<<" mm^4"<<endl;
    	cout<<"Modulus of Elasticity = "<<el<<" GPa"<<endl;
    }
    
    double Column::get_length()
    {
    	return len;
    }
    
    double Column::get_width()
    {
    	return wid;
    }
    
    double Column::get_radius()
    {
    	return rad;
    }
    double Column::get_height()
    {
    	return hei;
    }
    
    double Column::get_moment_inertia()
    {
    	return inertia;
    }
    
    double Column::get_elasticity()
    {
    	return el;
    }
    
    double Column::calc_rec_area()
    {
    	area1 = (wid/1000) * (hei/1000);
    	return area1;
    }
    
    double Column::calc_circ_area()
    {
    	area2 = PI * pow((rad/1000),2);
    	return area2;
    }
    
    double Column::calc_gyr_rec()
    {
    	rgyr_rec = sqrt((inertia / area1));
    	return rgyr_rec;
    }
    
    double Column::calc_gyr_circ()
    {
    	rgyr_circ = sqrt((inertia / area2));
    	return rgyr_circ;
    }
    
    
    int main()
    {
    	double l,h,r,w,e,i,g,a,K;
    	string choice;
    	cout<<" Would You Like to Analyse a Beam or a Column ?"<<endl;
    	cout<<"\t(Enter 1 for Beam, 2 for Column)"<<endl;
    	getline(cin,choice);
    
    	if (choice == "1")
    	{
    		cout<<"Not Done Yet"<<endl;
    	}
    
    	else if (choice == "2")
    	{
    
    		string answer, reply, result;
    		cout<<"Does the Beam Have a Circular x-section or a Rectangular x-section ?"<<endl;
    		cout<<"\t (Enter 'a' for Circular, 'b' for Rectangular)"<<endl;
    		getline(cin,answer);
    
    		if (answer == "a" || answer == "A")
    		{
    			cout<<"Enter the Length of the beam (mm): ";
    			cin>>l;
    			cout<<"Enter the Radius of the beam (mm): ";
    			cin>>r;
    
    			cout<<"Here are Your Choices for the End Conditions of The Column:"<<endl;
    			cout<<"\t(1) Free - Fixed"<<endl<<"\t(2) Pinned - Pinned"<<endl<<"\t(3) Fixed - Pinned"<<endl;
    			cout<<"\t(4) Fixed - Fixed"<<endl;
    			cout<<"Choose the Corresponding Number for the Condition Used:"<<endl;
    			getline(cin,result);
    
    			if (result == "1")
    			{
    				K = 2;
    			}
    			else if (result == "2")
    			{
    				K = 1;
    			}
    			else if (result == "3")
    			{
    				K = 0.7;
    			}
    			else if (result == "4")
    			{
    				K = 0.5;
    			}
    			else
    			{
    				cout<<"You Have Not Entered a Corresponding Number Indicated."<<endl;
    				return 0;
    			}
    
    			i = (PI/4)*pow((r/1000),4);
    			cout<<"Here is a List of the Material Types that are Available:"<<endl;
    			cout<<"\t(1) Steel"<<endl<<"\t(2) Aluminum"<<endl<<"\t(3) Brass"<<endl;
    			cout<<"Choose a Material from the List above by Entering the Corresponding Number: ";
    			getline(cin,reply);
    
    			if(reply == "1") e = 200;
    			else if(reply == "2") e = 70;
    			else if(reply == "3") e = 105;
    			else
    			{
    				cout<<"You Have Not Entered a Corresponding Number Indicated."<<endl;
    				return 0;
    			}
    
    			Column circ(l,r,e,i);
    			a = circ.calc_circ_area();
    			g = circ.calc_gyr_circ();
    			circ.print1();
    
    			cout<<"From the Data Inputted, the Following Results were Obtained:"<<endl;
    			double crit_load = (pow(PI,2)*(e * 10e9)*(i))/ (pow(((l /1000)*K),2));
    			double crit_stress = (pow(PI,2)*(e * 10e9)/ (pow((((l /1000)*K)/g),2))) ;
    			cout<<"\t Buckling Load = "<<crit_load<<" N"<<endl;
    			cout<<"\t Buckling Stress = "<<crit_stress * 10e-06<<" MPa"<<endl; 
    
    		}
    
    		else if (answer == "b" || answer == "B")
    		{
    			cout<<"Enter the Length of the beam (mm): ";
    			cin>>l;
    			cout<<"Enter the width of the beam (mm): ";
    			cin>>w;
    			cout<<"Enter the height of the beam (mm): ";
    			cin>>h;
    
    			cout<<"Here are Your Choices for the End Conditions of The Column:"<<endl;
    			cout<<"\t(1) Free - Fixed"<<endl<<"\t(2) Pinned - Pinned"<<endl<<"\t(3) Fixed - Pinned"<<endl;
    			cout<<"\t(4) Fixed - Fixed"<<endl;
    			cout<<"Choose the Corresponding Number for the Condition Used:"<<endl;
    			getline(cin,result);
    
    			if (result == "1")
    			{
    				K = 2;
    			}
    			else if (result == "2")
    			{
    				K = 1;
    			}
    			else if (result == "3")
    			{
    				K = 0.7;
    			}
    			else if (result == "4")
    			{
    				K = 0.5;
    			}
    			else
    			{
    				cout<<"You Have Not Entered a Corresponding Number Indicated."<<endl;
    				return 0;
    			}
    			
    			i = ((w/1000)* pow((h/1000),3)) /12;
    			cout<<"Here is a List of the Material Types that are Available:"<<endl;
    			cout<<"\t(1) Steel"<<endl<<"\t(2) Aluminum"<<endl<<"\t(3) Brass"<<endl;
    			cout<<"Choose a Material from the List above by Entering the Corresponding Number: ";
    			getline(cin,reply);
    
    			if(reply == "1") e = 200;
    			else if(reply == "2") e = 70;
    			else if(reply == "3") e = 105;
    			else
    			{
    				cout<<"You Have Not Entered a Corresponding Number Indicated."<<endl;
    				return 0;
    			}
    
    			Column rec(l,w,h,e,i);
    			a = rec.calc_rec_area();
    			g = rec.calc_gyr_rec();
    			rec.print2();
    			cout<<"From the Data Inputted, the Following Results were Obtained:"<<endl;
    			double crit_load = (pow(PI,2)*(e * 10e9)*(i))/ (pow(((l /1000)*K),2)); 
    			double crit_stress = (pow(PI,2)*(e * 10e9)/ (pow((((l /1000)*K)/g),2)));
    			cout<<"\t Buckling Load = "<<crit_load<<" N"<<endl;
    			cout<<"\t Buckling Stress = "<<crit_stress * 10e-06<<" MPa"<<endl; 
    
    		}
    		else 
    		{
    			cout<<"There is no Choice called "<<answer<<endl;
    			return 0;
    		}
    	}
    
    	else 
    	{
    		cout<<"You Have not Entered a Proper Argument. Please Try Again."<<endl;
    		return 0;
    	}
    
    	return 0;
    }
    C++ can hurt.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Way too much code to go through...post a small example that demonstrates the issues you are having - you'll get a much better response.

    gg

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    There are probably dozens of posts with the same problem (mine included), but here we go again:

    due to problems with getline, whenever a call to getline comes after a call to cin (not necessarily immediately after, but anytime after), you should precede the getline with:
    Code:
    cin.ignore();
    this will flush out leftover newlines and allow getline to work.

    So in your case, try this:
    Code:
    cout<<"Choose the Corresponding Number for the Condition Used:"<<endl;
    cin.ignore();
    getline(cin,result);
    This will take care of all subsequent calls to getline until the next call to cin, after which you will have to add another cin.ignore().

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    well here is the core problem at the moment

    Code:
                      string answer, reply, result;
    		cout<<"Does the Beam Have a Circular x-section or a Rectangular x-section ?"<<endl;
    		cout<<"\t (Enter 'a' for Circular, 'b' for Rectangular)"<<endl;
    		getline(cin,answer);
    the string answer doesn't get initialized when i call the getline function so when it checks for the value of answer, it just quits the program by jumping to the last else statement which is to tell teh user that he/she didn't enter the proper reply. Hope this helps to find yoiu guys to see the problem more clearly. Sorry for the code but i thought poeple would try and compile it directly.

    Thanks again
    C++ can hurt.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    we might if it wasnt peppered with smilies!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    we might if it wasnt peppered with smilies!
    i.e. next time check the Options box to "Disable Smilies in This Post"


  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    Code:
    #include <iostream>
    #include <string>
    #include <cmath>
    
    #define PI 3.141592654
    
    using namespace std;
    
    class Column
    {
    public:
    	Column(double l,double h,double w,double e, double i);
    	Column(double l, double r, double e, double i);
    	void print1() const;
    	void print2() const;
    	double get_length();
    	double get_elasticity();
    	double get_radius();
    	double get_height();
    	double get_width();
    	double get_moment_inertia();
    	double calc_gyr_circ();
    	double calc_gyr_rec();
    	double calc_rec_area();
    	double calc_circ_area();
    private:
    	double len;
    	double rad;
    	double hei;
    	double wid;
    	double el;
    	double inertia;
    	double rgyr_rec;
    	double rgyr_circ;
    	double factor;
    	double area1;
    	double area2;
    };
    
    Column::Column(double l,double h,double w,double e, double i)
    {
    	len = l;
    	hei = h;
    	wid = w;
    	el = e;
    	inertia = i;
    }
    
    Column::Column(double l, double r, double e, double i)
    {
    	len = l;
    	rad = r;
    	el = e;
    	inertia = i;
    }
    
    void Column::print1() const
    {
    	cout<<"The following is the data for the Column:"<<endl;
    	cout<<"Length = "<<len<<" mm"<<endl;
    	cout<<"Width = "<<wid<<" mm"<<endl;
    	cout<<"Heigth = "<<hei<<" mm"<<endl;
    	cout<<"Moment of Inertia = "<<inertia<<" m^4"<<endl;
    	cout<<"Cross-Sectional Area = "<<area1<<" m^2"<<endl;
    	cout<<"Radius of Gyration = "<<rgyr_rec<<" m"<<endl;
    	cout<<"Modulus of Elasticity = "<<el<<" GPa"<<endl;
    }
    
    void Column::print2() const
    {
    	cout<<"The following is the data for the Column:"<<endl;
    	cout<<"Length = "<<len<<" mm"<<endl;
    	cout<<"Radius = "<<rad<<" mm"<<endl;
    	cout<<"Cross-Sectional Area = "<<area2<<" m^2"<<endl;
    	cout<<"Radius of Gyration = "<<rgyr_circ<<" m"<<endl;
    	cout<<"Moment of Inertia = "<<inertia<<" mm^4"<<endl;
    	cout<<"Modulus of Elasticity = "<<el<<" GPa"<<endl;
    }
    
    double Column::get_length()
    {
    	return len;
    }
    
    double Column::get_width()
    {
    	return wid;
    }
    
    double Column::get_radius()
    {
    	return rad;
    }
    double Column::get_height()
    {
    	return hei;
    }
    
    double Column::get_moment_inertia()
    {
    	return inertia;
    }
    
    double Column::get_elasticity()
    {
    	return el;
    }
    
    double Column::calc_rec_area()
    {
    	area1 = (wid/1000) * (hei/1000);
    	return area1;
    }
    
    double Column::calc_circ_area()
    {
    	area2 = PI * pow((rad/1000),2);
    	return area2;
    }
    
    double Column::calc_gyr_rec()
    {
    	rgyr_rec = sqrt((inertia / area1));
    	return rgyr_rec;
    }
    
    double Column::calc_gyr_circ()
    {
    	rgyr_circ = sqrt((inertia / area2));
    	return rgyr_circ;
    }
    
    
    int main()
    {
    	double l,h,r,w,e,i,g,a,K;
    	string choice;
    	cout<<" Would You Like to Analyse a Beam or a Column ?"<<endl;
    	cout<<"\t(Enter 1 for Beam, 2 for Column)"<<endl;
    	getline(cin,choice);
    
    	if (choice == "1")
    	{
    		cout<<"Not Done Yet"<<endl;
    	}
    
    	else if (choice == "2")
    	{
    
    		string answer, reply, result;
    		cout<<"Does the Beam Have a Circular x-section or a Rectangular x-section ?"<<endl;
    		cout<<"\t (Enter 'a' for Circular, 'b' for Rectangular)"<<endl;
    		getline(cin,answer);
    
    		if (answer == "a" || answer == "A")
    		{
    			cout<<"Enter the Length of the beam (mm): ";
    			cin>>l;
    			cout<<"Enter the Radius of the beam (mm): ";
    			cin>>r;
    
    			cout<<"Here are Your Choices for the End Conditions of The Column:"<<endl;
    			cout<<"\t(1) Free - Fixed"<<endl<<"\t(2) Pinned - Pinned"<<endl<<"\t(3) Fixed - Pinned"<<endl;
    			cout<<"\t(4) Fixed - Fixed"<<endl;
    			cout<<"Choose the Corresponding Number for the Condition Used:"<<endl;
    			getline(cin,result);
    
    			if (result == "1")
    			{
    				K = 2;
    			}
    			else if (result == "2")
    			{
    				K = 1;
    			}
    			else if (result == "3")
    			{
    				K = 0.7;
    			}
    			else if (result == "4")
    			{
    				K = 0.5;
    			}
    			else
    			{
    				cout<<"You Have Not Entered a Corresponding Number Indicated."<<endl;
    				return 0;
    			}
    
    			i = (PI/4)*pow((r/1000),4);
    			cout<<"Here is a List of the Material Types that are Available:"<<endl;
    			cout<<"\t(1) Steel"<<endl<<"\t(2) Aluminum"<<endl<<"\t(3) Brass"<<endl;
    			cout<<"Choose a Material from the List above by Entering the Corresponding Number: ";
    			getline(cin,reply);
    
    			if(reply == "1") e = 200;
    			else if(reply == "2") e = 70;
    			else if(reply == "3") e = 105;
    			else
    			{
    				cout<<"You Have Not Entered a Corresponding Number Indicated."<<endl;
    				return 0;
    			}
    
    			Column circ(l,r,e,i);
    			a = circ.calc_circ_area();
    			g = circ.calc_gyr_circ();
    			circ.print1();
    
    			cout<<"From the Data Inputted, the Following Results were Obtained:"<<endl;
    			double crit_load = (pow(PI,2)*(e * 10e9)*(i))/ (pow(((l /1000)*K),2));
    			double crit_stress = (pow(PI,2)*(e * 10e9)/ (pow((((l /1000)*K)/g),2))) ;
    			cout<<"\t Buckling Load = "<<crit_load<<" N"<<endl;
    			cout<<"\t Buckling Stress = "<<crit_stress * 10e-06<<" MPa"<<endl; 
    
    		}
    
    		else if (answer == "b" || answer == "B")
    		{
    			cout<<"Enter the Length of the beam (mm): ";
    			cin>>l;
    			cout<<"Enter the width of the beam (mm): ";
    			cin>>w;
    			cout<<"Enter the height of the beam (mm): ";
    			cin>>h;
    
    			cout<<"Here are Your Choices for the End Conditions of The Column:"<<endl;
    			cout<<"\t(1) Free - Fixed"<<endl<<"\t(2) Pinned - Pinned"<<endl<<"\t(3) Fixed - Pinned"<<endl;
    			cout<<"\t(4) Fixed - Fixed"<<endl;
    			cout<<"Choose the Corresponding Number for the Condition Used:"<<endl;
    			getline(cin,result);
    
    			if (result == "1")
    			{
    				K = 2;
    			}
    			else if (result == "2")
    			{
    				K = 1;
    			}
    			else if (result == "3")
    			{
    				K = 0.7;
    			}
    			else if (result == "4")
    			{
    				K = 0.5;
    			}
    			else
    			{
    				cout<<"You Have Not Entered a Corresponding Number Indicated."<<endl;
    				return 0;
    			}
    			
    			i = ((w/1000)* pow((h/1000),3)) /12;
    			cout<<"Here is a List of the Material Types that are Available:"<<endl;
    			cout<<"\t(1) Steel"<<endl<<"\t(2) Aluminum"<<endl<<"\t(3) Brass"<<endl;
    			cout<<"Choose a Material from the List above by Entering the Corresponding Number: ";
    			getline(cin,reply);
    
    			if(reply == "1") e = 200;
    			else if(reply == "2") e = 70;
    			else if(reply == "3") e = 105;
    			else
    			{
    				cout<<"You Have Not Entered a Corresponding Number Indicated."<<endl;
    				return 0;
    			}
    
    			Column rec(l,w,h,e,i);
    			a = rec.calc_rec_area();
    			g = rec.calc_gyr_rec();
    			rec.print2();
    			cout<<"From the Data Inputted, the Following Results were Obtained:"<<endl;
    			double crit_load = (pow(PI,2)*(e * 10e9)*(i))/ (pow(((l /1000)*K),2)); 
    			double crit_stress = (pow(PI,2)*(e * 10e9)/ (pow((((l /1000)*K)/g),2)));
    			cout<<"\t Buckling Load = "<<crit_load<<" N"<<endl;
    			cout<<"\t Buckling Stress = "<<crit_stress * 10e-06<<" MPa"<<endl; 
    
    		}
    		else 
    		{
    			cout<<"There is no Choice called "<<answer<<endl;
    			return 0;
    		}
    	}
    
    	else 
    	{
    		cout<<"You Have not Entered a Proper Argument. Please Try Again."<<endl;
    		return 0;
    	}
    
    	return 0;
    }
    sorry bout the smiley faces. i usually never get them when i post code. Thanks for the tip.
    C++ can hurt.

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    i'll try the ignore call but why is it like this? Isn't the compiler supposed to do this on its own?
    C++ can hurt.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It's a bit lengthy, but it's worthwhile to understand the differences between >> and getline(), so here's my rendition.

    istream objects have several methods/operators that take material stored in the istream buffer and place it in designated variables. Each method/operator works a little differently, and each has it's advantages and disadvantages.

    >> ignores any leading whitespace. When if finds the first non whitespace stuff in the buffer it starts the storing process. It stops the storing process when it finds the next whitespace stuff and leaves the terminating whitespace stuff in the buffer. This allows for serial input like:

    cin >> a >> b >> c

    without regard to user putting whitespace in front or behind a given input.

    getline() doesn't ignore white space. If there is leading whitespace or embedded white space it doesn't matter. It's all placed into the desired string (getline() can't place input into anything but a string, which may be considered an advantage or disadvantage depending how you look at it) as it comes along. This allows input like "Hello world" into a single variable, rather than separate variables based on whitespace separation as you would need with just >>. Since input doesn't terminate with finding of whitespace like with >>, getline() needs to be told when to stop, either after a certain number of char or if a certain char is found. If termination occurs because a certain char is found, then the terminating char is removed from the buffer. This allows use of getline() in situations like reading from file one line at a time using newline char as terminating char (it is the default terminating char, too) using a loop. If the terminating char weren't removed from the buffer by the first call to getline() when the next call to getline() came around again there would still be a newline char in the buffer as the first char that getline() would find and nothing would be put into the buffer. That wouldn't be good, so the the terminating char is removed from the buffer.

    As long as you use nothing but >> or nothing but getline() in a given program everything works out fine. But if you call >> before getline() and there is some whitespace left in the buffer that you really don't want to utilize, then there is a problem. The fix to remove any unwanted stuff in the buffer is the ignore() method. The defaults for ignore are 1 and newline char. That is frequently all you need, but you can use any int up to a given maximum size specified in <limits> instead of the default number of char to ignore, and you can use any char to terminate ignore(), too, (the terminating char is removed from the buffer just like with getline()).

    Could an alternate system have been devised... maybe. But that's the way it is.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    thanks for the help i got it working now
    C++ can hurt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. getline() problem
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 01:16 AM
  4. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 11:59 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM