Dear all,

I am in the process of writting a hospital/doctor prog. I have the majority working. BUT the program will only work ONCE.

when I run the prog and input patient details (No.1), the details are inputed correctly.

when I choose (No.5) I am able to correctly input the Doctor Details.

When I choose to print out these details to screen it works.

the prog will work only ONCE.

when I run the prog it will throw back some of the previous results even though I have inputed new details.

Here is the kicker. If I rename the .h files it will work again but only the once before i'm back to where I started.

ANY ANY ANY ADVICE HELP WOULD BE GREATLY APPRECIATED.

I HAVE TRYED TO COMPLIE ON 2 SEPERATE PROGS BUT THE SAME ERROR KEEPS COMMING BACK.

I HAVE ATTACHED THE FILE. U ONLY NEED CONCERN YOURSELF WITH OPTIONS 1,5,11, & 12.

MANY THANKS ,TIM

Dani AI

Generated

The symptoms described — the program works once, then later runs show old data; renaming a header makes it behave correctly once; and a literal string like "test 3" appears even when commented out — point away from an input-buffer quirk and toward build / code-organization problems. 's suggestion to check input buffering is useful for IO bugs, but the fact that renaming a header forces a one-time fix strongly implicates either stale build artifacts, multiple conflicting definitions across translation units, or memory corruption that only shows up after a layout change (the header rename changes timestamps/layout and masks the issue). and were on the right track when they suspected project files and duplicate symbols.

Practical diagnostic checklist (in order):

  • Confirm the running executable is the freshly-built one: change a printed message or insert a unique temporary build message and rebuild. If the change does not appear, do a full clean (delete .o/.obj/.exe) and rebuild. On Make use make clean, on IDEs use the Clean/Rebuild command.
  • Search the whole tree for the literal string that still appears (Linux: grep -R "test 3" . ; Windows: findstr /s /n "test 3" *). That will reveal if another TU contains the print.
  • Search for duplicate definitions of main_menu (or any function appearing more than once). Headers should contain declarations only; definitions must live in a single .cpp.

Small corrective patterns to apply:

  • Use include guards in every header, e.g.
    
    #ifndef DISPLAYMENU_H
    #define DISPLAYMENU_H

int main_menu(); // declaration only

#endif

- Do not place non-inline function definitions or non-const global definitions in headers. For globals use `extern` in the header and define once in a .cpp.
- For safe console input in C++ prefer ignoring the rest of the line rather than relying on getchar loops:

#include <limits>
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');



Also enable strict diagnostics and run sanitizers: compile with warnings on (-Wall -Wextra), keep optimization off for debugging (-O0), add debug info (-g), and use AddressSanitizer/valgrind to catch overruns. If these checks still don’t locate the fault, isolating the menu/add/print paths into a minimal reproducible example will reveal whether the culprit is build configuration, duplicate symbols, or undefined-memory writes.

Recommended Answers

All 11 Replies

Sorry, I can't view your file (this stupid computer doesn't have WinZIP!), but I'm going to guess you're not clearing the keyboard buffer.

This will clear the keyboard buffer (it's in C):

int c;
while((c = getchar()) != '\n' && c != EOF);

Unfortunately that is not the solution. Can anybody else throw some light on the situation. I'm really under pressure.

mant thanks, tim

Are u using file handling??

Well what do you mean by running only once? I compiled and ran the program, and it ran okay. I added two patients, one doctor and printed the patient and doctor list, all by one go at the program. The other options didnt work as I think you have to implemented them.

must be the complier i'm using. when I go to use the program and choose the option to print out doctor details (no 12) it prints out "test 3" even though I have it commented out. Does it print out "test 3" for you??? thanks for trying to complie the code.

I ran your compiled program and yes it outputs test3. I compiled myself and ran again, but there was no test3 output. The closest I can think of is that you are modifying one source file but compiling another.

I ran your compiled program and yes it outputs test3. I compiled myself and ran again, but there was no test3 output. The closest I can think of is that you are modifying one source file but compiling another.

This wouldn't surprise me either as for example you have more than one .h file with the same function:
int main_menu() ( in menu.h and displayMenu.h)

yes i have int main_menu() declared in my main.cpp and I cleaned up my code by placing all my functions in (displayMenu.h) As i dont have a main.h I am still a little lost. thx tim

As i dont have a main.h I am still a little lost. thx tim

Sorry, displayMenu.h and menu.h

Sorry, displayMenu.h and menu.h

I can't see a menu.h file either. the only header files i can see are doctors.h, linkedlist.h, patient.h, & displayMenu.h.

I'm simply looking at the "copy of 4.zip" attached at the top of this thread.

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.