Hello, as you can tell from my username I'm completely new at C++ and have no idea how to write a function that won't chuck up any errors!

What I'm trying to do is to compare an inputted student's ID number to ones already stored in my current array and this way the user can sort of search for a student in the record. Here's my current code:

#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;
};

bool stdIDComp(string, string); //function prototype for cas1 function

int main()
{
  const int NUMRECS = 3;
  const int IDNUM = 8;
  int option, i, j, total = 0;
  string word, stdIDInput, msg="Please type in the number of the corresponding \ntask you wish to perform then press enter.";
  
    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:
        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);
        if (stdIDComp(stdIDInput, stdRec[j].studentID)) //****HERE, why doesn't this work????
        {
            for (j = 0; j < NUMRECS ; j++)
            {
                cout << setw(12) << stdRec[j].studentID
                     << setw(21) << stdRec[j].studentName
                     << setw(13) << stdRec[j].courseCode
                     << setw(5)  << stdRec[j].creditPoint << endl;
            }
        }
        else
        {
            cout << "Not Found.\n";
        }
        pause ();
        break;
    case 2://to be filled
    case 3://to be filled
    case 4://to be filled
        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://to be filled
    case 6://to be filled
    case 7://to be filled
    default:
      cout << "No options from choices one to seven have been typed.";
      pause ();
      break;
    }
    
  } while (option!=0);
  return 0;
}

bool stdIDComp(string stdIDIn, string student) //the function for case1
{
    if (stdIDIn.compare(student) == 0) return true;
    return false;
}

The menu display and search works fine, but it crashes just before it displays its results from the search!!! I don't know why! Any direction would be MUCH APPRECIATED! Please and thank-you!

C++newbie chick.

Recommended Answers

All 38 Replies

if (stdIDComp(stdIDInput, stdRec[j].studentID))

As far as I can tell, you are using the variable 'j', but it has not been initialised yet. You are not in the for loop, so 'j' has no value.

Also, you should get rid of

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

because it's more annoying to the user than useful, and calling the system to execute an O/S command is expensive.

And as for:

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

3 calls to system? Just to do what

cout << "\n\n";
cin >> x;

does? And the above is completely standard!

On all of those cases where you commented "to be filled", you should put a "break;" statement after each one or you're going to get problems later on.

if (stdIDComp(stdIDInput, stdRec[j].studentID))

As far as I can tell, you are using the variable 'j', but it has not been initialised yet. You are not in the for loop, so 'j' has no value.

Okay, I've made adjustments but it still crashes! Here's the section where I know it's going wrong:

switch(option)
    {
    case 1://*** I don't know why it crashes!
        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);
        if (stdIDComp(stdIDInput, stdRec[j].studentID))
        {
            cout << setw(12) << stdRec[j].studentID
                 << setw(21) << stdRec[j].studentName
                 << setw(13) << stdRec[j].courseCode
                 << setw(5)  << stdRec[j].creditPoint << endl;
        }
        else
        {
            cout << "Not Found.\n";
        }
        pause ();
        break;
    case 2:// to be filled
    case 3:// to be filled
    case 4:// to be filled
        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:// to be filled
    case 6:// to be filled
    case 7:// to be filled
    default:
      cout << "No options from choices one to seven have been typed.";
      pause ();
      break;
    }
    
  } while (option!=0);
  return 0;
}

bool stdIDComp(string stdIDIn, string student)//function for case1
{
    int j;
    for (j = 0; j < NUMRECS ; j++)
    {
        if (stdIDIn.compare(student) == 0) return true;
        return false;
    }
}

Any and all direction would be awesome! Please and thanks!
C++newbie chick.

You made changes? Like what? Did you take care of the variable "j" which was not initialised? I initialised it to zero and then the progam no longer crashed. The j variable was apparently causing memory to be accessed that was beyond bounds, causing an access violation. So if the program isn't crashing anymore for me now, why is it for you?

Or maybe you should post your entire code so it can be run.

Or maybe you should post your entire code so it can be run.

Okay, so here's my original code before I tried to replace some of case1 with a function:

