Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,398 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,107 people online right now. Registration is fast and FREE... Join Now!




Do While Loop

 
Reply to this topicStart new topic

Do While Loop, Cant get loop to exit

the_rook13
23 Oct, 2006 - 09:56 AM
Post #1

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 27


My Contributions
This is just a little bit of the code but I cant do anything till i get this loop to exit. Can someone please help me and tell me why this loop wont exit.
CODE

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
  double arrystudent_grades[4][5]={{5,8,9,8,6},{2,5,10,5,3},{8,8,8,8,9},{10,9,8,7,6}};
  int grade;
  int student;
  int num_students=4;
  int num_grades=5;
char menu_response;
int student_id;
  char studentName[4][35] = {"Amy","Ben","Cathy","Doug"};


  do{
  cout<<"\t***************************************\n";
  cout<<"\t**                                   **\n";
  cout<<"\t**      Success Tutoring Center      **\n";
  cout<<"\t**                                   **\n";
  cout<<"\t***************************************\n";
  cout<<"\n\n";
  cout<<"A-Change a single grade\n";
  cout<<"B-Output progress report\n";
  cout<<"C-Calculate fifth grade and output report\n";
  cout<<"\n";
  cout<<"\nPlease make a choice: ";
  cin>>menu_response;

}
while ((menu_response!='a') || (menu_response!='b') || (menu_response!='c'));

    



/*to see grades*/
for(student=0;student<num_students;++student)
   {
   cout<<"\nGrades for students"<<student+1<<":\t";
  for(grade=0;grade<num_grades;++grade)
  cout<<arrystudent_grades[student][grade]<<"\t";
  cout<<"\n";
}



  
  


  
  
    
  
    

    system("PAUSE");
    return 0;
}


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Do While Loop
23 Oct, 2006 - 10:27 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
you may wish to change this:
CODE

while ((menu_response!='a') || (menu_response!='b') || (menu_response!='c'));

to this:
CODE

while ((menu_response=='a') || (menu_response=='b') || (menu_response=='c'));

In your first attempt, the statement really says to continue while the response is not equal to a OR not eqaul to b OR not equal to c...no matter what is entered, one of those will evaluate to true.
User is offlineProfile CardPM
+Quote Post

the_rook13
RE: Do While Loop
23 Oct, 2006 - 11:57 AM
Post #3

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 27


My Contributions
I am trying to only exit the loop when it is a,b,or c because under each one i have to do more stuff. I am probally going at it all wrong but its the only way i know how.
User is offlineProfile CardPM
+Quote Post

the_rook13
RE: Do While Loop
23 Oct, 2006 - 12:38 PM
Post #4

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 27


My Contributions
QUOTE(the_rook13 @ 23 Oct, 2006 - 12:57 PM) *

I am trying to only exit the loop when it is a,b,or c because under each one i have to do more stuff. I am probally going at it all wrong but its the only way i know how.

To make it easier its suppose to look like this but i am only doing one thing at a time, or doing what i know how to do.


Welcome to Success Tutoring Center
==================================
A - Change a single grade
B - Output progress report
C - Calculate fifth grade and output grade report

Please enter your choice : K

Invalid input ! Please enter again

Welcome to Success Tutoring Center
==================================
A - Change a single grade
B - Output progress report
C - Calculate fifth grade and output grade report

Please enter your choice : A

Please enter the student ID : 8

ID is out of bound !

Do you want to continue? Press 'E' to exit : Y


Welcome to Success Tutoring Center
==================================
A - Change a single grade
B - Output progress report
C - Calculate fifth grade and output grade report

Please enter your choice : A

Please enter the student ID : 2

Please specify which quiz to change grade : 7

Quiz number is out of bound !

Do you want to continue? Press 'E' to exit : Y

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Do While Loop
23 Oct, 2006 - 03:19 PM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
Well, you don't really want to exit the loop if it's a,b,or c, as those are valid entries - entries which tell you to complete another action, and then ask the user if they wish to continue. If you modify the statement as I've shown above (although there are many more ways to structure it), then you'll stay in the loop for a,b, and c - you'll then have to expand the functionality of the loop.Something like:
CODE

do
{
   if(response=='a')
      //complete action
   else if(response=='b')
     //complete action
   else
     //complete action

   cin>>response;
}while ((menu_response=='a') || (menu_response=='b') || (menu_response=='c'));

Of course, this is only a snall example, barely above pseudocode, so it's not meant to be compiled and run, but to demonstrate the idea.

I would suggest prompting the user for input, and placing the type of structure shown in a while loop, then using a switch statement to trigger the events on the value of response.
User is offlineProfile CardPM
+Quote Post

the_rook13
RE: Do While Loop
23 Oct, 2006 - 03:40 PM
Post #6

New D.I.C Head
*

Joined: 2 Oct, 2006
Posts: 27


My Contributions
thanks man i didnt even think about that
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:02AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month