checking 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "stdafx.h"
#include <iostream>
#include <string>

 using namespace std;

 int main()
 {
	 int number = 0;
  char choice = 0;

  cout << "Enter a number: \n ";

  cin >> number;

  cin.ignore (255, '\n');

  cout << "\n1) number divisible by 3";

  cout << "\n2) number between 10 and 100\n";

  cout << "Enter choice: ";

  cin >> choice;
  cin.ignore();

  if (choice == '1')
  {
    // handle choice 1
  }
  else if (choice == '2')
  {
    // handle choice 2
  }
  else
  {
    // handle invalid choice
  }

 int num

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

	 
	
		 If (num == 1);
		 {
			 // num % 3 //
			 cin >> "It is a divisor of 1: \n";	 
			 cin.ignore();
	 }
	 
		 else if (num == 2);

		 {
			 // num > 10 || 10 && 100 || num < 100 //

			 cin >> "Message: \n";
			 cin.ignore();
		 }
		 else 
			 cin << "Not in menu: \n";
		 cin.ignore();
		

return 0;
 }

//


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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <math.h>
#include <iomanip>
#include <stdio.h>

#define PI 3.14159265359
 using namespace std;

 int main()
 {
	const double PI = 3.14259;

double angle;
double radian ;
double degree ;
double sin1;
double cos1;
double tan1;
double rad1;
double result;
double result1;
double result 2;
cout << fixed << setprecision(4)
	cout << "Please enter the angel in degree: \n";
rad1 = degree PI / 180;
result = ( sin rad1*180 ) / PI;
	result1 = ( cos rad1*180) / PI;
	result2 = (tan rad1*180) / PI

cout<<"Angle"<<"\t"<<"Sin"<<"\t"<<"Cos"<<"\t"<<"Tan"<<endl;

printf(" The sine of %f radians is %f. \n", result);
printf(" The sine of %f radians is %f. \n", result1);
printf(" The sine of %f radians is %f. \n", result2);

return 0;
 }

If anyone is able to help me out checking if both the codes are correct i'll be really grateful! it's for an assignment!

thanks!!
Last edited on
closed account (LNboLyTq)
Hi m7md65,

Your first program :
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
64
65
66
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int number = 0;
	char choice = 0;

	cout << "Enter a number: \n ";

	cin >> number;

	cin.ignore (255, '\n');

	cout << "\n1) number divisible by 3";

	cout << "\n2) number between 10 and 100\n";

	cout << "Enter choice: ";

	cin >> choice;
	cin.ignore();

	if (choice == '1')
	{
		// handle choice 1
	}
	else if (choice == '2')
	{
		// handle choice 2
	}
	else
	{
		// handle invalid choice
	}

	int num // (!) (001)

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



	If (num == 1); // (!) (002)
	{
		// num % 3 //
		cin >> "It is a divisor of 1: \n";
		cin.ignore();
	}

	else if (num == 2); // (!) (003)
	{
	// num > 10 || 10 && 100 || num < 100 //

	cin >> "Message: \n";
	cin.ignore();
	}
	else
	cin << "Not in menu: \n";
	cin.ignore();

	return 0;
}


There are three (compiler) errors in your code. Fix them first and let we know.
Also, remember to use code tags to make your code readable.
http://www.cplusplus.com/articles/jEywvCM9/

Andy.
If anyone is able to help me out checking if both the codes are correct i'll be really grateful!

Have you actually compiled and run these programs? Since there are obvious compiler errors in the second program, it would appear not. So the first thing to do is to make sure you use a C++ compiler, and pay attention to the error messages. If you need help understanding the error messages, please feel free to ask.

I wasn't able to compile it because of the errors in the program, It's been a few days since I've learned c++ so I'm not able to figure out what the problems are. It tells me that " If " isn't defined, I don't know how to define it
It tells me that " If " isn't defined, I don't know how to define it

Don't forget that C/C++ are case sensitive, "if" is not the same as "If".

Okay so that's fixed, so for the first program,
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
64
65
66
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
	int number = 0;
	char choice = 0;

	cout << "Enter a number: \n ";

	cin >> number;

	cin.ignore (255, '\n');

	cout << "\n1) number divisible by 3";

	cout << "\n2) number between 10 and 100\n";

	cout << "Enter choice: ";

	cin >> choice;
	cin.ignore();

	if (choice == '1')
	{
		// handle choice 1
	}
	else if (choice == '2')
	{
		// handle choice 2
	}
	else
	{
		// handle invalid choice
	}

	int num // (!) (001)

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



	If (num == 1); // (!) (002)
	{
		// num % 3 //
		cin >> "It is a divisor of 1: \n";
		cin.ignore();
	}

	else if (num == 2); // (!) (003)
	{
	// num > 10 || 10 && 100 || num < 100 //

	cin >> "Message: \n";
	cin.ignore();
	}
	else
	cin << "Not in menu: \n";
	cin.ignore();

	return 0;
}

