Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,420 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,360 people online right now. Registration is fast and FREE... Join Now!




if...else if...else, and continual loop

 
Reply to this topicStart new topic

if...else if...else, and continual loop, Trying to get ELSE statement to run, and continual loop maybe?

bbarker3
15 Oct, 2008 - 07:58 AM
Post #1

New D.I.C Head
Group Icon

Joined: 12 Oct, 2008
Posts: 28

How would I get this program to execute the else statement. I tried typing in letters, and it just acted as if I entered a number less than or equal to 0 and said "Illegal Number" instead of "Illegal Operations, quitting program". And also, is there any way I can have it continue to run the program, until the use wants to quit?

CODE
#include <iostream>

using namespace std;

int main()
{
  int score;
  
  printf("Enter the player's game score here: \n");
  scanf_s("%d", &score);
    
    if (score < 0) {
        printf("Illegal Number\n");
    }
    else if (score > 0 && score < 100)
    {
        printf("Player Level: \n");
        printf("Beginner\n");
    }
    else if (score >= 100 && score <500) {
        printf("Player Level: \n");
        printf("Intermediate\n");
    }
    else if (score >= 500) {
        printf("Player Level: \n");
        printf("Expert\n");
    }
    else {
        printf("Illegal operation, quitting program\n");
    }

}

User is offlineProfile CardPM
+Quote Post

Gloin
RE: If...else If...else, And Continual Loop
15 Oct, 2008 - 08:13 AM
Post #2

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
Try debugging the program by printing what actually was placed in the variable score after the scanf.

I.e, simply: printf("%d", score);

Most likely, C interprets the input as something and tries to squeeze it into the variable and then your program evaluates it thereafter. Or maybe some error code is put into score. You check it out and come to your own conclusion. It's a pretty interresting exercise and won't take much time.
User is offlineProfile CardPM
+Quote Post

Hyper
RE: If...else If...else, And Continual Loop
15 Oct, 2008 - 10:56 PM
Post #3

D.I.C Head
**

Joined: 15 Oct, 2008
Posts: 134



Thanked: 3 times
My Contributions
QUOTE(bbarker3 @ 15 Oct, 2008 - 08:58 AM) *

How would I get this program to execute the else statement. I tried typing in letters, and it just acted as if I entered a number less than or equal to 0 and said "Illegal Number" instead of "Illegal Operations, quitting program". And also, is there any way I can have it continue to run the program, until the use wants to quit?


Your answer lies here:

CODE

    else if (score <= 500) { /* If it's >= (greater than or higher) it'll never trigger the else statement */
        printf("Player Level: \n");
        printf("Expert\n");
    }
    else {
        printf("Illegal operation, quitting program\n");
    }


Welcome, hope it helps.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 01:06PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month