Hello guys, I'm trying to make it so that the user can opt to change student's course code or credit points (they can choose to change neither, either or both) in case 5 of my main switch statement. The problem is, the loop doesn't end after you choose to do anything! The search for a student works and the prompt works, but after that, it's just a neverending loop! Any direction or advice would be awesome! Here's my whole code (I've tried making functions for things as well, but I'm no good at it!):

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void clrscr()
{
  system("cls");
}

void pause()
{
  system("echo.");system("echo.");system("pause");
}

void displayMenu(string msg)
{
  clrscr();
  cout << msg << "\n\nMAIN MENU\n"
"\n0. Exit"
"\n1. Search for a student"
"\n2. List students enrolled in a course"
"\n3. List students eligible to graduate"
"\n4. List all students"
"\n5. Update a student record"
"\n6. Add a student record"
"\n7. Delete a student record"
"\n\nYour choice is ->";
};

const int MAXRECORD = 500;
struct stdRecord
{
string studentID;
string studentName;
int courseCode;
int creditPoint;
};

const int NUMRECS = 3;

int i;

int main()
{
  int option, i;
  string word, stdIDInput, msg="Please type in the number of the corresponding \ntask you wish to perform then press enter.";
  char choice;
  
  stdRecord stdRec[NUMRECS]={{"15000000","Joshua Andrew Smith", 3506, 240},
                               {"16666666", "Jack Williams", 3506, 180},
                               {"17000010", "Lily Jones", 3639, 110}};
  do
  {
    displayMenu(msg);
    cin.clear();
    if(!(cin >> option))
    {
      if(!cin.eof())
      {
        cin.clear();
        cin >> word;
      }
     continue;
    }
   
    switch(option)
    {
    case 1:
        do
        {
        cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n";
        cin >> stdIDInput;
        cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
        cout << setiosflags(ios::left);
        bool found = false;
        for(i = 0; i < NUMRECS; i++)
        {
            if(stdIDInput == stdRec[i].studentID)
            {
                found = true;
                cout << setw(12) << stdRec[i].studentID
                     << setw(21) << stdRec[i].studentName
                     << setw(13) << stdRec[i].courseCode
                     << setw(5)  << stdRec[i].creditPoint << endl;
            }
        }
        if(!found)
        {
            cout << "Not Found.\n";
        }
        cout << "Would you like to search for a student again? Y or N.\n";
        cin >> choice;
        }while(choice != 'N');
        pause ();
        break;
    case 2:
        int courseCodeIn;
        do
        {
        cout << "Please type in a valid course code (the SCM offers courses 3506, 3633, 3634 \nand 3639) to display students in that course, then press enter.\n"
             << "Note: there may or may not be students enrolled ina a course,\nin the case of the latter, 'Not Found' will be displayed.";
        cin >> courseCodeIn;
        cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
        cout << setiosflags(ios::left);
        bool found = false;
        for(i = 0; i < NUMRECS; i++)
        {
            if(courseCodeIn == stdRec[i].courseCode)
            {
                found = true;
                cout << setw(12) << stdRec[i].studentID
                     << setw(21) << stdRec[i].studentName
                     << setw(13) << stdRec[i].courseCode
                     << setw(5)  << stdRec[i].creditPoint << endl;
            }
        }
        if(!found)
        {
            cout << "Not Found.\n";
        }
        cout << "Would you like to search via a course code again? Y or N.\n";
        cin >> choice;
        }while(choice != 'N');
        pause ();
        break;
    case 3:
        cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
        cout << setiosflags(ios::left);
        for(i = 0; i < NUMRECS; i++)
        {
            if(stdRec[i].creditPoint == 240)
            {
                cout << setw(12) << stdRec[i].studentID
                     << setw(21) << stdRec[i].studentName
                     << setw(13) << stdRec[i].courseCode
                     << setw(5)  << stdRec[i].creditPoint << endl;
            }
        }
        pause ();
        break;
    case 4:
        cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
        cout << setiosflags(ios::left);
        for (i = 0; i < NUMRECS; i++)
            cout << setw(12) << stdRec[i].studentID
                 << setw(21) << stdRec[i].studentName
                 << setw(13) << stdRec[i].courseCode
                 << setw(5)  << stdRec[i].creditPoint << endl;             
        pause ();
        break;
    case 5: // ***HERE! Why won't this work?
        int decision, courseCodeUpdate, creditPointsUpdate;
        do
        {
        cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n";
        cin >> stdIDInput;
        cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
        cout << setiosflags(ios::left);
        bool found = false;
        for(i = 0; i < NUMRECS; i++)
        {
            if(stdIDInput == stdRec[i].studentID)
            {
                found = true;
                cout << setw(12) << stdRec[i].studentID
                     << setw(21) << stdRec[i].studentName
                     << setw(13) << stdRec[i].courseCode
                     << setw(5)  << stdRec[i].creditPoint << endl;
            }
        }
        if(!found)
        {
            cout << "Not Found.\n";
        }
        if(found = true)
        {
            do
            {
                cout << "Please type in the number\n of the corresponding choice below, then press enter.\n\n"
                     << "Z. Exit\nA. Update course code\nB. Update credit points\nC. Update both course code and credit points\n";
                cin >> decision;
                if (decision == 'A')
                {
                    cout << "Please type in the updated course code.\n";
                    cin >> courseCodeUpdate;
                    stdRec[i].courseCode = courseCodeUpdate;
                    cout << "The student's course code has now been updated to "
                         << stdRec[i].courseCode << endl;
                }
                else if (decision == 'B')
                {
                    cout << "Please type in the updated credit points.\n";
                    cin >> creditPointsUpdate;
                    stdRec[i].creditPoint = creditPointsUpdate;
                    cout << "The student's credit points have now been updated to "
                         << stdRec[i].creditPoint << endl;
                }
                else if (decision == 'C')
                {
                    cout << "Please type in the updated course code.\n";
                    cin >> courseCodeUpdate;
                    stdRec[i].courseCode = courseCodeUpdate;
                    cout << "The student's course code has now been updated to "
                         << stdRec[i].courseCode
                         << "\nNow please type in the updated credit points.\n";
                    cin >> creditPointsUpdate;
                    stdRec[i].creditPoint = creditPointsUpdate;
                    cout << "The student's credit points have now been updated to "
                         << stdRec[i].creditPoint << endl;
                }
                else
                {
                    cout << "No valid character from options N, A, B, or C have been chosen.";
                }
            } while (decision != 'Z');
        }
    cout << "Would you like to search for a student again? Y or N.\n";
    cin >> choice;
    } while (choice != 'N');
    pause ();
    break;
    case 6: 
        cout << "Dude";
        break;
    case 7: cout << "wutup?";
        break;
    default:
      cout << "No options from choices one to seven have been typed.";
      pause ();
      break;
    }
    
  } while (option!=0);
  return 0;
}

