Help me with my homework pls.

C++ Program for the ff:

*Ask the user to input a number.
*If the number is

< 10 print “Number is less than 10”
< 20 print “Number is less than 20 but greater than 10”
< 30 print “Number is less than 30 but greater than 20”
< 40 print “Number is less than 40 but greater than 30”
< 50 print “Number is less than 50 but greater than 40”
< 50 print “Number is greater than 50”

*Use a loop to repeat this process until the user inputs -1.



Output:

Input a number (-1 to exit): 5
Number is less than 10_
Input a number (-1 to exit): 19
Number is less than 20 but greater than 10.
Input a number (-1 to exit): 25
Number is less than 30 but greater than 20.
Input a number (-1 to exit): 55
Number is greater than 50.
Input a number (-1 to exit): -1
Press any key to continue…
Please note, that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem youself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again. As it is impossible to find deriviative of function without knowledge in ariphmetics, you cannot do more complex tasks in programming without clear understanding of basics.
————————————

What did you try and where did you fail? What part of this assigment gives you problems?
well sorry i didnt post my work, please help me check if i use the correct code for 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
#include <stdio.h>

int main(void)
{
int a=0;
printf("please enter a number : \n");

scanf("%i",&a);

switch(a)
{
         if (a<=10);
            printf("Number is less than 10");
            break;
         if (a<=20 && a>10);
            printf("Number is less than 20 but greater than 10");
            break;
         if (a<=30 && a>20);
            printf("Number is less than 30 but greater than 20");
            break;
         if (a<=40 && a>30);
            printf("Number is less than 40 but greater than 30");
            break;
         if (a<=49 && a>40);
            printf("Number is less than 50 but greater than 40");
            break;
         if (a>=50);
            printf("Number is greater than 50");
            break;
         
         return 0;
         }
}
Get rid of switch. It is not needed here. You just need a if-elseif chain here:
1
2
3
4
5
6
7
if (a < 10) {
    //Handle case a<10 here
} else if (a < 20) {
    //Handle case a<20 here
    //a which are less than 10 will not be handled by this routine, 
    //as they are catched by previous if. So no extra condition needed
} else if /*...*/
i tried this one but does not go with the output given above

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

int main(void)
{
int a;
printf("please enter a number : \n");

scanf("%i",&a);

switch(a)
{
         if (a<=10){
            printf("Number is less than 10");
            break;
         } else if (a<=20 && a>10){
            printf("Number is less than 20 but greater than 10");
            break;
         } else if (a<=30 && a>20){
            printf("Number is less than 30 but greater than 20");
            break;
         } else if (a<=40 && a>30){
            printf("Number is less than 40 but greater than 30");
            break;
         } else if (a<=49 && a>40){
            printf("Number is less than 50 but greater than 40");
            break;
         } else if (a>=50){
            printf("Number is greater than 50");
            break;
            }
         return 0;
         }
}
miinipaa told you to remove the switch.

C++ Program

Then rather than using printf and including stdio.h, you'd be better using cout and including iostream.

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

edit. I've just realised you haven't followed miinipaa's advice at all.
Last edited on
i already remove switch but if i run it and enter a number it automatically exit the output what are my lacking codes?

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

int main(void)
{
int a;
printf("please enter a number : \n");

scanf("%i",&a);
{
         if (a<=10){
            printf("Number is less than 10");
         } else if (a<=20 && a>10){
            printf("Number is less than 20 but greater than 10");
         } else if (a<=30 && a>20){
            printf("Number is less than 30 but greater than 20");
         } else if (a<=40 && a>30){
            printf("Number is less than 40 but greater than 30");
         } else if (a<=49 && a>40){
            printf("Number is less than 50 but greater than 40");
         } else if (a>=50){
            printf("Number is greater than 50");
            }
         return 0;
         }
}
Again, read Miinipaa's post.
Remove the switch block and the break statements

http://www.dev-hq.net/c++/4--if-and-else-statements

Use of switch,
http://www.cprogramming.com/tutorial/lesson5.html
it automatically exit
What IDE do you use. If it Visual Studio, launchw with Ctrl+F5 instead of F5.

Alternatively read this post: http://www.cplusplus.com/forum/beginner/1988/
I'm almost there guys, but when I input -1 it won't end the program.. where is my mistake? help me guys..

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
#include<iostream>
using namespace std;

int main ()
{
int a;

while (true){
      cout <<"Input a Number (-1 to exit): ";
      cin >> a;
      
         if (a<=10 && a>0)
            cout << "Number is less than 10 \n";
         else if (a<=20 && a>10)
              cout << "Number is less than 20 but greater than 10 \n";
         else if (a<=30 && a>20)
              cout << "Number is less than 30 but greater than 20 \n";
         else if (a<=40 && a>30)
              cout << "Number is less than 40 but greater than 30 \n";
         else if (a<=50 && a>40)
              cout << "Number is less than 50 but greater than 40 \n";
         else
             cout << "Number is greater than 50 \n";}
         
         system ("pause");
         return 0;
}


my output must be like this:

Input a number (-1 to exit): 5
Number is less than 10_
Input a number (-1 to exit): 19
Number is less than 20 but greater than 10.
Input a number (-1 to exit): 25
Number is less than 30 but greater than 20.
Input a number (-1 to exit): 55
Number is greater than 50.
Input a number (-1 to exit): -1
Press any key to continue…
Last edited on
I also tried this one, and the output are correct until i put -1 but when the display said
"Press any key to continue..." and i press any key the loop still continue the process..


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
#include<iostream>
using namespace std;

int main ()
{
int a;

while (true){
      cout <<"Input a Number (-1 to exit): ";
      cin >> a;
      
         if (a<=10 && a>0)
            cout << "Number is less than 10 \n";
         else if (a<=20 && a>10)
              cout << "Number is less than 20 but greater than 10 \n";
         else if (a<=30 && a>20)
              cout << "Number is less than 30 but greater than 20 \n";
         else if (a<=40 && a>30)
              cout << "Number is less than 40 but greater than 30 \n";
         else if (a<=50 && a>40)
              cout << "Number is less than 50 but greater than 40 \n";
         else if (a>50)
              cout << "Number is greater than 50 \n";
         else
              system ("pause");
              }
         return 0;
}
@kevin32

You're not really doing anything to end the cycle by entering a -1

Try this..
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 a;
bool run = true;
while (run){
      cout <<"Input a Number (-1 to exit): ";
      cin >> a;
        if(a==-1) // Checks now for -1 and exits the while loop
         {
            cout << "Okay, we're finished..\n";
            run = false;
          }
          else  if (a>=0  && a < 10)
            cout << "Number is less than 10 \n";
         else if (a<20 && a>=10)
              cout << "Number is less than 20 but greater than 10 \n";
         else if (a<30 && a>=20)
              cout << "Number is less than 30 but greater than 20 \n";
         else if (a<40 && a>=30)
              cout << "Number is less than 40 but greater than 30 \n";
         else if (a<50 && a>=40)
              cout << "Number is less than 50 but greater than 40 \n";
         else if (a>=50)
              cout << "Number is greater than 50 \n";
         }
         system ("pause"); // Not really a good idea to use system calls
         return 0;
}
@whitenite thanks for your help bro... now i know what code to end the loop with -1.. thanks so much i got new idea.. anyway im just a beginner :D
Topic archived. No new replies allowed.