Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

global variable in function file

Name: Anonymous 2012-06-03 18:50

Ok, very novice programmer here. This is part of one of my first programs just dicking around. Basically, I want this program to say "How may I serve you?" the first time the "menu" comes up and then "May I do anything else?" afterwards. With "bool runOnce = false" is inside the function, obviously it will be reset to false every time the function is called so the program will always print "How may I serve you". Making it global within this file fixes that issue ... but I get the feeling that's bad programming. I mean it's not really a global variable I don't think, since it can't be accessed by any other part of this program. But, it would be global to anything else that would go in this file if the program became much more complicated.

Can anyone think of a better way to tackle the "Do this1 the first time, do this2 all subsequent times" problem?

#include<iostream>
#include"menu.h"

bool runOnce = false;

int menu()
{
    int choice = 0;
   
    if (runOnce == false)
    {
        std::cout << "How may I serve you?" << std::endl;
        runOnce = true;
    } else
    {
        std::cout << "May I do anything else?" << std::endl;
    }
    std::cout << std::endl;

    std::cout << "(1) Make me a sandwich." << std::endl;
    std::cout << "(2) Give me a blowjob." << std::endl;
    std::cout << "(3) Nothing, go away." << std::endl;
    std::cout << "\n: ";
    std::cin >> choice;
    return choice;
}

Name: Anonymous 2012-06-03 21:06

static?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List