Click here to Skip to main content
15,943,061 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried compiling this following simple C program(calculating sum of 5 five subjects and its aggregate percentage) in visual studio 2015,but it throws an exception, can anyone help me, I'm new to use visual studio.
It closes the console application window (32bit) and pops up a window saying that

"Exception thrown at 0x54ACB5F2 (ucrtbased.dll) in ConsoleApplication3.exe: 0xC0000005: Access violation writing location 0xCCCCCCCC.

If there is a handler for this exception, the program may be safely continued."

What I have tried:

C++
#include "stdafx.h"
#include<stdio.h>
#include<conio.h>


int main()
{
	int marks=0,sub[4],i=1;
	while (i <= 5)
	{
		A:printf("\nEnter Subject %d Marks: ", i);
		scanf_s("%d", sub[i]);
		if(sub[i]<0)
		{
			printf("\nINVALID VALUE!!");
			goto A;
		}
		else
		marks = marks + sub[i];
	}
	printf("\nTotal Marks : %d out of 500, Aggregate percentage is %f", marks, marks / 5);
    return 0;
}
Posted
Updated 2-Mar-17 7:38am
v3
Comments
Graeme_Grant 2-Mar-17 12:08pm    
This smells like homework...

What error? Which line? Have you looked up the error in docs/google? Do you know how to use the debugger?

Update your question with details by clicking on Improve question
ArvindSai 2-Mar-17 12:20pm    
Its complies the program but, after entering the value for the first subject it closes the windows console application and pops a window showing the message as mentioned in the query
I'm afraid to say that i don't know how to use the debugger, I'm very new to visual studio, can you give me any suggestions to learn about visual studio compiler and how it works:( @Graeme_Grant
Graeme_Grant 2-Mar-17 12:22pm    
This video will get you started with the debugger: Debugging C/C++ applications in Visual Studio 2013 - YouTube[^]
ArvindSai 2-Mar-17 12:25pm    
Thanks for the link!!:)
ArvindSai 2-Mar-17 12:31pm    
I tried debugging but it pops the same message@Graeme_Grant

Arrays in C start with index zero, so if you declare an array of 4 integers, you can only access elements 0, 1, 2, and 3 - your code allows 1, 2, 3, 4, and 5 which will cause problems.

And remove goto from your memory until you have around five years of development experience behind you - by then you will understand when it is a good idea to use it. This is not a good time, so don't use it - it helps to create code that is difficult to understand, maintain, and write.
I haven't used a goto in years, possibly decades!
 
Share this answer
 
Comments
ArvindSai 2-Mar-17 12:48pm    
Thanks for the solution:)
so, How can I write a code that detects the negative values(in valid values) and jumps back to the statement that allows to re enter new valid values?, Any suggestions or ideas please??
ArvindSai 2-Mar-17 12:57pm    
I tried correcting the array values and removed the goto statement
for now the above program is like follows
int main()
{
int marks = 0, sub[5], i = 0;
while (i <= 4)
{
printf("\nEnter Subject %d Marks: ", i + 1);
scanf_s("%d", sub[i]);
marks = marks + sub[i];
}
printf("\nTotal Marks : %d out of 500, Aggregate percentage is %f", marks, marks / 5);
return 0;
}

But still .....same problem
OriginalGriff 2-Mar-17 14:12pm    
What you need is two loops: one inside the other.
The inner one asks for a number, validates it, and if it is correct exits the loop.
If it isn't (i.e. negative or not a number at all) it goes round again.
Once you have a correct value and have exited the loop, you need to include it in your total, and change the outer loop variable "i".

This is a lot easier to do than describe ... but it's your homework, not mine, so it's not fair to give you the code! :laugh:
As you have been told, the error comes from the array that is too small.
But if you look carefully, you will see that in fact you don't need an array at all, a variable is enough.
See what you do: you read a value, store on array, do something with it, then loop for next one.
And you never use again the values you saved in array.
All the interest of array is that you can reuse its values after filling the array.


To get rid of the goto, study the do ... while loop.

The debugger is a tool designed to gelp you tracking bugs, get used to it.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900