-1

First post on this community. Started auditing some C classes at my university and having trouble with If statements. After staring and changing variations of my code for a few hours I still have yet to figure out why I can't return a value other than one of the "scoring" criteria's I've set. If anyone would be kind enough to tell me where my syntax error occurred or even possible hints as to which section I should re-write, I would be extremely grateful. Also, if my logic could use some brushing up on I would love pointers. Thanks again.

#include <stdio.h>


/* Main function */

int main()
{
    int Answer1;
    float Answer2;
    float Answer3;
    int Answer4;
    int Answer5;
    float Answer6;

    int point1;
    point1 = 0;
    int point2;
    point2 = 0;
    int point3;
    point3 = 0;
    int point4;
    point4 = 0;
    int point5;
    point5 = 0;
    int point6;
    point6 = 0;

    char name;
    int sum;
    int score;
    int multiplier1;
    int bonus_score; 
    int counter;
    counter = 1;
    int x;
    x = 1;
    int y;
    y = 2;
    int z;
    z = 3;


    /*
    ****************
    ****************
    this is the end of my declaration system, now begins the actual functions.
    ***************
    ****************
    */


    printf (" Welcome to your career amplitude test! This simple program will tell you how far you'll go in life. \n");
    printf (" Remember to write your answer to at least two decimal places. \n \n ");


    printf ("1. What is 5 + 27? \n");
    scanf ("%i", &Answer1);

    printf("2. what is 2.7 - .85? \n");
    scanf ("%f", &Answer2);

    printf ("3. what is 2.3 - .1 * 4? \n");
    scanf ("%f", &Answer3);

    printf ("4. what is 123 * 123?\n");
    scanf ("%i", &Answer4);

    printf ("5. what is 945/5?\n");
    scanf ("%i", &Answer5);

    printf (" Bonus Question!!!!!  \n");
    printf (" what is the square root of 105487.19? You have 10 seconds to enter a number (not really though.) \n");
    scanf ("%f", &Answer6);


    /*
    ******************
    ******************
    after those are printed / scanned it will come up with a potential scoring 
    system using if statements and if else 
    *****************
    *****************
    */


    if ( Answer1 == 32)
    {
        point1 = 1;
    }
    else ( Answer1 != 32);
    {
        point1 = 0;
    } 
    if ( Answer2 == 1.85 )
    {
        point2 = 1;
    }    
    else ( Answer2 != 1.85 );
    {
        point2 = 0;
    }
    if ( Answer3 == 1.9 )
    {
        point3 = 1;
    }    
    else ( Answer3 != 1.9 );
    {
        point3 = 0;
    }
    if ( Answer4 == 15129 )
    { 
        point4 = 1;
    }
    else ( Answer4 != 15129 );
    {
        point4 = 0;
    }
    if ( Answer5 == 189 )
    {
        point5 = 0;
    }
    else ( Answer5 != 189);
    {
        point5 = 0;
    }

    if ( Answer6 != 324.787 )
    { 
        point6 = 0;
    }
    if ( Answer6 = 324.787 )
    {
        point6 = 1;
    }

    /* 
    ******************
    ******************
    Now to actauly grade the assignment compared to the scoring system just established.
    ******************
    ******************
    */

    while (counter < 100)
    {
        counter = counter+x+y+z;
        printf("Processing at a light speed rate equal to %i \n \n \n", counter);
    }

    /* the above is a joke and just wanted to implement a loop for pratice */

    printf(" This is your raw score without the Bonus. \n");    

    sum = (point1 + point2 + point3 + point4 + point5); 
    score = sum;

    if ( score = 0 )
    {    
        score = 0;
        printf (" Score: 0 \n");
        printf (" You missed every question!  \n");
    }

    else if ( score = 1 )
    {
        score = 1;
        printf ("  Score: 1 out of 5 \n");
        printf ( " You only got one question right! The world needs ditch diggers too. \n");
    }

    else if ( score = 2 )
    {
        score = 2;
        printf ("  Score: 2 out of 5  \n");
        printf ( " You missed 3 questions, pratice your soft skills  \n");
    }

    else if ( score = 3 )
    {
        score = 3;
        printf (" Score: 3 out of 5 \n" );
        printf ("  I forsee a future in the hedge fund industry \n");
    }

    else if ( score = 4 )
    {
        score = 4;
        printf (" Score: 4 out of 5 \n ");
        printf (" you could probably cook books for Enron \n");
    }


    else if ( score = 5)
    {
        score = 5;
        printf (" Score: 5 out of 5  \n");
        printf (" Go out there and break code for the CIA  \n");
    }

    printf ("With the bonus considered, your score is now \n");
    if ( point6 = 1 )
    {
         multiplier1 = 2;
    }
    else if ( point6 = 0)
    {
         multiplier1 = 1;
    }

    if ( multiplier1 = 2)
    {
        bonus_score = score * 2;

        printf (" %i", bonus_score );
    }
    else if ( multiplier1 = 1)
    {
        bonus_score = score;
        printf (" You got the Bonus wrong. Nothing new to see here. \n");
    }


    return 0;
}
javier_el_bene
  • 450
  • 2
  • 10
  • 25
