Pls help

Hello everybody, i am new here and i need some help with C++. Meaning, i have to assignments to finish till tomorrow and have a problem:

1. I need to enter a number and the code to check if it is a single digit, double or multi digit.



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
  #include<iostream>

using namespace std;
int main() 

{
   int n;
   
   cout << "enter a number" << endl;
   
   cin >> n ;
   
   
{ if (n>=0 && n <=9);

cout << "single digit" << endl;

}
{
if (n>=10 && n<=99);
    
cout << "double digit" << endl;
}
{
if (n>=100);

cout << "multi" << endl;
}       
}


and it displayed all the text when entered any number.

2. Problem

Need to enter an angle (alfa) and the code to tell me if the angle is:

acute angle-an angle between 0 and 90 degrees.
right angle-an 90 degree angle.
obtuse angle-an angle between 90 and 180 degrees.
straight angle-a 180 degree angle.

please help.

Best regards!!!
if (n>=0 && n <=9);

Remove that ;

Also on other if statements.
Thank you. I did it and not the right answers came when I enter the numbers.

#include <iostream>
using namespace std;

int main()
{
int alfa;
cout << "Enter the angle " << endl;
cin >> alfa;

if (alfa=0)
{
cout << "Angle 0 " << endl;
}
else if (alfa>=1 && alfa <=89)
{
cout<<"Sharp " << endl;
}
else if (alfa=90)
{
cout << "Right" << endl;
}
else if (alfa>=91 && alfa <=179)
{
cout << "Bad" << endl;
}

else if (alfa=180)
{
cout << "Flat" << endl;
}

else if (alfa=360)
{
cout << "Full" << endl;
}

else
{
cout << "Entered not possible value" << endl;
}
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
#include<iostream>
#include<cstdlib>//for exit

using namespace std;
int main() 

{
   int n;
   
   cout << "enter a number" << endl;
   
   cin >> n ;
   
   
 if (n>=0 && n <=9)

{cout << "single digit" << endl;
exit(1);
}


if (n>=10 && n<=99);
    
{cout << "double digit" << endl;
exit(1);}

if (n>=100);

cout << "multi" << endl;
return 0;// int returns value to check if the program has executed correctly,
//but you can do with void main() also if you are allowed   
}

or just use else if statements
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
#include<iostream>


using namespace std;
int main() 

{
   int n;
   
   cout << "enter a number" << endl;
   
   cin >> n ;
   
   
 if (n>=0 && n <=9)

cout << "single digit" << endl;



else if (n>=10 && n<=99)
    
cout << "double digit" << endl;


else if (n>=100)

cout << "multi" << endl;
return 0;// int returns value to check if the program has executed correctly,
//but you can do with void main() also if you are allowed  in classes
}

For your second problem just do it like the firs one with if statements asking for example
1
2
3
if(n>0&&n<90)
cout<<"Accute angle"<<endl;
else if(...)
Last edited on
I did it, thank you. For the second one it doesn't gives the correct
answers, it states always "right" whatever number i enter. What can be the problem?

#include <iostream>
using namespace std;

int main()
{
int alfa;
cout << "Enter the angle " << endl;
cin >> alfa;

if (alfa=0)
{
cout << "Angle 0 " << endl;
}
else if (alfa>=1 && alfa <=89)
{
cout<<"Sharp " << endl;
}
else if (alfa=90)
{
cout << "Right" << endl;
}
else if (alfa>=91 && alfa <=179)
{
cout << "Bad" << endl;
}

else if (alfa=180)
{
cout << "Flat" << endl;
}

else if (alfa=360)
{
cout << "Full" << endl;
}

else
{
cout << "Entered not possible value" << endl;
}
return 0;
}
#include<cstdlib>//for exit


In C++ exit should be avoided whenever possible. This C function doesn't understand C++ classes and doesn't properly call C++ class destructors.

And in this single function program (main()) there is absolutely no reason to be using this C function since a simple return is all you need.

By the way your indentation is just horrible which makes reading your program difficult.

And that last if() statement is doing absolutely nothing and is not needed.

//but you can do with void main() also if you are allowed

Well since you're never allowed to use void main() in C++ this is a mute point. Stick with the standard and always define main() to return an int.

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
37
38
39
40
41
42
#include <iostream>
using namespace std;

int main()
{
int alfa;
cout << "Enter the angle " << endl;
cin >> alfa;

if (alfa==0)// in if statement == this is if two operands are equal

cout << "Angle 0 " << endl;//when you have one line after if you do not need brackets

else if (alfa>=1 && alfa <=89)

cout<<"Sharp " << endl;

else if (alfa==90)//Here you had mistake

cout << "Right" << endl;

else if (alfa>=91 && alfa <=179)

cout << "Bad" << endl;


else if (alfa==180)//Here you had mistake

cout << "Flat" << endl;


else if (alfa==360)//Here you had mistake

cout << "Full" << endl;


else

cout << "Entered not possible value" << endl;

return 0;
} 
Compile with more warnings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ g++ -Wall foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:10:11: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
 if (alfa=0)
           ^
foo.cpp:18:17: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
 else if (alfa=90)
                 ^
foo.cpp:27:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
 else if (alfa=180)
                  ^
foo.cpp:32:18: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
 else if (alfa=360)
                  ^

You probably meant to use == where you used =.


Thank you very much!!!
Topic archived. No new replies allowed.