#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 main()
{
  int option, i, j, total = 0;
  string word, stdIDInput, msg="Please type in the number of the corresponding \ntask you wish to perform then press enter.";
  
    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:
        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);
        for (j = 0; j < NUMRECS ; j++)
        if (stdIDInput == stdRec[j].studentID)
        {  
            cout << setw(12) << stdRec[j].studentID
                 << setw(21) << stdRec[j].studentName
                 << setw(13) << stdRec[j].courseCode
                 << setw(5)  << stdRec[j].creditPoint << endl;    
        }
        pause ();
        break;
    case 2:
    case 3:
    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:
    case 6:
    case 7:
    default:
      cout << "No options from choices one to seven have been typed.";
      pause ();
      break;
    }
    
  } while (option!=0);
  return 0;
}

I'm trying to code it so that case1 can handle out of range input so I tried an if-else chain with a called function in it, but obviously my logic was wrong. Then I want to be able to code so that the user can choose to search another ID or go back to the main menu... but I need to get my function right first! Any suggestions would be awesome!
C++newbie chick.

Did you post your execution so we can see what went wrong? That usually helps.

Did you post your execution so we can see what went wrong? That usually helps.

Okay so the output is as so if you choose to search a student ID and input a valid number:

Please type in the number of the corresponding 
task you wish to perform, then press enter.

MAIN MENU

0.Exit
1. Search for a student
2. List students enrolled in a course
3. List students eligible to graduate
4. List all students
5. Update a student record
6. Add a student record
7. Delete a student record

Your choice is ->1
Please type in the student ID number
of the student you want to search,
then press enter.
16666666
Student ID Student Name    Course Code Credit Points

16666666   Jack Williams   3506        180

Press any key to continue...

I would like to know how I'd be able to code it so that it will cout "Not Found." if an invalid ID is inputted. And also how after the search, the user can opt to start a new search or go back to the main menu. Any direction would be sensational, please and thank-you!
C++newbie chick.

Well, if you check your studentID's and it's not there, output "Not Found".

Think about it. Where would you do this?

OK, first let's concentrate on the out of range input, then after that you can do the additional ID input.

The main problem here is that you need to test for high or low input ranges, but you are dealing with strings. The string "15000000" has no relation at all to the value of 15 million. It's just a string of symbols, no different than "Hello World". So you are going to have to change the varible stdIDInput to type int instead of type string.

Once you do that, then stdIDInput is an integer value that you can simply test for high or low values with an if statement, and loop back around with an error message and to re-enter the stdIDInput.

switch(option)
    {
      case 1:

    k:     cout << "Please type in the student ID number\nof the student you want
                    to search,\nthen press enter.\n";
           cin >> stdIDInput;
           if(stdIDInput < 15000000 || stdIDInput > 17000010)
           goto k;

Of course, you don't need to use an actual goto statement, but you get the idea. Also, just in case you didn't know, you can beep the beeper whenever an input in out of range, just add escape a in your cout statement. For example:

cout "\aYou entered a number too high";

Then you will get a beep.

commented: Quite worthless information... -4

OK, first let's concentrate on the out of range input, then after that you can do the additional ID input.

The main problem here is that you need to test for high or low input ranges, but you are dealing with strings. The string "15000000" has no relation at all to the value of 15 million. It's just a string of symbols, no different than "Hello World". So you are going to have to change the varible stdIDInput to type int instead of type string.

Once you do that, then stdIDInput is an integer value that you can simply test for high or low values with an if statement, and loop back around with an error message and to re-enter the stdIDInput.

switch(option)
    {
      case 1:

    k:     cout << "Please type in the student ID number\nof the student you want
                    to search,\nthen press enter.\n";
           cin >> stdIDInput;
           if(stdIDInput < 15000000 || stdIDInput > 17000010)
           goto k;

Of course, you don't need to use an actual goto statement, but you get the idea. Also, just in case you didn't know, you can beep the beeper whenever an input in out of range, just add escape a in your cout statement. For example:

cout "\aYou entered a number too high";

Then you will get a beep.

Ah, well just to make it more annoying for us students, it's been set that we HAVE to have the student ID as a string... otherwise, it would be a whole world easier to code it! And "escape a"? So if I code in \a, where would it escape to? Sorry to keep annoying you with questions, by the way... Oh, and we haven't learn how to use goto yet so... I don't know what that does...

Ah, well just to make it more annoying for us students, it's been set that we HAVE to have the student ID as a string... otherwise, it would be a whole world easier to code it! And "escape a"? So if I code in \a, where would it escape to? Sorry to keep annoying you with questions, by the way... Oh, and we haven't learn how to use goto yet so... I don't know what that does...

OK, then the way Walt posted would be sensible, since if stdIDInput wasn't found, because of an out of range number, then just handle that.

You know how to use the escape n (\n) for a newline, right. Well, just use escape a (\a) in your cout statement to make a beep, just like in my example.

A goto statement just says to goto some label, like k: in the example, and is just a simple form of loop which is rarely used because the while or for loops are better structured. I just used it as a quick and dirty loop : ).

And also how after the search, the user can opt to start a new search or go back to the main menu. Any direction would be sensational, please and thank-you!

With most menu systems, they are layered. For example, you start with the Main menu. If you select 1).Search then you go to a different screen, and now you are in search mode, and the Main menu doesn't appear anymore because you are down a layer in the menu.

