1 Replies - 219 Views - Last Post: 04 December 2012 - 10:56 AM Rate Topic: -----

#1 andyc222  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-November 12

How to assign a known value to a variable when user enters string

Posted 04 December 2012 - 10:02 AM

I am developing a program to analyse the strength of timber. I want my user to enter a strength class, such as C14 or D40 and then for a known value of that strength class to be assigned to a variable strength. As you can see below my attempt at this is very ugly, although it does work. Is there a better method I could use where instead of using my long horrible code below, my user actually enters the string C14 and a then a known value for C14 is assigned to a variable strength.

I am new to coding so any advice on a more efficient method would be much appreciated, thankyou!

 
void BendingStrength()
{
	cout << endl;
	cout << "Please enter your solid timbers strength class where C14=0, C16=1, C18=2, C20=3, C22=4, C24=5, C27=6, C30=7, C35=8, C40=9, C45=10,  C50=11, D30=12,  D35=13, D40=14, D50=15, D60=16, D70=17: ";
	int StrengthClass[1][18]={14000,16000,18000,20000,22000,24000,27000,30000,35000,40000,45000,50000,30000,35000,40000,50000,60000,70000};
		cin >> strengthclassnumber;

	switch(strengthclassnumber)
	{
	case 0:
		{ 
			fk=StrengthClass[0][0];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}
	case 1:
		{ 
			fk=StrengthClass[0][1];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}
	
	case 3:
		{ 
			fk=StrengthClass[0][3];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 4:
		{ 
			fk=StrengthClass[0][4];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 5:
		{ 
			fk=StrengthClass[0][5];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 6:
		{ 
			fk=StrengthClass[0][6];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 7:
		{ 
			fk=StrengthClass[0][7];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 8:
		{ 
			fk=StrengthClass[0][8];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 9:
		{ 
			fk=StrengthClass[0][9];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 10:
		{ 
			fk=StrengthClass[0][10];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 11:
		{ 
			fk=StrengthClass[0][11];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 12:
		{ 
			fk=StrengthClass[0][12];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 13:
		{ 
			fk=StrengthClass[0][13];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 14:
		{ 
			fk=StrengthClass[0][14];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 15:
		{ 
			fk=StrengthClass[0][15];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 16:
		{ 
			fk=StrengthClass[0][16];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 17:
		{ 
			fk=StrengthClass[0][17];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		case 18:
		{ 
			fk=StrengthClass[0][18];
			cout << "The benching strength, fk, of your beam is " << fk << "kN/m2";
			cout << endl;
			break;
		}

		default:
		{ 
			cout << "This value has not been assigned to a strength class."
				<< endl;
			cout <<endl;
			return BendingStrength();
		}

		}
	}



Is This A Good Question/Topic? 0
  • +

Replies To: How to assign a known value to a variable when user enters string

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: How to assign a known value to a variable when user enters string

Posted 04 December 2012 - 10:56 AM

int StrengthClass[1][18]={14000,16000,18000,20000,22000,24000,27000,30000,35000,40000,45000,50000,30000,35000,40000,50000,60000,70000};


An array of size 1 is just wrong. Why are you trying to make this a two-dimensional array anyway?

int StrengthClass[] = {14000,16000,18000,20000,22000,24000,27000,30000,35000,40000,45000,50000,30000,35000,40000,50000,60000,70000};


so C22 would be StrengthClass[4]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1