This program is supposed to take in four scores two are assignment scores and the other two are test scores it should only take in score that are 0 to 100 me test chain goes someting like this

//check for valid data
	if (test1<0 ||test1>100)&& (test2<0||test2>100)&& (assign1<0||assign1>100)&&(assign2<0||assign2>100)
	{
		printf("Error your score must be between 0 and 100\n");
		printf("Please Enter two test scores\n");
        scanf("%f,%f", &test1,&test2);
		printf("Please Enter two assignment scores\n");
        scanf("%f,%f", &assign1,&assign2);

	}

I know its probably wrong but could I have no idea why i'm thinking the chain is too long any other ideas?

Dani AI

Generated

The compiler errors you reported come from a few distinct issues. The stray semicolons after conditional headers (for example while (cond); { ... }) make the block run unconditionally. Parentheses and operator precedence errors mean your averages and logical tests do not do what you expect. was right to flag the syntax problem; was right that validating each score separately is friendlier for the user. Below are concise fixes and a small, robust input pattern you can plug into your program.

Fixes to apply

  • Remove any semicolon that immediately follows an if or while header. That turns the conditional into an empty statement.
  • When checking “any score is out of range,” combine per-score tests with OR (||), not AND (&&). Use one combined expression that returns true if any single score is <0 or >100.
  • Don’t use "%f,%f" unless you require a literal comma in the input. Prefer separate prompts or whitespace-separated formats.
  • Parentheses matter for averages: use (a + b) / 2.0f, not a + b / 2.
  • Keep pass threshold on the same scale as the scores (use 50.0 or 0.5 consistently; do not mix).

A robust helper to read one validated score (reads a line, parses, and repeats until valid):

#include <stdio.h>
#include <stdlib.h>

float read_score(const char *prompt) {
    char buf[128];
    float v;
    while (1) {
        fputs(prompt, stdout);
        if (!fgets(buf, sizeof buf, stdin)) exit(1);
        if (sscanf(buf, "%f", &v) == 1 && v >= 0.0f && v <= 100.0f)
            return v;
        puts("Please enter a number between 0 and 100.");
    }
}

Use the helper to read each score separately, compute avg = (s1 + s2) / 2.0f, and compare against a pass value in the same units (e.g., 50.0f). Enable compiler warnings (GCC -Wall, MSVC warnings) to catch stray semicolons and mismatched braces; that will save a lot of time.

Recommended Answers

All 10 Replies

offhand I'd say it looks fine, though you may consider inputting each one and checking it separately. That way if the 2nd assignment is wrong, you don't have to input the other 3 again.

Why not just write a small check function which will do the small grained tasks for you which unnecessarily clutter the "main()".

int is_valid( int value )
{
    if ( value < 0 || value > 100 )
        return 0 ;
    else
        return 1 ;
}

And carrying ahead with Mr. Infarcion's advice, do something like

// ask the user to enter the data in a loop
// accept the data in the required variable
if( is_valid( required_variable ) )
    continue;
else
{
    printf ("Data out of range") ;
    exit (1) ;
}

Hope it helped, bye.

I kept get error with it are you sure its correct?

Maybe you can simply modify the code like this:
if (test1<0 ||test1>100)
&& (test2<0||test2>100)
&& (assign1<0||assign1>100)
&&(assign2<0||assign2>100)

nope, is there supposed to be a semicolon somewhwere in that line before the && operator?

What kind of error you gettting ? Post your entire recent code and highlight the line on which you are getting the error.

ok i'm getting a bunch of errors but most of them are missing ";" before "&&" here's the whole thing

int main()
{
//intitialize variables
float test1,test2,assign1,assign2,avg_test,avg_assign;
const float pass_grade=0.50;

//get user input
printf("Please enter two test scores");
scanf_s("%f,%f",&test1,&test2);
printf("Please Enter two assignment scores\n");
scanf_s("%f,%f", &assign1,&assign2);

//check for valid data
	while  (test1<0 ||test1>100)&& (test2<0||test2>100)&& (assign1<0||assign1>100)&&(assign2<0||assign2>100);
	{
		printf("Error your score must be between 0 and 100\n");
		printf("Please Enter two test scores\n");
        scanf_s("%f,%f", &test1,&test2);
		printf("Please Enter two assignment scores\n");
        scanf_s("%f,%f", &assign1,&assign2);

	}
	
	
		avg_test=test1+test2/2;
		avg_assign=assign1+assign2/2;
	
	
		if  (avg_test>=avg_assign)&& (avg_test>=pass_grade);
		{
			printf ("You have passed because of your test grade :%.2f",avg_test);
		
		else if(avg_assign>=avg_test) &&(avg_assign>=pass_grade);
		  {

		   printf ("You have passed because of your assignment grade :%.2f",avg_assign);

		   }
		else if (avg_test>avg_assign);
		{
			printf ("Your test grade was not high enough you have failed %.2f",avg_test);
		}
		else
		{
			printf ("Your assignment grade was not high enough you have failed %.2f",avg_assign);
		}

		

	return 0;

Looks like you have a major problem with your syntax concepts.

1. There should be no semicolon after the conditional stmts
if ( ) ; is wrong

2. You need to nest your braces properly.

if (something) && (something eles) .. is wrong

It should be like

if ( (something) && (something eles) )
int main()
{
//intitialize variables
float test1,test2,assign1,assign2,avg_test,avg_assign;
const float pass_grade=0.50;

//get user input
printf("Please enter two test scores");
scanf_s("%f,%f",&test1,&test2);
printf("Please Enter two assignment scores\n");
scanf_s("%f,%f", &assign1,&assign2);

//check for valid data
    while ( (test1<0 ||test1>100)&& (test2<0||test2>100)&& (assign1<0||assign1>100)&&(assign2<0||assign2>100))
    {
        printf("Error your score must be between 0 and 100\n");
        printf("Please Enter two test scores\n");
        scanf_s("%f,%f", &test1,&test2);
        printf("Please Enter two assignment scores\n");
        scanf_s("%f,%f", &assign1,&assign2);

    }


        avg_test=test1+test2/2;
        avg_assign=assign1+assign2/2;


        if  ((avg_test>=avg_assign)&& (avg_test>=pass_grade))
        {
            printf ("You have passed because of your test grade :%.2f",avg_test);
        }

        else if((avg_assign>=avg_test) &&(avg_assign>=pass_grade))
          {

           printf ("You have passed because of your assignment grade :%.2f",avg_assign);

           }
        else if (avg_test>avg_assign)
        {
            printf ("Your test grade was not high enough you have failed %.2f",avg_test);
        }
        else
        {
            printf ("Your assignment grade was not high enough you have failed %.2f",avg_assign);
        }



    return 0;
        }

wow thks, seems i've got alot to learn, know of any good newbie tutorials?
also the program dosent wait for the assignment scores would it be better to read all of the scores in at one time?

Some good newbie tuts HERE.

Also try adding getchar() before return 0 for the screen to wait for you to see the results.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.