Zach G
  • 11

3 Answers3

1

OK, let's first have a look your if statements:

if (Answer1 == 32)
{
    point1 = 1;
}
else (Answer1 != 32);
//                  ^ !!!

This semicolon ends your else block immediately (@Joe: nicely spotted...). So the following block will be executed undonditionally:

{
    point1 = 0;
}

I. e. all your pointx values will be set to 0. Obvious that this is not what you intended.

else (Answer1 != 32);

Actually, this line is equivalent to

else
{
    (Answer1 != 32);
}

So you calculate answer1 != 32 without ever evaluating the result... I assume you actually intended to write this:

if (Answer1 == 32)
{ ... }
else if(Answer1 != 32)
//   ^^               ^  (if and no colon) 
{ ... }

However, there is one point left: the second if is the exact complement to the first one, so if the first one fails, the second one is always true. So you can just leave it out:

if (Answer1 == 32)
{ ... }
else
//   ^^ no if at all any more...
{ ... }

Those if else-if else are used if you evaluate (more or less) independent conditions, something like:

if(action1() == ERROR)
    logIt();
else if(action2() == ERROR)
    logIt();
else if(action3() == ERROR)
    logIt();
else
    return OK;
return ERROR;

or if you check for different values:

if(x == 1) {} else if(x == 2) {} ...

In the latter case, a switch statement is often the better choice, though.

Side note: In your specific case, you could have calculated it easier: C guarantees result of comparisons being either 1 or 0, so you could have simply done:

pointX = answerX == resultX;

Finally, your evaluation of score: you need to compare

if(score == x)
//       ^^ 

A single equality character is assignment:

if(score = 0) // will assign 0 to score and miss the condition
else if(score = 1) // will assign 1 to score and pass the condition

I. e. you always get a score of 1... By the way: Exactly this would have been an example where I would have preferred the switch statement as mentioned above...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • I really appreciate you taking the time to help me out. I'm still learning the fundamentals so I knew my first program would probably be garbage. Hope to get better with time though. Thanks again – Zach G Jun 22 '17 at 19:37
1

A few things:

if ( Answer1 == 32)
{
    point1 = 1;
}
else ( Answer1 != 32); <<< problem
{
    point1 = 0;
} 

That code is being parsed as

if ( Answer1 == 32 )
{
  point1 = 1;
}
else
  Answer1 != 32;  // expression is evaluated, result is discarded.

{
  point1 = 0;
}

Because of this, point1 = 0; is being executed unconditionally (outside the body of the if statement). I'm guessing you must be using gcc, since the compiler didn't yell at you for having a naked block in there.

An else doesn't take a controlling expression; you'd just write

else
{
  point1 = 0;
}

And, since you've already initialized point1 to 0, you don't need the else branch at all; you just need

if ( Answer1 == 32 )
{
  point1 = 1;
}

You've repeated that same error in that block of if statements, so that's the first thing you need to deal with.

Secondly, using == with floating point types is not recommended. Most floating point values cannot be stored exactly in a given number of bits, so what gets stored is an approximation. The approximation stored into Answer2 by scanf may not be the same as the approximation stored by direct assignment. Further compounding the problem is that floating point constants like 1.85 have type double, which uses a different representation from float, so == is even less likely to work in this case.

Unless you're really tight on space (which you're not), use double instead of float - you get greater range and precision, and for what you're doing it's not going to be any slower.

Proper floating-point comparisons get pretty ugly in a hurry; here's one approach:

#include <math.h>
#include <float.h>

