6 Replies - 745 Views - Last Post: 25 October 2014 - 08:34 PM Rate Topic: -----

#1 wdk001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 14

If statement comparing array to scanf input problem

Posted 25 October 2014 - 06:44 PM

Hey guys, first post on this forum. The issue that I'm having is that when I run this part of the code it seems to enter the if statement and set it to 0 when comparing the array to the input.

What I want it to do is to search the array for the grade, set it to zero, and then exit the loop in case there are any repeat grades. It always sets the first number to 0 and then exits.

Thanks in advance

//Below is the prototyping for the array

int grades[14] = {60,50,50,20,75,90,0,0,0,0,0,0,0,0};

//There is some code between here, but it is probably not relevant so I didn't post it.

	printf("\n\nEnter the grade that you want to remove. Press Enter after each input.\n");
	printf("Enter 0 to exit: \n");

	int c = 0;
	int p;

		do
		{
			scanf("%d", &p);
			fflush(stdin);
			do
			{
				if (grades[c] == p);
				{
					grades[c] = 0;
					break;
				}				
				c = c + 1;
				
			}while(c < 15);	
					
		}while(p > 0);



Correction to the first post:
I didn't mean to leave the "prototyping" part in the code. I was initially going to put the function prototypes in there, but decided it wasn't necessary for the question.

Is This A Good Question/Topic? 0
  • +

Replies To: If statement comparing array to scanf input problem

#2 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: If statement comparing array to scanf input problem

Posted 25 October 2014 - 07:04 PM

Well, you should zero c at the start of the first the loop. But the condition for the first loop, p > 0 doesn't seem like it would be in the right place, since you would still try to remove negative grades, which is wasted processing.
Was This Post Helpful? 0
  • +
  • -

#3 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: If statement comparing array to scanf input problem

Posted 25 October 2014 - 07:35 PM

Hi, on line 19 you have a semicolon at the end of the if expression.

19			if (grades[c] == p);


Was This Post Helpful? 0
  • +
  • -

#4 wdk001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 14

Re: If statement comparing array to scanf input problem

Posted 25 October 2014 - 07:41 PM

View Postinfernorthor, on 25 October 2014 - 07:04 PM, said:

Well, you should zero c at the start of the first the loop. But the condition for the first loop, p > 0 doesn't seem like it would be in the right place, since you would still try to remove negative grades, which is wasted processing.


I see how that would be an issue with needing to 'zero out' c before each input and I just fixed that in the program.
The reason that It set to exit the loop when p is <= 0 is so that you can input 0 into the scanf and it will exit the loop, which means you are finished removing elements from the array.

View Post#define, on 25 October 2014 - 07:35 PM, said:

Hi, on line 19 you have a semicolon at the end of the if expression.

19			if (grades[c] == p);



Jesus I have been looking at this thing for hours and didn't even notice this. Thanks so much. That's about 3 hours of wasted time for a semicolon in the wrong place.
Was This Post Helpful? 0
  • +
  • -

#5 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: If statement comparing array to scanf input problem

Posted 25 October 2014 - 07:44 PM

Lol, it happens.
Was This Post Helpful? 0
  • +
  • -

#6 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: If statement comparing array to scanf input problem

Posted 25 October 2014 - 08:01 PM

Just to address another problem you go out of bounds with c < 15 and you should call the break, in case more than 1 grades has that grade.

He is how I would do that, though the prompt might slightly confusing, can just put the scanf() in replace of it and add the messages.

int prompt(int *p )
{
	puts("Enter grade to remove, zero to quit");
	return scanf("%d",p) ; // returns number of successful inputs
}

int main( void ) {

	int grades[14] = {60,50,50,20,75,90,0,0,0,0,0,0,0,0};

	int p;

	while( prompt(&p) && p > 0) // will get the number
	{
		int i;
		for( i=0;i<14;i++)
		{
			if( grades[i] == p)
			{
				grades[i] = 0;
			}

			printf("%d ",grades[i]); //print the grades out to see changes
		}
		printf("\n");
	}



	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#7 wdk001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 25-October 14

Re: If statement comparing array to scanf input problem

Posted 25 October 2014 - 08:34 PM

View Postinfernorthor, on 25 October 2014 - 08:01 PM, said:

Just to address another problem you go out of bounds with c < 15 and you should call the break, in case more than 1 grades has that grade.

He is how I would do that, though the prompt might slightly confusing, can just put the scanf() in replace of it and add the messages.



The program is set up so that, in the if statement, it will exit the loop if one grade is changed to zero. That way it only changes one grade. This part of the program is within a function that is not in the main function. The program as a whole is around 300 lines so I didn't post the entire thing. That would also print all the grades, including the zero's, which I didn't want to do.

Thanks for the input. I have a lot of practicing ahead of me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1