0

I want to clear the screen after select option, but I don't know somehow it is not working. It will show the things in Display() function and the Create New Purchase and other things. It is because in the while loop?

while (selection != -1) // While for create new purchase 
        {
            cout << "Create New Purhcase" << endl << endl;
            cout << "1. Display Item" << endl;
            cout << "2. Create a New Purchase" << endl << endl <<endl;
            cout << "0. Back to Main Menu" << endl; 
            cout << "Enter Option:";

            cin >> selection;

            //Back to main menu 
            if (selection == 0)
            {
                system("CLS");
                break;
            }
            if (selection == 1)
            {
                system("CLS");
                cout << "Display Menu" << endl;
                Display();
            }

void Display()
{
system("CLS");
temp = itemHead; //start at the first node
cout << "Dispaly Menu" << endl << endl; 

while (temp != NULL)
{
    cout << "ID:" << temp->itemid << endl; 
    cout << "Item Name:" << temp->name << endl;
    cout << "Item Type:" << temp->type << endl;
    cout << "Item Price" << temp->cost << endl;
    cout << endl << endl;
    temp = temp->next; //forward to the next node
}

}

Pepper
  • 31
  • 5
  • 1
    http://stackoverflow.com/questions/19913446/why-should-the-system-function-be-avoided-in-c-and-c –  Feb 17 '17 at 15:53
  • @Pepper: Reduce you problem to a concrete, minimal example that readers here can try, and post that code in its entirety. For now I'm voting to close the question as lacking such a reproducible example. – Cheers and hth. - Alf Feb 17 '17 at 15:55
  • @NeilButterworth: You quoted a link to a question about why `system` should be avoided, to a beginner trying to clear the screen in a Windows program. Context does matter. In this context that link is untrue. – Cheers and hth. - Alf Feb 17 '17 at 16:03
  • What exactly is not working? This thing *should* work (in as: clear the screen) if it runs on Windows in a normal environment. Even though it's probably not a good idea, because the program makes many assumptions: it assumes it's running on Windows and that it's running inside of a console window. Both assumptions could be wrong. – Christian Hackl Feb 17 '17 at 16:04
  • With all due respect to concerned commentors, quit arguing. As for the post, which while loop are you discussing? I see 2 while loops. – BusyProgrammer Feb 17 '17 at 16:10
  • The first while loop. Since the first system("CLS") is not working, so I added another one, but still not working. – Pepper Feb 17 '17 at 16:11
  • @Pepper: Are you sure you're running this in a Windows console window? And not, say, in some Linux? – Cheers and hth. - Alf Feb 17 '17 at 16:13
  • Yes, I am pretty sure. I have did the similar code and that is working. – Pepper Feb 17 '17 at 16:17
  • The `system("CLS")` in the first while loop works fine for me. – BusyProgrammer Feb 17 '17 at 16:18
  • Unfortunately, not for me. I did another system("CLS") in else if (selection ==2) is working fine after I did longer code instead of just cout<<"Display Menu"< – Pepper Feb 17 '17 at 16:32
  • @Pepper This is a longshot and possibly stupid, but try cin.ignore() before the cls. – MerajA Feb 17 '17 at 16:54
  • I found the problem, see my answer below @Pepper. – BusyProgrammer Feb 17 '17 at 18:47

1 Answers1

-1

The problem here is that your code does not stop after printing the Display() function, because it is a part of the while loop. As a result, it prints the menu, and then goes on to print the options again.

To ensure that the loop pauses after the menu is printed, change your code to:

if (selection == 1)
{
    system("CLS");
    cout << "Display Menu" << endl;
    Display();
    cout << endl << endl;
    system("pause");
}

The system("pause") is also a part of the algorithm file, so you don't need to include anything. This way, the entire menu will be printed from your Display() function, then some newlines, and finally a prompt to press the ENTER key. Until you hit the ENTER key, the while loop will not continue.

NOTE: there are other ways to do it too, but this is the simplest and shortest. If you have any questions regarding my answer, or if my answer is not working, then please inform me in the comments box.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31