Edited 5 Years Ago by C++newbie chick: n/a

Hola chica de newbiliciousness

after briefly lookin' at ye' code, try this:

//instead of
}while(choice != 'N');

//try
}while(choice != 'n' || choice != 'N');
//and
}while(choice != 'z' || choice != 'Z');

//cool alternatives
}while(toupper(choice) != 'N');
//or
}while(tolower(choice) != 'z');

Edited 5 Years Ago by Clinton Portis: n/a

After a breif look, I found that in line 154, if you change the variable "decision" to type char instead of type int, then the endless loop does not happen. Then you are able to change the course code, and the program aknowledges that the course code has been updated. However, when I press Z to exit, it doesn't. I don't have time to deal with this further now, but I will later on tonight.

Comments
awesome

Actually, I think it did exit when I pressed "Z". Anyway, just changing the variable "decision" to type char fixes the endless loop. Also, I will give you a clue to a problem on line 213: you need an else-if statement there instead of just an else. And it has something to do with Zebras.

Actually, I think it did exit when I pressed "Z". Anyway, just changing the variable "decision" to type char fixes the endless loop. Also, I will give you a clue to a problem on line 213: you need an else-if statement there instead of just an else. And it has something to do with Zebras.

MandrewP, I can always depend on you and your eye for detail! Thank you so much! It works fine now... and thanks for the heads up on my cout message... I originally had it to N, but changed it to Z to see if my decision and choice variables were getting mixed up or something... but they weren't, silly beginner mistakes!

OK, and here is a tip if you ever want to use it:

cin >> choice;
choice &= 223;   //converts to uppercase.

Line 2 makes choice uppercase always. If you enter lower case, it comes out of line 2 as uppercase, and if you enter uppercase then it stays uppercase. So no matter what, coming out of line 2 is only uppercase. That way, with just this one simple line, you don't have to test later for upper or lowercase because it is always uppercase!

