Having trouble with closing the program...

This is my code,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <time.h>
#include <conio.h>
using namespace std;

int opselect;


int main ()
{
   
    cout << "\nMAIN MENU" << endl
         << "0. Exit" << endl
         << "1. Add a student mark" << endl
         << "2. List all student marks" << endl
         << "3. Calculate the marks average" << endl
         << "4. Calculate the standard deviation" << endl
         << "5. Delete a student mark" << endl
         << "6. Find the number of students via a mark" << endl
         << "7. display distinct marks and their occurrences" << endl;
 
 cout << "\nYour Choice -> ";
 cin >> opselect;
 switch (opselect)
{
        case 0:
             cout << "The program will exit in 3 seconds..."<< endl;
              void Wait(int seconds);
             {
             clock_t endwait;
             endwait = clock () + 3 * CLK_TCK ;
             while (clock() < endwait) {}
             }
        case 1:
             cout<< "Hi there" << endl;
             system ("PAUSE");
             system ("cls");
             return main ();
 
return 0;
}
}


Hey guys, this is part of my assignment. The trouble I am having is that when I execute case 1 before case 0 my program does not close. It just displays "The program will exit in 3 seconds..." and on the next line "Hi there" and then it returns to the menu. I was wondering if I can execute case 0 after I finish with the other parts of the program?
And also, for some reason the function clrscr() does not work.. I am using devshed C++. :/.
Last edited on
return main ();

This line runs the function called main (it does not return to the start of main - it runs a whole new main function inside the one that is already running). Your code runs main over and over and over and over and over and over, each one nested inside an already running main function, going deeper and deeper and deeper and deeper... you get the picture.

Why are you running main within itself? What are you trying to do?

Your switch statement has no default case - what happens if you don't choose 0 or 1? Your case 0: code has no break statement, so if you choose 0, you will also run case 1.

That return 0; is directly after the return main(); when will return 0; ever be reached? (Never)

Did you mean to put return 0; inside case 1:? That's what you've done.


As an aside, use of #include <conio.h> is non-standard but not horrific. Use of

1
2
system ("PAUSE");
system ("cls");


is completely system dependent and frankly a little dangerous; it surrenders control completely to an external programme.

Also, I note that you're able to use system without including the cstdlib header. This suggests a build environment that is not C++ standard compliant. Can you move to a better build environment (i.e. dump your IDE and get a modern one)? You will simply learn bad habits that will be hard to break, and when you try to use your code on a compliant system, you'll be confused as to why it doesn't work. I must admit I've never heard of "DevShed C++"; I thought DevShed was a programming forum and articles site.

Last edited on
Sorry for being so unclear, I am still working on the program. As you can see here,

1
2
3
4
5
6
7
8
9
 MAIN MENU
0. Exit
1. Add a student mark
2. List all student marks
3. Calculate the marks average
4. Calculate the standard deviation
5. Delete a student mark
6. Find the number of students via a mark
7. Display distinct marks and their occurrences

I have to include 7 cases in my program. All of which perform a specific function if chosen. The number depicts the usability of the program. So basically if i choose number 1 I can add marks into the program. If i choose 2 it lists all the marks I entered in number 1.

The question states that for Case 1, I have to clear the screen and then return to main menu and select the "Add a student mark" again. That is why i put in the return main() function there. The return 0; is just there as a precaution.
I still havent worked on the other cases so if i choose number 2 the program quits automically. This was just an issue I uncovered whilst I was testing the first part of the program.

I just figured out how to make the program exit even when other cases are used before case 0. All i did was i put in exit (1) function here,
1
2
3
4
5
6
7
8
9
 case 0:
             cout << "The program will exit in 3 seconds..."<< endl;
              void Wait(int seconds);
             {
             clock_t endwait;
             endwait = clock () + 3 * CLK_TCK ;
             while (clock() < endwait) {}
             }
             exit (1);


*Please do not mark this as "Solved" since I might have other issues with this program. I do not want to start new topics for every problem I face. I am really grateful for anyone's input.
Last edited on
exit (1) - I see you can also use this function without having to include cstdlib. Definitely non-compliant with the C++ standard. That's not necessarily the end of the world, but you're better off learning standard compliant C++.

Is there some reason why you can't have the programme finish normally? That is, by reaching the line return 0; ?

I cannot stress enough that return main() is a terrible way to make the code repeat. If you repeat it like this enough times, you will run out of memory and it will crash. I suggest you wrap the code you want to repeat in some kind of loop.

1
2
3
4
5
6
  void Wait(int seconds);
             {
             clock_t endwait;
             endwait = clock () + 3 * CLK_TCK ;
             while (clock() < endwait) {}
             }



This is almost certainly not doing what you want it to. You are declaring the existence of a function called Wait, and then you are running some lines of code that have absolutely nothing whatsoever to do with a function called Wait. If you meant to define a function called Wait, and then use it, you've misunderstood in a big way how and where to define functions, and how to use them. Not knowing how to write and use functions will cripple you when trying to write code. Harsh but true, I'm afraid.

http://www.cplusplus.com/doc/tutorial/functions/

