It runs but closes

Nov 23, 2012 at 4:52pm
This program should solve for a matrix but I havent started with the main body since it closes when I run it

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 <stdlib.h>
#include <conio.h>
#include <stdio.h>

int choice, dmatrix, size;
int quit=0;

main()
{
      matrix:
      printf("Choose the type of MATRIX OPERATION/n");
      printf("/n [1] Find the Determinant/n [2] Cofactor Matrix /n [3] Reduced Echelon Form /n [0]Exit Program");
      scanf("%d", &choice);
      switch(choice)
      {
                    case 1:
                         printf("Enter the size of the matrix (max 5 by 5)");
                         scanf("%n", &dmatrix);
                         do
                         {
                                     if (dmatrix == 1);
                                     printf("INVALID MATRIX SIZE");
                                     goto case 1;
                                     
                                     if (dmatrix ==2);
                                     printf(" enter A11/n");
                                     scanf("
                         getch();
                         break;
                    case 2:
                         printf("");
                         break;
                    case 3:
                         printf("Enter the size of the matrix (max 5 by 5)");
                         break;
                    case 0:
                         quit =50;
                         break;    
                    default:
                         printf("Invalid choice Please Re-enter choice");
                         break;
                         goto matrix;
                         }
                      
                         
}
 
Nov 23, 2012 at 4:54pm
And why would it stay open?
Nov 23, 2012 at 4:56pm
when i run it, it closes immedietly
Nov 23, 2012 at 4:56pm
What do you see when you run it? Does it print out the text specified in lines 11 and 12? Does it allow you to enter a choice?

Do you get any error messages?
Nov 23, 2012 at 5:02pm
when i run it it just closes on the spot
Nov 23, 2012 at 5:02pm
when i run it, it closes immedietly


So you don't even get to type anything in?
Nov 23, 2012 at 5:09pm
yeah nothing i dont know why. but devc doesnt show that i have an error
Nov 23, 2012 at 5:14pm
If your compiler doesn't complain about line 27:
scanf("
throw it away. The code above cannot compile.

If you're using Dec Cpp from before version 5, also throw it away.

Also, this is C++. Why use printf and scanf when cout and cin are so much safer and easier to use?
Last edited on Nov 23, 2012 at 5:15pm
Nov 23, 2012 at 5:22pm
Instead of having that awful goto matrix: (goto is not a good practice to get into). I would suggest that you have something like:

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
int main()
{
   bool running(true);
   
   while (running)
   {
      system("cls");
      
      // put your printf scanf here
      
      switch(choice)
      {
         case 1:
            // to do
            break;
         case 2:
            // to do
            break;
         case 3:
            // to do
            break;
         case 0:
            running = false;
            break;
         default:
            // to do
      };
   }

   return 0;
}
Nov 23, 2012 at 5:22pm
sorry im not that accustomed to use c++ i usually use .c but i need the "goto" syntax for easier use
Nov 23, 2012 at 5:25pm
sorry im not that accustomed to use c++ i usually use .c but i need the "goto" syntax for easier use


Is it not easy to follow the above code I have pasted with a while() loop? If you think you need goto's then call a function rather than use goto.
Nov 23, 2012 at 5:27pm
Using "goto" is almost always a terrible idea. ajh32's suggestion is vastly better. C and C++ have perfectly good control structures, including loops, and you should use them.
Nov 23, 2012 at 5:27pm
... also the main() function should always return an integer back to the OS. Non zero indicates an error, zero indicates to OS that all was OK.
Topic archived. No new replies allowed.