0

i wrote following program but it is not right. would you please assist me where i went wrong?

#include<stdio.h>
void main()
{ 
    int count=0,a;
    do
    {
       scanf("%d",&a);
       if(a>0 )
       {
         for(count=0;count<15;count++);
           else;
       }
       while(int>=0);
    }

So would you tell me the correct code?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 1
    What makes you think it is not right? Crash? Hang? Misbehaviour? Compiler errror? – Yunnosch May 09 '18 at 04:32
  • 2
    I formatted and indented your program for you. Look at it. I think the problems are obvious now. Or maybe not, did you double check that the code you show here is identical to the code you have? Please copy paste instead of typing again. I cannot believe that this code even compiles. What are the errors your compiler tells you? Did you copmile strictly, e.g. at least with `gcc -Wall`? Please edit the code until it at least compiles without errors. Better without errors and warnings. Ideally without errors or strict warnings. – Yunnosch May 09 '18 at 04:37
  • The code after your edit still looks like: broken snytax for `if-else`, `do-while` and `main`. Also the newly added `for` loop does not have an effect. Would you like to base your code on working example from tutorials? Start with a HelloWorld. Add in a correct `do-while`. Insert a working `if-else`. Insert a working `for`. Add to place the code you want. – Yunnosch May 09 '18 at 04:42
  • so would you tell me the correct code sir. i am newbie in programming – Rishi Srivastava May 09 '18 at 04:49
  • I provided the syntax elements, which you really should have picked up from a tutorial or book. Now the actual problem of your code is the question. What is not working? You did not ask a question here about the syntax of random constructs, instead you are trying to achieve a certain behaviour, aren't you? Talk about that, after you edited your code to actually be what you have at your place. Make a [mcve]. Double check that the code you post here acts as the one at home. – Yunnosch May 09 '18 at 04:54
  • there was no any syntax misstake when i compiled – Rishi Srivastava May 09 '18 at 04:55
  • 1
    Which means that the code you compiled is very different from the one you show here. Please make sure they are identical. Do not type again. Copy paste after turning the code you compile into a MCVE and compiled again. – Yunnosch May 09 '18 at 04:57
  • What makes you think it is not right? Crash? Hang? Misbehaviour? Compiler errror? – Yunnosch May 09 '18 at 04:57
  • i wrote the same code which is written here but the problem is program should read only integer until user enter negative integer or number of positive integer reaches up to 15 – Rishi Srivastava May 09 '18 at 05:02
  • 1
    If the code you show here is identical to the code you compile successfully at home, then you have a - lets say - extremly interesting compiler. Please double check that the code here and at home are identical and also please double check that you really compile the code you are looking at and not accidentally any other code in your file system. – Yunnosch May 09 '18 at 05:04
  • i wrote for(count=0;count<15;count++); when i use ; for is supposed to end . there is no need of { and }. same happens with else statement . so my syntax was right. – Rishi Srivastava May 09 '18 at 05:10
  • 1
    That is at least correct syntax, but it does not do anything. – Yunnosch May 09 '18 at 05:10
  • What makes you think it is not right? Crash? Hang? Misbehaviour? Compiler errror? – Yunnosch May 09 '18 at 05:10
  • 1
    Please take the [tour] again, read [ask] and study and apply the concept of making a [mcve]. Also have a look at this checklist: https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/ – Yunnosch May 09 '18 at 05:12
  • 1
    Alternatively, if you code is as short as what you showed here, [edit] your question, delete the code which is there, copy the code you compile at home and paste it again. I will even take care of correct formatting for you. – Yunnosch May 09 '18 at 05:15
  • 1
    The shown code cannot be compiled without errors, at least for the misplaced `else;` and the botched loop condition. It also contains an endless loop and a useless loop and (most harmless of all) you seem to have forgotten the last closing `}`. I pay your dinner for a year (if you come here) if that code is actually compiled without errors. Please, please double check that the shown code is identical to the code you compile successfully. – Yunnosch May 09 '18 at 05:35

1 Answers1

0

Please use building blocks of correct syntax:

main:

#include <stdio.h>
int main(void)
{ 
    /* other code */
    return 0;
}

do-while:

do
{
    /* other code */
} while(count>=0);

if:

if(a>0 )
{
    /* code if true */
} else
{
    /* code if false */
}

for:

for(a=0;a<15;a++)
{
    /* repeated code */
}

For getting input right, please read:

http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
How to read / parse input in C? The FAQ

When the syntax is right and the input is done correctly, then we can discuss the behaviour of your code; which I think is what you are actually asking about. Though what you are describing might already be solved/achieved once you spend some work on how to get input properly, based on the links above.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54