4 Replies - 419 Views - Last Post: 10 February 2014 - 03:44 PM Rate Topic: -----

#1 peterbrown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-November 13

sentinel control loop C++

Posted 09 February 2014 - 12:37 AM

hello everyone.
I cant figure out how to use a sentinel control loop using a for loop that takes 'q' as input to terminate the inouts from the use.
i just need the concept rest i think i can do it.
can anyone please assist me in the for loop condition.
basically the porogram I a masked to code is this:
Write a code that takes the marks (out of 100) of subjects as an input from user and
calculate the sum of that marks using FOR loop. And the loop should repeat until the
user press q from a keyboard.

I reiterate i don't need the whole code. i just need the condition that i am supposed to use.

please help me out. I have been stuck on this for days.
thanks a lot.

Is This A Good Question/Topic? 0
  • +

Replies To: sentinel control loop C++

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 771
  • View blog
  • Posts: 2,250
  • Joined: 20-March 10

Re: sentinel control loop C++

Posted 09 February 2014 - 01:12 AM

You would take input from the user and us an 'if' statement to check if the input is 'q'

if 'q' then break;

so the keyword you are looking for is break

Reference

Regards

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: sentinel control loop C++

Posted 10 February 2014 - 11:40 AM

This is not a code-writing service, so you're going to need to show us what you have come up with and we can nudge you in the right direction. The problem itself is very simple. You get input (as a char) from the user and use the equality operator (==) to compare it to the sentinel value (q). If it matches, simple exit the loop.
Was This Post Helpful? 0
  • +
  • -

#4 peterbrown  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-November 13

Re: sentinel control loop C++

Posted 10 February 2014 - 11:48 AM

View Postsnoopy11, on 09 February 2014 - 01:12 AM, said:

You would take input from the user and us an 'if' statement to check if the input is 'q'

if 'q' then break;

so the keyword you are looking for is break

Reference

Regards

Snoopy.


I coded this as an interpretation to what you said. I am thankful to you for the prompt reply but I can't hunt the error. I would be thankful if you could help me out.
thanks a lot.

#include <iostream>

using namespace std;

int main()
{
	int marks, total = 0;
	for (marks = 0; marks !='q'; )
	{
		cin >> marks;
		
		total = total + marks;

		
	}
	cout << total;
	system("pause");
}


This post has been edited by JackOfAllTrades: 10 February 2014 - 03:01 PM
Reason for edit:: Fixed code tags

Was This Post Helpful? 0
  • +
  • -

#5 Black_Epiphany  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 64
  • Joined: 02-February 14

Re: sentinel control loop C++

Posted 10 February 2014 - 03:44 PM

Write an if statement to break the loop if a condition is met. I'd also recommend doing a while loop instead of a for loop and creating a boolean variable named 'done'. Either way works though.

http://msdn.microsof...y/37zc9d2w.aspx
http://www.cplusplus...cctype/isdigit/

do{

//get user input
//blah blah blah

if(/*condition to exit loop*/){

done = true;

}

}while(!done)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1