So if you choose the search option, the screen should clear and you would have the search screen.
There you can keep entering student ID numbers (round and round ; ) or else exit back to the main menu.

Main menu items numbers 2,3 and 4 are just one time printouts and probably don't need a separate screen.

BTW, you can keep asking questions all you want, that's what this site is for : )

With most menu systems, they are layered. For example, you start with the Main menu. If you select 1).Search then you go to a different screen, and now you are in search mode, and the Main menu doesn't appear anymore because you are down a layer in the menu.

So if you choose the search option, the screen should clear and you would have the search screen.
There you can keep entering student ID numbers (round and round ; ) or else exit back to the main menu.

Main menu items numbers 2,3 and 4 are just one time printouts and probably don't need a separate screen.

BTW, you can keep asking questions all you want, that's what this site is for : )

YAY! Questions! And thanks so much for answering them, I think that may be enough to keep me going for a while! But if I do get stuck, you get a notification if someone replied to your post right? Just in case...

Ah, I tried to cout "Not Found." in an else statement after my if statement and it printed out "Not Found." when I typed an invalid student ID BUT it also printed out "Not Found" when it couts the match for a correct student ID! So I didn't know where else to put it...

YAY! Questions! And thanks so much for answering them, I think that may be enough to keep me going for a while! But if I do get stuck, you get a notification if someone replied to your post right? Just in case...

Ah, I tried to cout "Not Found." in an else statement after my if statement and it printed out "Not Found." when I typed an invalid student ID BUT it also printed out "Not Found" when it couts the match for a correct student ID! So I didn't know where else to put it...

Well, here is a clue: That if statement is in a loop, and every loop is a test for a match and if the first loop doesn't match then "not found" would print, right? Even though the next loop may find the match. You need to wait until ALL records have been searched before deciding if it was found or not.

And yes, questions! Even though we may sometimes seem terse in our response, it's only because we are "texting" and not really that way at all.

I made a boolean, which has the value if any user has been found. If after the for() loop the value is still false, no user has been found, and the message can be displayed.

bool found = false;
for (j = 0; j < NUMRECS ; j++) {
        if (stdIDInput == stdRec[j].studentID)
        {  
found = true;
            cout << setw(12) << stdRec[j].studentID
                 << setw(21) << stdRec[j].studentName
                 << setw(13) << stdRec[j].courseCode
                 << setw(5)  << stdRec[j].creditPoint << endl;    
        }
if (!found) {
cout << "The user was not found in our database.";
}
}
commented: uber-helpful! +1

The main problem here is that you need to test for high or low input ranges, but you are dealing with strings. The string "15000000" has no relation at all to the value of 15 million. It's just a string of symbols, no different than "Hello World". So you are going to have to change the varible stdIDInput to type int instead of type string.

Once you do that, then stdIDInput is an integer value that you can simply test for high or low values with an if statement, and loop back around with an error message and to re-enter the stdIDInput.

All completely worthless crap! Please ignore this advice. All this does is test a range. We still need to test exact IDs. And converting to int is a complete waste of time.

