2 Replies - 635 Views - Last Post: 30 October 2011 - 12:24 AM Rate Topic: -----

#1 C++ newb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-September 11

Never-ending loop- problem!

Posted 29 October 2011 - 04:29 AM

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!!! I don't know why it goes wrong!
        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;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Never-ending loop- problem!

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6245
  • View blog
  • Posts: 24,013
  • Joined: 23-August 08

Re: Never-ending loop- problem!

Posted 29 October 2011 - 04:54 AM

if(found = true)


= for assignment, == for comparison

You should learn to use your debugger and step through the code line by line.
Was This Post Helpful? 1
  • +
  • -

#3 C++ newb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 04-September 11

Re: Never-ending loop- problem!

Posted 30 October 2011 - 12:24 AM

View PostJackOfAllTrades, on 29 October 2011 - 04:54 AM, said:

if(found = true)


= for assignment, == for comparison

You should learn to use your debugger and step through the code line by line.

Agh! Thank goodness for you and your eye for detail! Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1