int EqualEnough( double a, double b, double max_diff )
{
  double diff = fabs( a - b );
  double a_abs = fabs( a );
  double b_abs = fabs( b );
  double larger = a_abs > b_abs ? a_abs : b_abs;

  return diff <= larger * max_diff;
}

It's not perfect, it doesn't cover all cases, but for your purposes it should work well enough. You'd define it before main, and you'd call it as

if ( EqualEnough( Answer2, 1.85, DBL_EPSILON ) ) 
{
  point2 = 1;
}

See this page for a more complete discussion of floating-point comparisons.

Next:

if ( score = 0 )

In this and the following if statements, you used the = assignment operator instead of the == equality operator; you're actually assigning the value 0 to score. Since the result of an assignment expression is the value of the left-hand-side after assignment, that expression evaluates to 0, which is false, so that branch isn't taken.

else if ( score = 1 )

In this case, score is set to 1, the value of the expression is 1, so that branch is taken. So, for these statements, make sure you use ==:

if ( score == 0 )
{
  ...
}
else if ( score == 1 )
{
  ...
}

Finally, and this is just a style comment more than anything else:

When you find yourself creating a bunch of variables of the same type with the same name followed by a cardinal (point1, point2, point3, etc.), that's a strong hint you really want an array:

int point[6] = {0};  // initializes all elements to 0

if ( Answer1 == 32 )
  point[0] = 1;

if ( EqualEnough( Answer2, 1.85, DBL_EPSILON ) )
  point[1] = 1;

etc. Just remember that arrays in C are indexed starting from 0, not 1, so the elements in your point array would be point[0], point[1], point[2], ..., point[5]. This also makes it easy to sum up your points:

for ( size_t i = 0; i < 6; i++ )
  sum += points[i];

You wouldn't do this for your Answer... variables since they have different types.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Hey - I really appreciate you taking the time to explain so much of what I've done that is wrong. This is my 6th day learning C so I expected my first program to be garbage. Thanks again – Zach G Jun 22 '17 at 19:43
  • @ZachG: You're welcome. C is not an easy language to learn, even for experienced programmers. There are lots of land mines waiting for you to step on. Just keep plugging away and it will eventually start to make sense. If you're serious about learning C, you might want to bookmark the [online draft](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) of the language standard. It's not a good *teaching* resource by itself, but it's an invaluable reference. – John Bode Jun 23 '17 at 12:47
0

Your if-else statements are wrong, among other things. To start, the block of code after the else should not need a condition. else already assumes any condition not met by the preceding if statement. In fact, since you've already set the answer variables to 0, then you should entirely remove the else statement, since it's redundant. For example, the Answer 1 and 2 score statements should look more like this:

if ( Answer1 == 32)
{
    point1 = 1;
}

if ( Answer2 == 1.85 )
{
    point2 = 1;
}    

It's also true that you correctly used == here to check equality, but use = as assignment elsewhere, which isn't right for if statements.

if ( score = 0 ) <--needs to have ==, or else it actually sets the score to 0!
    {    
    score = 0;
    printf (" Score: 0 \n");
    printf (" You missed every question!  \n");
    }

Every time the above statement executes, score is set to 0! That's why you cant get any other result. Each subsequent if statement after that also sets the scoreto the value it's trying to evaluate.

If, in the future, you do want to use an else statement, you have an erroneous semicolon as well.

else ( Answer1 != 32); <--This semicolon should not be here.
{
    point1 = 0;
} 

That semicolon ends the else statement right there, instead of running the code below it. and what's probably happening is that the block of code underneath is no longer tied to a condition and executes every time.

I'd suggest going back and looking over some fundamentals and reading through other simple example programs. There is a lot here that needs improvement. you're experimenting with some functionality of code, so separate it into smaller programs, that way when something isn't working the way you want, you at least have a better idea of where to look, because as it is, there is a lot of intertwining problems with your code in the program that can make it difficult for you to figure out which part is causing you trouble.

I agree with others, look at some other examples, and rewrite this program, there are a lot more issues than the one's I've mentioned.

jzeef
  • 731
  • 1
  • 13
  • 25
  • Hey - I really appreciate you taking the time to explain how I'm going about it wrong. This is my sixth day learning the language so I knew it would likely be garbage. Hopefully I get better with time. Thanks again. – Zach G Jun 22 '17 at 19:35