Also, just in case you didn't know, you can beep the beeper whenever an input in out of range, just add escape a in your cout statement.

More useless information (and annoying to the user).

So if you choose the search option, the screen should clear and you would have the search screen.

Another annoying piece of advice. Since Standard C++ cannot clear the screen, and most users find clearing the screen unnecessary and annoying, completely ignore this advice, too.

Well, here is a clue: That if statement is in a loop, and every loop is a test for a match and if the first loop doesn't match then "not found" would print, right? Even though the next loop may find the match. You need to wait until ALL records have been searched before deciding if it was found or not.

Finally! Useful advice. Thanks.

I made a boolean, which has the value if any user has been found. If after the for() loop the value is still false, no user has been found, and the message can be displayed.

bool found = false;
for (j = 0; j < NUMRECS ; j++) {
        if (stdIDInput == stdRec[j].studentID)
        {  
found = true;
            cout << setw(12) << stdRec[j].studentID
                 << setw(21) << stdRec[j].studentName
                 << setw(13) << stdRec[j].courseCode
                 << setw(5)  << stdRec[j].creditPoint << endl;    
        }
if (!found) {
cout << "The user was not found in our database.";
}
}

Ah! I tried using a boolean into my function but I could never figure out where on earth I could fit it in the for or if loop! I was also recommended to return a true or false with the boolean function, but I didn't know how... Thank you so much for the help! It's made everything so much easier to understand! I just have a question: why do you have to have "found = false" at the beginning?

I'll explain it in steps, just like in the method:

- Initialize the boolean as false: no use has been found yet.
- check each user, if it matches with the given ID.
- if a user matches, the boolean is true, a user HAS been found.
The loop exits...
- if the boolean is still false, that means the Boolean has not been changed, and no user has been found. If the boolean is true, a user has been found.
- print the not found message if the boolean is still false, if not, at least once a user has been found.

<< The main problem here is that you need to test for high or low input ranges, >>

All completely worthless crap! Please ignore this advice. All this does is test a range. We still need to test exact IDs. And converting to int is a complete waste of time.

I agree, completely worthless crap. My mistake in accessing the problem : (

<< Also, just in case you didn't know, you can beep the beeper whenever an input in out of range, just add escape a in your cout statement. >>

More useless information (and annoying to the user).

This information might be annoying, but it's not useless! Maybe someone wants to be annoying, or find a use for it in some program. I didn't advise the poster to use this in her program, but only that she could. This information was offered "just in case you didn't know" (i.e., for your information, i.e., for your learning).
No knowledge or information is useless.


<< You need to wait until ALL records have been searched before deciding if it was found or not. >>

Finally! Useful advice. Thanks.

Well, I had to hit the nail on the head sooner or later!

I'll explain it in steps, just like in the method:

- Initialize the boolean as false: no use has been found yet.
- check each user, if it matches with the given ID.
- if a user matches, the boolean is true, a user HAS been found.
The loop exits...
- if the boolean is still false, that means the Boolean has not been changed, and no user has been found. If the boolean is true, a user has been found.
- print the not found message if the boolean is still false, if not, at least once a user has been found.

EDIT: scratch the "Not Found" thing, I fixed that, but the option to search again is still confusing... any opinions?

Aaaah okay! Just a question: how do I fix the code so that it doesn't print "Not Found" along with the successful search results? Because at the moment when I run the program, it output's "Not Found" just above the retrieved student information! Also... could you tell me what's wrong with my current code below? I was trying to make it so that the user can opt to re-search or go back to the main menu, but it doesn't work... Any direction would be much appreciated! I'm still working on the code though, so it's not done yet at all! Please and thank-you!

#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 main()
{
  int option, i, j;
  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:
        cout << "Would you like to search for a student via their student ID? Y or N.\n";
        cin >> choice;
        while(choice == 'Y')
        {
        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(j = 0; j < NUMRECS; j++)
            {
                if(stdIDInput == stdRec[j].studentID)
                {
                    found = true;
                    cout << setw(12) << stdRec[j].studentID
                         << setw(21) << stdRec[j].studentName
                         << setw(13) << stdRec[j].courseCode
                         << setw(5)  << stdRec[j].creditPoint << endl;
                }
                
            }
                if(!found)
                {
                    cout << "Not Found.\n";
                }
        };
        pause ();
        break;
    case 2:
    case 3:
    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:
    case 6:
    case 7:
    default:
      cout << "No options from choices one to seven have been typed.";
      pause ();
      break;
    }
    
  } while (option!=0);
  return 0;
}

