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

Join 132,401 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,217 people online right now. Registration is fast and FREE... Join Now!




if, else if, else..........Problems

 
Reply to this topicStart new topic

if, else if, else..........Problems, Hiii, I am a new in c language....

aqshaf
post 31 Aug, 2008 - 07:06 AM
Post #1


New D.I.C Head

*
Joined: 18 Aug, 2008
Posts: 28


My Contributions


Hiiiiiiii Friends how r u???
I hope fine!!

Please the below code. Tell me why this not printing out the if esle structures....i.e.., it just give me the average grade and tell me the counter, how many time it counts....But doesn't tell me the the average grade in the A, B, C as I am trying to get the this output from the else if structure.........I hope you got me..

Note: I am a new in C
and also If I want to use switch instead of else if statements then what will I have to do........I will be very thankful to you...



cpp

//example 4.22
#include<stdio.h>
int main ()
{
int sum = 0, marks = 0, counter = 1;
float average = 0;
printf( "Please enter the marks obtained or -1 to quit:\n");
scanf( "%d", &marks );
while ( marks != -1 )
{
sum = sum + marks;
counter ++;
printf( "Please enter the marks obtained or -1 to quit:\n");
scanf( "%d", &marks );
}
if( counter != 0 )
{
average = (float) sum/counter;
printf( " The average grade marks are: %.2f ", average);
}
else if ( average >= 80.00 )
printf( "The average grade is 'A'.\n");
else if( average < 80.00 && average >= 70.00 )
printf( "The average grade is 'B'. \n");
else if( average < 70.00 && average >= 60.00 )
printf( "The average grade is 'C'. \n");
else if( average < 60.00 && average >= 40.00 )
printf( "The average grade is 'D'.\n");
else
printf( "\nThe average grade is 'F'. \n");
printf( "You enter: %d times\n", counter);
return 0;
}


Mod Edit: Please use code tags when posting your code. Code tags are used like so => code.gif

Thanks,
PsychoCoder smile.gif
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 31 Aug, 2008 - 07:25 AM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Hello aqshaf... the problem here is that you calculate your average in the if statement and test for average's value in the else if portion. Remember that if the if is executed, it will skip right over else if. Else if is only executed if the "if statement" is false.

So what you need to do is calculate average first, then go into your if else statements. If you calculate average in the if statement, it will test counter being greater than zero (don't test for not equal to zero because we want it to fail if counter is some how negative) and calculate average, then skip right over else if statements that test its value for grades.

Here is your program fixed up a bit. Notice 2 things...

1) We make your tests > 0 not != 0 because we don't want it to succeed if counter is ever negative.

2) We first calculate average, then we go into an if else statement to test that average variable.

cpp

//example 4.22
#include<stdio.h>
int main ()
{
int sum = 0, marks = 0, counter = 1;
float average = 0;
printf( "Please enter the marks obtained or -1 to quit:\n");
scanf( "%d", &marks );
while ( marks != -1 )
{
sum = sum + marks;
counter ++;
printf( "Please enter the marks obtained or -1 to quit:\n");
scanf( "%d", &marks );
}

// Calculate average before going into an if statement test
if( counter > 0 )
{
average = (float) sum/counter;
printf( " The average grade marks are: %.2f ", average);
}

// Again test we have some scores and if so, it means
// we have an average calculated too
if (counter > 0) {
// Now go into testing average
if ( average >= 80.00 )
printf( "The average grade is 'A'.\n");
else if( average < 80.00 && average >= 70.00 )
printf( "The average grade is 'B'. \n");
else if( average < 70.00 && average >= 60.00 )
printf( "The average grade is 'C'. \n");
else if( average < 60.00 && average >= 40.00 )
printf( "The average grade is 'D'.\n");
else
printf( "\nThe average grade is 'F'. \n");
}
printf( "You enter: %d times\n", counter);
return 0;
}


Notice I test counter twice here, one to make sure that we have values to find an average and the second is to make sure that we did calculate an average. We could have tested if average was still equal to 0, but in theory you could have an average of zero and still be legitimate.

Enjoy!

"At DIC we be average calculating and if else testing code ninjas... else we would be just plain old nerds" decap.gif
User is offlineProfile CardPM

Go to the top of the page

aqshaf
post 31 Aug, 2008 - 07:37 AM
Post #3


New D.I.C Head

*
Joined: 18 Aug, 2008
Posts: 28


My Contributions


very much thanks.......

If I use here switch structure then what I have to adopt the syntax....Please can you tell me....

e.g..,
CODE

printf(" Enter Marks.");
scanf("%f", &marks);

switch (marks)
{
case >= 80 && case < 100;
printf(" The grade is 'A');
break;


similarly same for other grades
now tell me, am using correct syntax?????
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 31 Aug, 2008 - 08:45 AM
Post #4


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,027



Thanked 173 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Switch statement syntax is....

cpp

// This is the structure of a switch statement in C.
// Keep in mind that switch statements evaluate constants, this means you can't use ranges.
// You can only compare values like integers, chars, floats etc.
// For your situation the if/elseif situation is best.
switch (x) {
case 1:
// Statements
break;
case 2:
// Statements
break;
default:
// Statements for default case
}


As my comments state you can't test for ranges in a C programming switch statement. You can only test against constant values. Other languages like VB allowed you to test ranges, but no C. So for your grades, the if/elseif is your best option.

Hope that helps.

"At DIC we be switch statement handling code ninjas... sometimes we switch statements too when no one is looking." decap.gif
User is offlineProfile CardPM

Go to the top of the page

aqshaf
post 31 Aug, 2008 - 10:58 PM
Post #5


New D.I.C Head

*
Joined: 18 Aug, 2008
Posts: 28


My Contributions


Thanks again for my help....You are very nice person..May you get success in every field of life..
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 07:54AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month