Last edited on
Well the IDE is called Dev-C++ 4.9.9.2. And its made by Bloodshed (I think). That is why I called it "devshed" :s.
I use this IDE since the teachers who mark the assignment also use this.

I understand the use of system () is dangerous but for some reason clrscr does not work, I get an error saying that it is "undeclared" and I dont know of any other commands that clears the screen or has the same function as system ("cls") and clrscr ()

Btw, thanks for all your input so far Moschops. Really appreciate it :).
Well honestly, I found that bit of code on the web.. But its working for me? but you have to include #include <time.h>
Calling main inside your program results in undefined behavior, so you can't do that.
Use a loop instead.
Ah. I suspected as much. Please read this regarding Dev-C++:

http://www.cplusplus.com/forum/articles/36896/

If you're stuck with it, you're stuck with it; be aware that it's held up as an example of what not to use. On the one hand, you're not being taught to be a professional coder, so bad tools and bad habits don't matter so long as you demonstrate understanding of what you are being taught. On the other hand, it would cost nothing for the class to switch to decent tools and teach correct code and good habits. Never mind; such is life.

"undeclared" means that the compiler has never heard of this function. Wherever you're trying to get it from is not working; either it plain doesn't exist, or you're not including the right header file. My guess is that it plain doesn't exist. A quick alternative is just to output line-endings until the screen is clear; something like this, for example.

1
2
3
4
5
6
7
8
9
#include <iostream>
int main()
{
  for (int i=0; i<50; ++i)
  {
     std::cout << std::endl;
   }
   return 0;
}


This is far, far better code than a system call to cls. However, it may well not look as pretty and thus may lose you marks.









Implementing the code for every case is going to make it hard to mark. :/. Guys, thanks for your input its really helped. I shall finish my code and post it here when I am done with it. I know it will be unstable and that but still its worth trying lol.
Implementing the code for every case is going to make it hard to mark.


I assume you mean implementing the code to clear the screen. Perhaps you could implement it once, in a function, and just call that function each time you want that code to run. This is exactly why functions exist. I definitely think you need to go back and read what functions are.
Last edited on
I think I should too :/. I am really bad at C++. Well I tried to use the code you gave me and it does not clear the screen, It closes the whole program. Maybe I am doing something wrong?
Did you also put in the return 0; ?

return is how a function ends and passes control back to where it came from. If you return from main, the programme finishes.

Last edited on
Yes I did, I pasted exactly what you mentioned earlier. This is what I did so far, Please dont laugh LOL. It lets me select the option, i type in 1 then press enter, then it just closes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <time.h>

using namespace std;

const int stu = 1000; 
int Mark[stu];
int opselect;

int main ()
{
  
    cout << "\nMAIN MENU" << endl
         << "0. Exit" << endl
         << "1. Add a student mark" << endl
         << "2. List all student marks" << endl
         << "3. Calculate the marks average" << endl
         << "4. Calculate the standard deviation" << endl
         << "5. Delete a student mark" << endl
         << "6. Find the number of students via a mark" << endl
         << "7. display distinct marks and their occurrences" << endl;
 
 cout << "\nYour Choice -> ";
 cin >> opselect;
 switch (opselect)
{
        case 0:
             cout << "The program will exit in 3 seconds..."<< endl;
              void Wait(int seconds);
             {
             clock_t endwait;
             endwait = clock () + 3 * CLK_TCK ;
             while (clock() < endwait) {}
             }
             exit (1);
        case 1:
             cout << "You have selected option 1, Please type in a mark to ADD " << endl;
             cin >> Mark[stu];
             {
            for (int i=0; i<50; ++i)
            {
            std::cout << std::endl;
            }
            return 0;
}
}
}
Last edited on
When a function gets to return 0; the function will end.

You have return 0; at the end of case 1: - when the code gets to that, the function it is in (main) will end. When main ends, your programme ends; that is how C++ works. If you don't want the programme to end at that point, do not put return 0; there.

I strongly suggest you stop trying to write this code. It is currently beyond your understanding and you are voodoo coding - you are copying things that you see without understanding, and are then unable to identify your errors or understand your own code.

Start here:

http://www.cplusplus.com/doc/tutorial/

If you follow it as far as the end of the section named "Functions (I)" you will know everything you need to complete your assignment.
closed account (zb0S216C)
elementz wrote:
It lets me select the option, i type in 1 then press enter, then it just closes

Thats because you're returning from main in case 1. Changing return 0 to break will stop your program from closing.

Also, you don't return a value from main( ). Secondly, your program will still close once it leaves the switch block. Adding std::cin.get( ) before the closing brace of main( ) will keep the program open until a key is pressed.

Wazzak
Last edited on
@Moschops, Yes mate I didnt pay much attention in class neither have I read the book. I dislike programming (no offence) its just not my cup of tea. But I still have to do it to get my degree. I have been memorising codes during class exercises and barely passing this subject. So yea, I am pretty noob at this stuff. I suck at at. But I still HAVE to do this assignment in order to pass that is why im "voodoo coding" I am trying to understand at the same time :/.

Thank you framework for your reply, I shall see what happens.
It is far, far easier to understand a small number of actually quite easy concepts than it is to memorise code in the hopes of cobbling it together. But you know that already. Good luck.
Topic archived. No new replies allowed.