It runs perfectly fine, but only for the first part. It shows me the output, tells me to enter a number and i do, and I either choose 1 or 2 and I do that, enter a number and it shuts down without continuing. What can i do?
Last edited on
closed account (iw7Gy60M)
Lines 47 and 54. Notice anything at the end of those lines that shouldn't be there?
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <istream>

using namespace std;

int main()
{
      
        int num; // (!) (001)
			int a;

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

		cout << " Choose 1 or 2 : \n" << endl;
		cin >> a;
		cin.ignore();


          if (a == 1) // (!) (002)
       {
		  

		   // (a % 3)
		 
                cout << "It is a divisor of 3 \n"; 

        }

          else if (a == 2) // (!) (003)
        {
         
			  if( a < 10)
			{
                 cout << " Less than 10 : \n";
			}
			  else if(a > 10 && a > 100)
			{
                 cout << " Between 10 and 100 : \n";
			}
			   else(a > 100)
;			{
                 cout << " Greater than 100 : \n";
			}

        }

        else

		{
        cout << "Not in menu: \n";
	    }

		cin.get();
        return 0;
}


Okay so i tried to fix the code but it's still pretty wrong lol, how do I find the divisor when the user enters an integer?

1
2
3
		   // (a % 3)
		 
                cout << "It is a divisor of 3 \n"; 


Idk how to do this, does anyone know how?
closed account (iw7Gy60M)
1
2
3
4
5
6
7
8
if (num % 3 == 0)
{
   // num is divisible by 3
}
else
{
   // num is not divisible by 3
}
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 <string>
#include <istream>

using namespace std;

int main()
{
      
        int num; // (!) (001)
			int a;

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

		cout << " Choose 1 or 2 : \n" << endl;
		cin >> a;
		cin.ignore();


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

        }

          else if (a == 2) // (!) (003)
        {
         
			  if( a < 10)
			{
                 cout << " Less than 10 : \n";
			}
			  else if(a > 10 && a > 100)
			{
                 cout << " Between 10 and 100 : \n";
			}
			   else(a > 100)
;			{
                 cout << " Greater than 100 : \n";
			}

        }

        else

		{
        cout << "Not in menu: \n";
	    }

		cin.get();
        return 0;
}


Is this correct?
Is this correct?

1. Does the code look correct?
2. Does the code compile?
3. During testing, does it give the expected results? Remember to test for all possible input conditions. (not every possible number, just samples of values which meet the various conditions).

My responses.
1. The code looks erroneous in a few places, as well as some suspicious-looking use of semicolons.

2. Yes, the code does compile.

That is useful, because it means you can then go on to test it.
The program should display the following menu:
1) number divisible by 3
2) number between 10 and 100

In fact it displays
Choose 1 or 2 :

That's the first problem - the menu does not appear as specified. How is the user supposed to know what 1 and 2 mean?

Next, let's test option 1.
if the user enters the value 1, determine whether the vale 3 is a divisor of the integer number and output an appropriate message

The program fails to display anything at all.

Now let's test option 2.
determine whether the integer number is less than 10, between 10 and 100, or greater than 100 and output an appropriate message


No matter what input is given, the output is always:
 Less than 10 :
 Greater than 100 :


Is this correct? There is one part which seems correct. If the user enters a value which is not 1 or 2, it does indeed display a message to inform the user.


Some of the code appears to be heading in the right direction, but needs work.
Line 14 - starting a line with a semicolon is not good practice. Usually place semicolons (where needed) at the end of a line, not the start.

Line 43else if(a > 10 && a > 100)
looks pretty odd. If a number is greater than 100 it must be greater than 10, so that test seems unnecessary.

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.

Line 48. A stray semicolon at the start of the line. I guess this was added to patch up the error on the previous line. Remove the semicolon.

Regarding the logic for this part.
The variable name 'a' is meaningless. If you gave it a more meaningful name such as 'menu' or 'choice' it might help you to see where some of the mistakes are in the code.



Now, please don't respond with more code and "Is this correct?" Do some testing - see whether or not it works for yourself. If you do get stuck, then please do ask again if necessary. But don't depend on others to do the testing for you, that's part of your job as the programmer.
Last edited on
Topic archived. No new replies allowed.