I just have a question: why do you have to have "found = false" at the beginning?

BOOL Found = false is the creation of the variable found so that it can be used later in line 5. And found is initialized to false to start out with. The same idea as int x = 0;. BTW, found could have been declared at the top of your program, just like your other variables are.

Something that is worth pointing out here is that it is good programming practice to declare your variables at the point at which they will first be used, rather than all at the top of your program. You want your variables to come into existence, be used, then go out of existence when they are no longer needed. This helps avoid bugs from data that might get changed in error, and the longer the variable remains in existence, the greater the chance of that happening.

It is good for found to be declared where it is, because it is used just 5 lines later, rather than at the top of the program. This also makes your code easier to understand, since the data and the code that uses that data are all there in one area. That way, we can clearly see that found is of type BOOL and is initialized to false without having to go looking for it's declaration, like I had to do with your variable j in the beginning. It's kind of like having a complete unit of code there with it's data.

In small programs like this, it doesn't really matter in a practical sense. But in real world programs, you may have a zillion variables, and it is good for them to just pop up into existence at the point they are needed, then pop out of existence when they are done. That way everything stays nice a tidy and neat and safe. Remember that a variable comes into existence at the point is was declared, and goes out of existence when program flow exits the brackets { } in which the variable was defined.

If you want your code to reflect better coding practice, try declaring your variables at the point of first need. For example, you could declare the stdIDInput variable after case 1 and i in it's own case and these variables would go out of existence after the closing bracket of the switch statement.

Just a question: how do I fix the code so that it doesn't print "Not Found" along with the successful search results? Because at the moment when I run the program, it output's "Not Found" just above the retrieved student information!

When I run your program, it works good for correct ID's but a wrong ID prints out:

Student ID Student Name Course Code Credit Points
Not found
"Please type in the student ID number
of the student you want to search,
then press enter.

Is that what you want, or should it just say Not Found
"Please type in the student ID....
?

I was trying to make it so that the user can opt to re-search or go back to the main menu, but it doesn't work...

OK, here is what I'm going to do: Below is code that will do what you want, just copy and paste it into your compiler. However, you will still have to figure some things out.
But first, you need to understand how the goto statement works - it's easy. Goto is a statement that causes program flow to jump to a label, like so:

do_again:          //This is a label and ends with a ":".
cout "I'm in a goto loop!";
goto do_again;

You can put a label (the label can be called anything you want, just end it with ":") anywhere in your program and then you can jump to that location with "goto label".

Now, I used a goto statement to make your program work. However, goto is bad bad programming practice. It's EVIL! LOL - not really evil, after all, it is a valid programming statement. However, for reasons to be discussed later, if you use goto in your program like I did, it will be condemned by academia forever. If you use goto at all, it should only be in the privacy of your own home - lol.

But I used it here just to show you the idea of how this could work. Once you see the logic of the program, then you HAVE to re-code it properly, without using any goto statements!
Also, consider using goto as a quick and dirty means of trying out program logic, just to see if it works, then re-code it properly.

In this program, when you are in search and entering ID's, enter a lower case x to return to the main menu. See how I did it with goto and code it right! Also, look at the program statements in case 1 and see what I've changed there. Here is the dirty and frowned upon and despised code:

#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 main()
    {
    int option, i, j;
    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}};