If you do use this in one of your programs, you should be able to explain how it works:

Lowercase letters are 32 higher than uppercase letters in their ASCII values. So to convert lower to upper you just need to subtract 32 from the lowercase letter: choice - 32 or a = 97 and A = 65 so a - 32 = 65 (A).

Of course, if choice is already uppercase and you subtrct 32, then there is an error. Here is the binary representation of 97 (a): 01100001. The 7th bit (second from the left) has a value of 32. So if we can just switch this bit to 0 then we subtracted 32 from this number. And that is excatly what happens when we "and" a number with 223.

1100001 ----> lowercase a
1011111 ----> the number 223
----------
1000001 ----> the "anded" result, which is uppercase A.

Notice the zero in the 7th bit of 223. The result of anding with zero is always zero. Notice that the rest of 223 are ones. Anding with ones always results in straight "pass through", meaning no digits are changed for the result. Lowercase letters always have the 7th bit zero, and uppercase letters always have the 7th bit one. In otherwords, the only difference between upper and lowercase letters is bit 7.

Here is a short program if you want to see this is action:

#include<iostream>
using namespace std;
int main()
{
   char choice = 0; 
   while(1)
    {
     cout << "\nEnter char: \n";
     cin >> choice;
     choice &= 223;
     cout << choice;
    }
	
    return 0;
}

Of course, choice &= 223 is the same as choice = choice & 223. Also, remember that && is used with logical values, like you use in your if statements and & is used for anding the actual bits in the two operands.

OK, I'll shut up now : )

OK, and here is a tip if you ever want to use it:

cin >> choice;
choice &= 223;   //converts to uppercase.

Line 2 makes choice uppercase always. If you enter lower case, it comes out of line 2 as uppercase, and if you enter uppercase then it stays uppercase. So no matter what, coming out of line 2 is only uppercase. That way, with just this one simple line, you don't have to test later for upper or lowercase because it is always uppercase!

If you do use this in one of your programs, you should be able to explain how it works:

Lowercase letters are 32 higher than uppercase letters in their ASCII values. So to convert lower to upper you just need to subtract 32 from the lowercase letter: choice - 32 or a = 97 and A = 65 so a - 32 = 65 (A).

Of course, if choice is already uppercase and you subtrct 32, then there is an error. Here is the binary representation of 97 (a): 01100001. The 7th bit (second from the left) has a value of 32. So if we can just switch this bit to 0 then we subtracted 32 from this number. And that is excatly what happens when we "and" a number with 223.

1100001 ----> lowercase a
1011111 ----> the number 223
----------
1000001 ----> the "anded" result, which is uppercase A.

Notice the zero in the 7th bit of 223. The result of anding with zero is always zero. Notice that the rest of 223 are ones. Anding with ones always results in straight "pass through", meaning no digits are changed for the result. Lowercase letters always have the 7th bit zero, and uppercase letters always have the 7th bit one. In otherwords, the only difference between upper and lowercase letters is bit 7.

Here is a short program if you want to see this is action:

#include<iostream>
using namespace std;
int main()
{
   char choice = 0; 
   while(1)
    {
     cout << "\nEnter char: \n";
     cin >> choice;
     choice &= 223;
     cout << choice;
    }
	
    return 0;
}

Of course, choice &= 223 is the same as choice = choice & 223. Also, remember that && is used with logical values, like you use in your if statements and & is used for anding the actual bits in the two operands.

OK, I'll shut up now : )

Gah! Absolute pearls of wisdom is what you're made of! So, this is just like using toupper? And... since we're on the subject of nifty tips, would you happen to be able to take a quick look at my attempt of a binary search? I only know how to cout student records via the linear way, but how could I do it for a binary search? I'm not even sure if my binary search is right, but I can't test it yet because it's not finished! Please, please, please and thank-you!

case 6:
        int first = 0;
        int last = NUMRECS;
        int mid = (first + last)/2;
        cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n";
        cin >> stdIDIn;
        cout << "\nStudent ID  Student Name         Course Code  Credit Points\n\n";
        cout << setiosflags(ios::left);
        bool found = false;
        while (first <= last && !found)
        {
            if (stdRec[mid] == stdIDIn)
            {
                found = true;
                cout << //I'm, stuck here!
        pause ();
        break;
This question has already been answered. Start a new discussion instead.