Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>
    int main(){ 
int marks[4];
int i;
for ( i = 0; i < 5; ++i)// Even after mentioning i<5 why it is printing values till 5 it should stop at 0 and why I am not able to pass input by user properly 
{
   printf("Enter the  value of  %d: \n",i+1);
    scanf("%d\n", &marks[0]);
}

    

return 0;
}


What I have tried:

Even after mentioning i<5 why it is printing values till 5 it should stop at 0 and why I am not able to pass input by user properly
Posted
Updated 1-Jun-22 1:15am
Comments
0x01AA 1-Jun-22 6:54am    
Why? Because you print i+1 and because you read in allways to marks[0] ....
Greg Utas 1-Jun-22 9:56am    
You're going to have a problem when you change this. You define marks[4] but then check i < 5. This will lead to using marks[4], which is out of bounds. The index for an array defined with [n] goes from 0 to n-1.

Even id this code does nothing, I would try to replace
C++
<pre>scanf("%d\n", &marks[0]);

with
C++
<pre>scanf("%d\n", &marks[i]);


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
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 know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
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.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
den2k88 1-Jun-22 9:36am    
Sadly no university ever teaches how to use a debugger. A part of this comes from the common usage of plain gcc, with gdb being a steaming pile of youknowwhat to use, and a part from the teacher being teachers and never having worked in real development.

Basically, I had to teach myself the use of a debugger well after graduation, when I started my first job - I used to debug image analysis algorithms with printfs... that's the sorry state of CS education nowadays.
jeron1 1-Jun-22 13:06pm    
LOL

"I used to debug image analysis algorithms with printfs..."

You were not alone.

The bad old days.
Patrice T 1-Jun-22 12:46pm    
I think this is a reply to previous comment.
You should use the "Reply" buttom on right of member nickname on top of comment.
jeron1 1-Jun-22 13:05pm    
Yes, it should have been, my apologies.
Patrice T 1-Jun-22 13:15pm    
:) I did the same mistake a couple times myself .
 
Share this answer
 
Comments
merano99 1-Jun-22 17:55pm    
Apparently the questioner is not interested in a working solution to his question. I'll give you another 5
Richard MacCutchan 2-Jun-22 3:58am    
The sad thing is that far too many of these questions are from people who really have no ability in this field. They just believe it is a shortcut to a well paid job.

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