0

I was writing some code and every thing seemed alright. I carefully watched it run, but only to half way and then it stops, when it's time to give output from my given input.
But when I removes while loop and added do while loop it worked. After that I again used the while loop, i.e. exactly the code that was not running before, not changing anything, same to same code as before. But now it works.
I faced this problem 3-4 times.
I fixed all and even though my code was correct, sometimes it runs and sometimes it doesn't.
This wastes my time a lot. Why is this happening?

My pc is i5 7200u @ 2.5 ghz, 4 gb ram.

I notice that in Visual Studio Code the text looked like they move from its place while i am scrolling through. my code is this

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

struct node{
    int data;
    struct node *next;
};
struct node *head;

void insert(int a);
void print();

int main (){
    head = NULL;
     printf("How many numbers?\n");
      int x,n;
    scanf("%d",&x);
    for(int i=0 ;i<x ;i++){
    printf("enter no\n");
    scanf("%d",&n);
    insert(n);
    }
    print();
    
}

void insert(int a){
    struct node *temp=(struct node*)malloc(sizeof(struct node));
    (*temp).data=a;
    (*temp).next=head;
    head=temp;

}

void print(){
    struct node *temp=head;
/* i declearde this while i was doing do while loop*/ int i=0;
     printf("list is :");
    while(temp != NULL)  //only changed this with do while 
    {                    //and again replaced this same code i am 100% sure
        printf(" %d",(*temp).data);
        temp=(*temp).next;
    }
 /* do{
printf("list is :");
printf("%d",(*temp).data);
temp=(*temp).next;
i++
}while(i<3);//just to conform that I was right I set i and looped for 3 times 
//and then the code worked out and after this I tried while loop then also it 
worked out fine same code that was not working before, started working
*/
    printf("\n");
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Post the code, preferably a [minimal reproducilbe example](https://stackoverflow.com/help/minimal-reproducible-example) – anastaciu Jul 20 '20 at 16:23
  • 1
    @P__J__ For special values of "magic", i.e. Undefined Behaviour. – Yunnosch Jul 20 '20 at 16:33
  • You only show one version of code. Please show both. – Yunnosch Jul 21 '20 at 05:19
  • note that when it did'nt ran at first it asked the numbers for list and i entered but after entering numbers it would print "list is :" and then program crashed i needed to kill terminal.i use code runner may be its thats fault? – Coder Manish Jul 21 '20 at 10:04

1 Answers1

2

Two possible answers:

A) You are mistaken in "i again made it while loop,exactly the code that was not running before" and missed a tiny but relevant difference. If you do not happen to have used a versioning system for your code, this is my bet.

B) If for any reason your are 100% sure about "i again made it while loop,exactly the code that was not running before", then you probably have undefined behaviour in your code due to a mistake you made. Without seeing your code this cannot be analysed in more detail.
Undefined behaviour (see Undefined, unspecified and implementation-defined behavior) could explain any strange behaviour, including the one you describe.
With the shown code (sadly only one version of it), I can see a vulnerability to wrong input in combination with non-initiliased variables. I.e. you do not check the return value of scanf(), hence you do not notice when scanning fails. Then you use x befor it is initialised, whatever loop you use.
Gotcha: Undefined Behaviour.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • So it is probably B) then. – Yunnosch Jul 21 '20 at 05:27
  • so i need to initialize x before scanning ?and how to check scanning fails? – Coder Manish Jul 21 '20 at 10:08
  • 1
    As I said. "check the return value of scanf()". You are aware that it has a return value, aren't you? Otherwise read up on it, it is extremely useful for detecting input problems. – Yunnosch Jul 21 '20 at 10:10
  • 1
    Probably helpful: https://stackoverflow.com/questions/8186115/how-can-i-determine-if-scanf-read-what-was-specified-in-format – Yunnosch Jul 22 '20 at 12:50
  • Thanks it helped a lot. I am going to post another question please help me in that also!! – Coder Manish Jul 22 '20 at 14:33
  • i have upgraded my question i cant make this program whork please help! – Coder Manish Jul 22 '20 at 16:55
  • Well, you did not upgrade your question, you defaced it by completly changing it away from an existing answer (yes, mine). I have undone that. Please ask a new question. – Yunnosch Jul 22 '20 at 17:46
  • no i cant i have reached my limit for asking qn !! i am learning programming i don't know so i ask a lot of questions – Coder Manish Jul 23 '20 at 02:36
  • I only see one question on your account. I conclude that you might find this helpful: https://stackoverflow.com/help/question-bans – Yunnosch Jul 23 '20 at 04:44