loop: do
       {
         displayMenu(msg);
         cin.clear();
         if(!(cin >> option))
           {if(!cin.eof())
              {cin.clear();
               cin >> word;
              }
             continue;
           }
     
         switch(option)
          {
           case 1:
           cout << "Would you like to search for a student via their student ID? Y or         N.\n";
           cin >> choice;
          while(choice == 'Y')
        {

    cout << "Please type in the student ID number\nof the student you want to search,\nthen press enter.\n";
    cin >> stdIDInput;
    
    cout << setiosflags(ios::left);
    bool found = false;
    for(j = 0; j < NUMRECS; j++)
    {
    if(stdIDInput == stdRec[j].studentID)
    {
    found = true;
    cout << "\nStudent ID Student Name Course Code Credit Points\n\n";
    cout << setw(12) << stdRec[j].studentID
    << setw(21) << stdRec[j].studentName
    << setw(13) << stdRec[j].courseCode
    << setw(5) << stdRec[j].creditPoint << endl;
    }
     
    }
    if(!found)
      {
	if (stdIDInput == "x")
	goto loop;
    cout << "Not Found.\n";
    }
    };
    pause ();
    break;
    case 2:
    case 3:
    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:
    case 6:
    case 7:
    default:
    cout << "No options from choices one to seven have been typed.";
    pause ();
    break;
    }
     
    } while (option!=0);
    return 0;
    }
commented: NO NO NO NO!!!! -4

No! No labels! Please!

Labels are about the worst programming you can do. It makes code unreadable, and is always solvable using loops. Labels can often crash your program, and are impossible to debug, because of the jumps ni your code.

Please don't use labels, it will only make things worse.

When I run your program, it works good for correct ID's but a wrong ID prints out:

Student ID Student Name Course Code Credit Points
Not found
"Please type in the student ID number
of the student you want to search,
then press enter.

Is that what you want, or should it just say Not Found
"Please type in the student ID....
?

Well what I'd like for it to simply spit out the search results of correct input, cout "Not Found" for incorrect input, then after displaying results or "Not Found", I would like for the user to have the option of trying to search again OR going back to the main menu... I tried doing it with a while loop, but after it cout the results of correct input or "Not Found" for incorrect input, it would keep displaying the prompt message "Please type in the student ID of the student you want to search, then press enter". I would then be stuck in a never ending loop of that prompt message after I'd entered my search, OR it would spit out the results of the last search and didn't let me search at all! I know it's just my loop that I've stuffed up, but I do not have the brain for C++ logic... probably because I'm still new at it and am not used to it yet though! Still working on it!

Great! Just keep trying: that is the only way to get 'C++ logic' :).
In some time, it will come to you, I am sure.

If I'd have to change the loop, I would do it like this:

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(j = 0; j < NUMRECS; j++)
            {
                if(stdIDInput == stdRec[j].studentID)
                {
                    found = true;
                    cout << setw(12) << stdRec[j].studentID
                         << setw(21) << stdRec[j].studentName
                         << setw(13) << stdRec[j].courseCode
                         << setw(5)  << stdRec[j].creditPoint << endl;
                }
                
            }
                if(!found)
                {
                    cout << "Not Found.\n";
                }
       cout << "Would you like to search for a another student ? Y or N.\n";
       cin >> choice;
} while (choice != 'N');
 pause ();
        break;

I made a do/while loop, so that when the user enters 1, it always searches one time. When the user enters a correct number, the user is displayed, if no users are found, the Not Found is displayed. Finally, the user is asked if he wants to search again.

OK, here is what I'm going to do: Below is code that will do what you want, just copy and paste it into your compiler. However, you will still have to figure some things out.
But first, you need to understand how the goto statement works - it's easy. Goto is a statement that causes program flow to jump to a label, like so:

do_again:          //This is a label and ends with a ":".
cout "I'm in a goto loop!";
goto do_again;

Do NOT Follow this advice!!!!

1) We do NOT - repeat NOT - recommend bad coding practices here!
2) We do NOT - repeat NOT - write the code for people!
3) We suggest - repeat suggest - corrections they can apply to their own code!
4) When posting illustrative code segments it is properly formatted code so people can learn the proper techniques! Yours is terrible. Doesn't matter where it started from. Reformat.

If you are going to recommend such bad practices there are other forums to peddle you bad advice. Here, we help people learn to code properly, and goto is rarely proper. And what's the instructor going to say when he gets a program with GOTOs? Immediate fail I'll bet. Especially since he hasn't taught it.

So leave your GOTOs to yourself and never bring them up again. And do NOT post working code "that will do what you want, just copy and paste it into your compiler". The term for that is cheating!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.