ok so if i have somthing like

#include <iostream>

using namespace std;

int main(){
    int action;
    cout << "have this show up first" << endl;
    cout << "then have an if statement here" << endl;
    cin >> action;
    if (action == 1){
               cout << "then have this show up but none of the above text ^^." << endl;
               }
    system("pause");
    return 0;
    
    }

how would i do that so it does what it says in the "cout"?

Dani AI

Generated

wanted each menu to replace the previous text instead of stacking output. correctly pointed out using the OS clear; was right to caution it’s platform-specific. Beyond screen-clearing, the posted code has three root causes that make behavior unpredictable: functions declared to return an int but not returning values, invoking functions as if they were printable values, and wide reuse of the same global/local variables. Fixing the program structure is the reliable solution.

A robust approach: keep a single control loop in main, have small helper functions that either return a chosen option or draw a submenu and return void, validate input, and redraw the screen on each state transition. Avoid relying on nonstandard system calls if portability matters; a simple ANSI escape sequence clears most modern terminals and is lightweight as a first step. Also enable compiler warnings (-Wall -Wextra) to catch missing returns and shadowed variables.

Example pattern (minimal, original) that illustrates structure and portable input handling:

#include <iostream>
#include <string>
#include <limits>

void clear_screen() {
    // ANSI clear + cursor home (works on POSIX and modern Windows terminals)
    std::cout << "\033[2J\033[H";
}

int showMainMenu() {
    std::cout << "Main menu\n"
              << "1) United States\n"
              << "2) Europe\n"
              << "0) Quit\n"
              << "Choice: ";
    int choice;
    while (!(std::cin >> choice)) {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "Please enter a number: ";
    }
    return choice;
}

void showEuropeMenu() {
    clear_screen();
    std::cout << "Europe menu\n1) Quality\n2) Malpractice\n0) Back\n";
    int c;
    if (!(std::cin >> c)) { std::cin.clear(); std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); }
    // handle c...
}

int main() {
    while (true) {
        clear_screen();
        int opt = showMainMenu();
        if (opt == 0) break;
        switch (opt) {
            case 1: /* showUnitedStatesMenu(); */ break;
            case 2: showEuropeMenu(); break;
        }
        std::cout << "Press Enter to continue...";
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cin.get();
    }
    return 0;
}

Final notes: change any menu functions that only print to void, avoid calling a function inside cout << as if it produces printable output, and keep state local or explicitly passed. If the target is an old Windows cmd.exe, consider a platform-specific fallback; otherwise redrawing with ANSI escape codes works well and keeps the code clean.

Recommended Answers

All 6 Replies

some people use

system("cls");

Here's one example.

i tried that it didnt work it when i put in the IF statement it got rid of all the stuff...

It worked when I did it just now:

if (action == 1){
               system("cls");
               cout << "then have this show up but none of the above text ^^." << endl;
               }

According to you can also use clrscr(); if you have Borland C++ Builder.

Could you please give us an example of what you want rather than half an explanation using strange code?

ok so what i want is for me to be able to go through my script and as i go to another item it gets rid of the previous text...
heres an example...

#include <iostream>
using namespace std;

 int Mainmenu();
 char action1;
 int action;
using namespace std;
int Unitedstates()
{
int action;
char action1;
cout << "you have chosen to learn more about the united states. Now What will you do?" << endl;
cout << "1. The quality of doctors in the United States." << endl;
cout << "2. What happens when a doctor commites malpractice." << endl;
cout << "3. Average salery of doctors." << endl;
cout << "4. Practices in  the United States." << endl;
cin >> action;
if (action == 1){
           cout << "Coming soon" << endl;
           }
else if (action == 2){
     cout << "coming soon" << endl;
     }
else if (action == 3){
     cout << "the average salery is $100,000 a year." << endl;
     }
else if (action == 4){
cout << "coming soon" << endl;
}
else {cin >> action;}
}

int Europe(){
int action;
char action1;
cout << "You have chosen to learn more about Medecine in Europe." << endl;
cout << "Now what do you want to learn about?" << endl;
cout << "1. The quality of doctors in Europe." << endl;
cout << "2. What happens when a doctor commites malpractice." << endl;
cout << "3. Average salery of doctors." << endl;
cout << "4. Practices in  the Europe." << endl;
cin >> action;
if (action== 1){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 2){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 3){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 4){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
     }
else cin >> action;
}


int China(){
cout << "You have chosen to learn more about Medecine in China." << endl;
cout << "Now what do you want to learn about?" << endl;
cout << "1. The quality of doctors in China." << endl;
cout << "2. What happens when a doctor commites malpractice." << endl;
cout << "3. Average salery of doctors." << endl;
cout << "4. Practices in China." << endl;
cin >> action;
if (action== 1){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 2){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 3){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 4){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
     }
else cin >> action;
}


int Australia(){
cout << "You have chosen to learn more about Medecine in Australia." << endl;
cout << "Now what do you want to learn about?" << endl;
cout << "1. The quality of doctors in Europe." << endl;
cout << "2. What happens when a doctor commites malpractice." << endl;
cout << "3. Average salery of doctors." << endl;
cout << "4. Practices in  Australia." << endl;
cin >> action;
if (action== 1){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 2){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 3){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 4){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
     }
else cin >> action;
}



int Africa(){
cout << "You have chosen to learn more about Medecine in Africa." << endl;
cout << "Now what do you want to learn about?" << endl;
cout << "1. The quality of doctors in Europe." << endl;
cout << "2. What happens when a doctor commites malpractice." << endl;
cout << "3. Average salery of doctors." << endl;
cout << "4. Practices in  Africa." << endl;
cin >> action;
if (action== 1){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 2){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 3){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 4){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
     }
else cin >> action;
}



int Brazil(){
cout << "You have chosen to learn more about Medecine in Brazil." << endl;
cout << "Now what do you want to learn about?" << endl;
cout << "1. The quality of doctors in Europe." << endl;
cout << "2. What happens when a doctor commites malpractice." << endl;
cout << "3. Average salery of doctors." << endl;
cout << "4. Practices in  Brazil." << endl;
cin >> action;
if (action== 1){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 2){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 3){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
}
else if (action == 4){
cout << "coming soon" << endl;
cout << "to go back to the main menu, press 1, and then enter." << endl;
cin >> action;
if (action == 1)
cout << Mainmenu();
else cin >> action;
     }
else cin >> action;
}




int Mainmenu(){
int action;
char action1;
 cout << "Welcome to my Quarter Project." << endl;
 cout << "What would you like to learn more about?" << endl;
 cout << "1. Medecine in the United States." << endl;
 cout << "2. Medecine in Europe." << endl;
 cout << "3. Medecine in China." << endl;
 cout << "4. Medecine in Australia." << endl;
 cout << "5. Medecine in Africa." << endl;
 cout << "6. Medecine in Brazil." << endl;
 cin >> action;
 if (action == 1)cout << Unitedstates();
 else if (action == 2) cout << Europe();
 else if (action == 3) cout << China();
 else if (action == 4) cout << Australia();
 else if (action == 5) cout << Africa;
 else if (action == 6) cout << Brazil();
else {cin >> action;}



}




int main()
{
int action;
char action1;
cout << Mainmenu();

 system("pause");
 return 0;

}

thats my script and i want it to be able to like as i go from "mainmenu" to say Europe, the main menu dosnt show up anymore and then if i pick something in Europe it makes the previous text disappear and only show whats next

Then just put the system("cls"); command immediately before the new menu is displayed.

We usually don't recommend doing things like this though because there is no standard way to clear the screen. It's different for each O/S.

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.