program crashing

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "stdafx.h"
#include <iostream>
#include <istream>

using namespace std;

int main()
{
      
        int num; // (!) (001)
			char choice;

        cout << " Please enter a number : \n" << endl;
        cin >> num;

		cin.ignore (255, '\n');
cout << "\n1) number divisible by 3"; 
  cout << "\n2) number between 10 and 100\n"; 
  cout << "Enter choice: "; 
  cin >> choice;


          if (choice == '1') // (!) (002)
       {
		   // handle choice 1
		  
         if (num % 3 == 0)
{
   // num is divisible by 3 
                                                         }
        else
{
   // num is not divisible by 3
                                                         }

        }

          else if (choice == 2) // (!) (003)
        {
			// handle choice 2
         
			  if( choice < 10)
			{
                 cout << " Less than 10 : \n";
			                                             }
			  else if(choice > 10 && choice > 100)
			{
                 cout << " Between 10 and 100 : \n";
			                                             }
			   else if(choice > 100)
			{
                 cout << " Greater than 100 : \n";
			                                             }
 else
{
	// handle invalid choice
        cout << "Not in menu: \n";
	                                                     }

		cin.get();
        return 0;
}


I'm unable to continue with the program, after entering a number, then a choice, it crashes, I do think the problem is within somewhere around here

1
2
3
4
5
		cin.ignore (255, '\n');
cout << "\n1) number divisible by 3"; 
  cout << "\n2) number between 10 and 100\n"; 
  cout << "Enter choice: "; 
  cin >> choice;


i'm not entirely sure tho, after I did a semicolon after every cout and cin,
Last edited on
First let me apologise if I seemed unhelpful previously.

Anyway, currently, the program does not seem to crash when I tested it. However it does end normally without much in the way of useful output.

For option 1, you need to change the comments (lines 29 and 33) into actual cout statements that will put out the message. That's one reason why there's no output.

For option 2, there are a number of issues.
At line 38, choice is a character, and the program compares it with an integer. It should be '2' rather than just 2.

The rest of that block of code is a bit lost because it is comparing the user's choice which we know is always the character '2' to see what value it might have. Shouldn't it be testing the number num which the user entered at the start? Lines 42, 46 and 50 are testing the wrong variable.

Last edited on
No you don't need to apologize, you're helping me a lot trying to figure out my mistakes so that next time I would see them.
1
2
3
4
5
6
7
if (num % 3 == 0)
{
   cout << "num is divisible by 3: \n " 
                                                         
        else
{
   cout << " num is not divisible by 3 : \n "


Altho in the problem, else is under a red line and the program works perfectly, but shuts down after it because it doesn't seem to understand it and doesn't continue. Isn't it supposed to be like this?

1
2
3
4
5
6
7
if (num % 3 == 0)
{
   cout << "num is divisible by 3: \n " 
            }                                             
        else
{
   cout << " num is not divisible by 3 : \n "


Altho now in the program, the } Is having a red line under it.

really appreciate it btw
There should be a semicolon at the end of each of those two cout statements.
 
        cout << "num is divisible by 3: \n ";



and yes you are right, the opening and closing braces { and } should be in matched pairs.
Anyway, currently, the program does not seem to crash when I tested it.
However it does end normally without much in the way of useful output.


You said this previously, but it's still occurring and i fixed everything that you said.
1
2
3
4
5
6
7
8
9
10
11
12
 if( num < 10)
	{
                 cout << " Less than 10 : \n";
			                                             }
			  else if(num > 10 && num > 100)
	{
                 cout << " Between 10 and 100 : \n";
			                                             }
			   else if(num > 100)
	{
                 cout << " Greater than 100 : \n";
			                                             } 


I changed it from choice, i re-read the program and It's whenever the user inputs a number which is a positive integer, It goes down and inputs the number, then checks if the choice is equal to 1 or 2. If it's 1 it checks if num is a divisor of 3, and goes on, else if it's 2, checks if it's less than, between or greater, and then else then shows not in menu. But what seems to be the problem in the code? I think everything is correct so far
Last edited on
Did you change line 38?
 
          else if (choice == 2) // (!) (003) 
as I mentioned above, 2 is an integer, you need the character '2'
 
          else if (choice == '2') // (!) (003) 


In addition as I stated previously, this line doesn't make sense:
 
    else if(num > 10 && num > 100)

If the number is greater than 100 it must also be greater than 10, so testing both is clearly not much use. consider whether you should be using the less than < or greater than > sign here.
The question is prompts the user to enter a choice, if the user enters the value 1, determine whether the value 3 is a divisor of the integer number and output an appropriate message, if the user enters the value 2, determine whether the integer number is less than 10, between 10 and 100, or greater than 100, and output an appropriate message, if the user enters a value other than 1 or 2, output a message indicating that the value is not a menu item., it is not expected to loop back to show the menu again.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "stdafx.h"
#include <iostream>
#include <string>
#include <istream>

using namespace std;

int main()
{
      
        int num; // (!) (001)
			char choice;

        cout << " Please enter a number : \n" << endl;
        cin >> num;

		cin.ignore (255, '\n');
cout << "\n1) number divisible by 3"; 
  cout << "\n2) number between 10 and 100\n"; 
  cout << "Enter choice: "; 
  cin >> choice;


          if (choice == '1') // (!) (002)
       {
		   // handle choice 1
		  
          if (num % 3 == 0)
{
     cout << "num is divisible by 3: \n " ;
		                                     }                                                       
        else
{
     cout << " num is not divisible by 3 : \n " ;
                         }

                               }

              else if (choice == '2') // (!) (003)
    {
			// handle choice 2
         
			  if( num < 10)
	{
                 cout << " Less than 10 : \n";
			                   }
			  else if(num > 10 && num < 100) 
	{
                 cout << " Between 10 and 100 : \n";
			                       }
			   else (num > 100)
	{
                 cout << " Greater than 100 : \n";
			                        }
 else
    {
	// handle invalid choice
                 cout << "Not in menu: \n";
	               }

		cin.get();
        return 0;
}


and this is the code. Isn't it supposed to be correct?
Last edited on
However it doesn't compile so it isn't correct.

I get a compiler error at line 52
[Error] expected ';' before '{' token

I'm getting a sense of deja vu here.

Look at line 51
else (num > 100)
Now compare it with what you had previously
else if(num > 100)

See the difference? It was something I tried to explain in an earlier post,
I still didn't understand, could you explain pls?

thanks!
Maybe take a look at this part of the tutorial.
http://www.cplusplus.com/doc/tutorial/control/

I'm not sure which part of it you're not getting.

Here's what I wrote previously:
chervil wrote:

Line 47 looks wrong.
else(a > 100)
The compiler may swallow it without a murmur, but it is definitely incorrect.
The else should stand alone (it cannot have an attached condition).
If another condition is necessary, then use another if. (Sometimes the else and if may be placed on the same line, so it looks like this:
else if (a > 100) but remember the condition belongs to the if. Also, remember if you do add an if then it will also usually need its own corresponding else later.
I'm sorry for this but I'm still not getting what else and if else is wrong.

else should stand alone, If i wanna add one more, it should be else if, but isn't that what i did?
1
2
3
 error C2143: syntax error : missing ';' before '{'
error C2181: illegal else without matching if
fatal error C1075: end of file found before the left brace '{' at ' 


I'm getting these errors when running it, altho it does compile. You told me in the previous one not to put a ; at the start of the line because it's irrelevant. I checked illegal else without matching if, it's either the semicolons or the brackets. last one i didn't understand. so i'm guessing it's more like

1
2
3
4
5
6
7
8
9
10
11
12
13
14

     
			  if( num < 10)
	{
                 cout << " Less than 10 : \n";
			                                             }
			  else if(num > 10 && num <100) 
	{
                 cout << " Between 10 and 100 : \n";
			                                             }
			   else (num > 100)
			 ;  {
                 cout << " Greater than 100 : \n";
			                                             }


1
2
3
4
else
    {
	// handle invalid choice
                 cout << "Not in menu: \n";


This is the illegal else without matching if


There are different ways of looking at this. Purely from the point of view of writing valid C++ code, you cannot write this:
else (num > 100)

The else is used to control what happens when the previous if statement is not satisfied.
It always looks like this:
1
2
if (condition)
    do something;


or this:

1
2
3
4
if (condition)
    do something;
else
    do some other thing;


It never looks like this:
1
2
3
4
if (first condition)
    do something;
else (second condition)
    do some other thing;
Last edited on
Another way of looking at it, in the context of your own program.
1
2
3
4
if (num < 10)
    action to be taken when num is less than 10
else
    action to be taken when num is 10 or more

The useful thing to note here is that when we arrive at line 4 (after the else statement), we already know that num is not less than 10, so there's no need to test that again. All that remains is to test whether it is greater than 100. That means another if, and a final else.


edit It may be convenient to test num <= 100, rather than num > 100.


Last edited on
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
#include "stdafx.h"
#include <iostream>
#include <istream>

using namespace std;

int main()
{   
        int num; 
			char choice;
        cout << " Please enter a number : \n" << endl;
        cin >> num;
		cin.ignore (255, '\n');
cout << "\n1) number divisible by 3"; 
  cout << "\n2) number between 10 and 100\n"; 
  cout << "Enter choice: "; 
  cin >> choice;

          if (choice == '1')      
          if (num % 3 == 0)
     cout << "num is divisible by 3: \n " ;		                                                                                                      
        else
     cout << " num is not divisible by 3 : \n " ;
              else if (choice == '2') 
          if( num < 10)
	  cout << " Less than 10 : \n";
		  else if(num > 10 && num <100) 	
                 cout << " Between 10 and 100 : \n";			                                             
			   else (num > 100)
                 cout << " Greater than 100 : \n";
 else
                 cout << "Not in menu: \n";
		cin.get();
		system("pause");
        return 0;
}


This is the final code, It works perfectly and gives me the right outcome.
Thank you for everything really! I appreciate your help.

One final question tho, do I need to put system("pause"); If i didn't put it, it shuts down?
This is the final code, It works perfectly and gives me the right outcome.[/code][code]

Sorry, it still doesn't compile.
Line 29 gives an error
else (num > 100)

The condition (num > 100) cannot be preceded by else all by itself. There must be an if in there.


Then you need to test it.

what happens if the user enters 10 and then 2?
what happens if the user enters 100 and then 2?

As for system("pause"), you could repeat the cin.get() a second time instead.

1
2
3
4
5
6
  else if(num > 10 && num <100) 	
                 cout << " Between 10 and 100 : \n";			                                             
			   else if (num > 100)
                 cout << " Greater than 100 : \n";
else 
cout << “ Not valid \n “

this should work, correct?
That will probably fix the compiler error.

Though I strongly urge you to test the program.
As a minimum, try entering each of 9, 10, 11, 99, 100, 101 for option 2.
Pay attention in particular to the output when num is 10 or 100.

edit: My compiler gives an error with this line:
cout << “ Not valid \n “
The important one is a missing semicolon at the end of the line. (it also uses non-ASCII quotes).

However there is now a compiler warning
[Warning] suggest explicit braces to avoid ambiguous 'else' [-Wparentheses]

That is to say, does the else at line 31 match the if at line 29, or does it match the if at line 24?

The compiler cannot tell, the code is ambiguous, whichever version is assumed, it presents a problem where certain inputs may be misinterpreted.
Last edited on
One thing which can help is to use a consistent style of indentation, and opening/closing braces { and } where appropriate. These are as much to aid the human reader as to control the program logic.


Below, I tidied up the latest version of the code, and fixed the ambiguity which I referred to above. However I've left the flawed logic unaltered. It's not my intention to do the entire task, just to guide the way a little bit.
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 <istream>

using namespace std;

int main()
{   
    int num; 
    char choice;
    cout << " Please enter a number : \n" << endl;
    cin >> num;
    cin.ignore (255, '\n');
    cout << "\n1) number divisible by 3"; 
    cout << "\n2) number between 10 and 100\n"; 
    cout << "Enter choice: "; 
    cin >> choice;
    
    if (choice == '1')
    {  
        if (num % 3 == 0)
            cout << "num is divisible by 3: \n " ;
        else
            cout << " num is not divisible by 3 : \n " ;
    }
    else if (choice == '2')
    { 
        if (num < 10)
            cout << " Less than 10 : \n";
        else if (num > 10 && num <100) 	
            cout << " Between 10 and 100 : \n";
        else if (num > 100) // inserted the word 'if' here
            cout << " Greater than 100 : \n";
        else                // added final 'else' and suitable message 
            cout << "not less then 10, not between 10 and 100, not greater than 100\n"; 
    }
    else
        cout << "Not in menu: \n";
    
    cin.get();
    cin.get();
    return 0;
}



Note that lines 33 and 34 do not belong in the program. I added them only in order to highlight a problem in the program logic.
33
34
        else                // added final 'else' and suitable message 
            cout << "not less then 10, not between 10 and 100, not greater than 100\n";


Here, my previous comments still stand:
chervil wrote:
Then you need to test it.

what happens if the user enters 10 and then 2?
what happens if the user enters 100 and then 2?

Topic archived. No new replies allowed.