10 Replies - 7898 Views - Last Post: 16 September 2010 - 05:13 AM Rate Topic: -----

#1 Guest_Serena*


Reputation:

Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 02:44 AM

Is there a better way of doing this? My professor doesn't like using us using multiple if statements, he wants them nested, but i can't figure out how to do it.


	cout << "Do you want to update your first name? ";
		cin >> choice;
		if ( choice == static_cast< char >( 'y' ) )
		{
			cout << "Enter new first name: " ;
			cin >> newFname;
			strcpy( nameF, newFname );
		}
		cout << endl << "Do you want to update your last name? ";
		cin >> choice;
		if ( choice == static_cast< char >( 'y' ) )
		{
			cout << "Enter new last name: " ;
			cin >> newLname;
			strcpy( nameL, newLname );
		}
		cout << endl << "Do you want to update the age? ";
		cin >> choice;
		if ( choice == static_cast< char >( 'y' ) )
		{
			cout << "Enter new age: " ;
			cin >> newage;
			age = newage;
               }



Thank you very much,
Serena

Is This A Good Question/Topic? 0

Replies To: Trying to avoid using multiple if statements in C++

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5356
  • View blog
  • Posts: 27,315
  • Joined: 10-May 07

Re: Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 02:49 AM

while(choice!='y') { ... }

** Subject named to be more descriptive **
Was This Post Helpful? 0
  • +
  • -

#3 Alex6788  Icon User is offline

  • kitties == adorable


Reputation: 144
  • View blog
  • Posts: 1,667
  • Joined: 15-July 10

Re: Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 02:59 AM

Well you could use switch statements.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_Serena*


Reputation:

Re: Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 03:03 AM

while(choice!='y') { ... }

Does not work because it does not fall through, if user enters no for one of the prompt

e.g. The user can update the name, and not the age or age and not the name
Was This Post Helpful? 0

#5 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5356
  • View blog
  • Posts: 27,315
  • Joined: 10-May 07

Re: Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 03:07 AM

If you have three conditions, you need to check the results three times. Since it's pass or fail, then I don't know why you would use anything other than if/else logic.

The only thing I would suggest is not using the same variable for each. Dunno what else your professor expects.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_Serena*


Reputation:

Re: Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 03:10 AM

My professor will fail me if i use if statements like this

Do you think an ASSERT would be helpful in this case?
Was This Post Helpful? 0

#7 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5848
  • View blog
  • Posts: 12,707
  • Joined: 16-October 07

Re: Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 04:17 AM

Sorry, changed your topic title from C to C++. They're different beasts. It irked me.

Though, strangely, you're using c-strings in your result? Why not std::string?

Right, when there is repetition there's a chance for a method...

bool askEntry(const char *prompt) {
	cout << prompt;
	char choice;
	cin >> choice;
	return choice=='y';
}

bool askUser(const char *doYouPrompt, const char *valuePrompt) {
	bool takeEntry = askEntry(doYouPrompt);
	if (takeEntry) { cout << valuePrompt; }
	return takeEntry;
}

//...
if (askUser("Do you want to update your first name? ", "Enter new first name: ")) {
	cin >> newFname; strcpy( nameF, newFname );
}

if (askUser("Do you want to update your last name? ", "Enter new last name: ")) {
	cin >> newLname; strcpy( nameL, newLname );
}

if (askUser("Do you want to update the age? ", "Enter new age: ")) {
	// I really don't understand the reading value just to put somewhere else
	// cin >> newage;
	// age = newage;
	cin >> age;
}


Was This Post Helpful? 1
  • +
  • -

#8 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 04:30 AM

Not really sure what your professor is looking for, but if I knew what the rest of your program looked like I could give you a better answer. Is your program OO? If so, it would be easy to write a while-loop that's condition was boolean, create separate bool functions for each question and return false if the answer is 'n'. Then initialize your class member variables to the new input. You would still have if statements for each question, but they would be separated. You wouldn't even need the functions actually, but if your professor's only complaint is the fact there are so many if-statements, not sure what else to say. Not sure how he intends to nest three logically separate questions in this case.
Was This Post Helpful? 0
  • +
  • -

#9 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 04:38 AM

baavgai beat me to, but minus OOP so that might be more helpful in this case.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Serena*


Reputation:

Re: Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 04:42 AM

View Postbaavgai, on 16 September 2010 - 03:17 AM, said:

Sorry, changed your topic title from C to C++. They're different beasts. It irked me.

Though, strangely, you're using c-strings in your result? Why not std::string?

Right, when there is repetition there's a chance for a method...

bool askEntry(const char *prompt) {
	cout << prompt;
	char choice;
	cin >> choice;
	return choice=='y';
}

bool askUser(const char *doYouPrompt, const char *valuePrompt) {
	bool takeEntry = askEntry(doYouPrompt);
	if (takeEntry) { cout << valuePrompt; }
	return takeEntry;
}

//...
if (askUser("Do you want to update your first name? ", "Enter new first name: ")) {
	cin >> newFname; strcpy( nameF, newFname );
}

if (askUser("Do you want to update your last name? ", "Enter new last name: ")) {
	cin >> newLname; strcpy( nameL, newLname );
}

if (askUser("Do you want to update the age? ", "Enter new age: ")) {
	// I really don't understand the reading value just to put somewhere else
	// cin >> newage;
	// age = newage;
	cin >> age;
}



Thank you

Nice rep btw, you are 1 off from being elite
Was This Post Helpful? 0

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Trying to avoid using multiple if statements in C++

Posted 16 September 2010 - 05:13 AM

View PostSerena, on 16 September 2010 - 07:10 PM, said:

My professor will fail me if i use if statements like this

There are three separate inputs being collected.
One if() for each input is not going to get you a fail from a sane marker.

If there is anything to the above statement then it is your professor suggesting that asking those questions one by one is a hugely aggravating way for a user to have to interact with your program.
I think you are very confused about what your teacher is telling you.
If they want nested if() tests it's because they are suggesting that you need to combine the input collection.
So something like
"What would you like to change? Enter the relevant letter. First name (f), Last name(l), Age(a) - Quit(q)"
That would collect a input that requires multiple handling and thereby justifies nesting (or better a switch).
Do you think that might be what you are really being asked to consider?

Or is it just that you have been punished for badly using multiple if()'s before in another circumstance for another challenge and because you didn't understand what the feedback was you are now applying it to different case where the feedback doesn't apply?
Is that possible?

View PostSerena, on 16 September 2010 - 07:10 PM, said:

Do you think an ASSERT would be helpful in this case?

I certainly do not.
Do you know what an assert is for?
How would you use it here to make a program action choice?

EDIT - Add a quit option to the question

This post has been edited by janotte: 16 September 2010 - 05:35 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1