Why this loop of searching in lnked list runs infinitely? - list

I am a beginner C programmer. I cant understand why the program goes into an infinite loop and print "The key element is not found". It may be a trivial error. Please help me.This program is to delete a node of a linked list
The program code is avalible here.
https://repl.it/#sandipanman/Linkeddel

Related

What does an exit value of -1 mean when compiling and how to debug it?

I am a C++ beginner. For some reason I cannot find a page with error codes and what they mean. Often my eclipse does this:
Does this -1 rather than 0 mean the same thing all the time? A segmentation fault perhaps? Or is it just a general "we didn't reach the end of the program" error?
Right now I put cout statements before and after every line until I find where it stops running. Is there somewhere specific I should look when seeing this -1?

My loop is not adding anything to my list

Within my program for my TI 83 Plus, I have a loop that is supposed to add random integers to my list, yet it is not working, here is my code:
0->I
While I<10
I+1->I
augment(⌞X,{RandInt(0,52))})
End
Disp (⌞X)
Pause
The program ends up just printing {0} Can anyone tell me where I'm going wrong?

C++ Unable to print the entire list in a that has already been created and read from a text file

Hello I have been having trouble with my program I've created a list and I have the following program
Its goal is to Read the 5 variables inside the text file 5 variables in 5 different inputs
so I manage to correctly show the list inside the while loop since every time a line is read it's printed untill .eof(End Of File)
My goal is that I am trying to print the list OUTSIDE of the while Loop that has already read and printed those 5 variables 10x the problem is that it repeats the last entered 5 Variables in the list
I and repeats that 10x (size of the list)
I've also tried something like this inside the for loop which I found in here:
int ID=it->ID;
string NAME=it->NAME;
int SEMESTER=it->SEMESTER;
string DIRECTION=it->DIRECTION;
double GRADE=it->GRADE;
As if those are nodes but I am a starter with nodes as well as lists and it seems to have failed
When I look at the code you've provided, I see that in your for loop the a_student is used, which is not updated by/with the iterator. Basically you're not looping over the created student list.
Maybe try using std::for_each (cppreference) it will save you a lot of time:)
Godbolt example: https://godbolt.org/z/fQUhcr
The issue on the left column of code is that you are not de-referencing the iterator. You never set the a_students variable inside the loop, so why do you expect it to change on each iteration?
Write something like:
a_students = *it;
inside the loop. However there is a simpler way. Instead of manually handling the iterators, use:
for (auto a_students: s_stl_list)
{
// do something with each "a_students" which will be AUTOmatically the right type
}

do-while doesn't quit on first 'q'

So I'm working on a simple little text-based game in D to gain some experience working with the language. Here is a do-while loop that I'm currently struggling with:
do{
writeln("a. Advance 1 year\tc. Advance 10 years\tq. Quit");
writeln("b. Advance 5 years\td. Modify faction");
input = chomp(stdin.readln());
switch(input){
...
default:
break;
}
writeln(input[0]);
}while(input[0] != 'q');
Now the problem I'm running into is that when I hit q and enter the loop doesn't exit. It just keeps going. But then after the first time q is input, another q will terminate the loop. The writeln is in there as a sanity check, and it prints out the characters I type in exactly as typed. I feel like I'm going crazy, but it's probably just a simple type-o or something you guys will spot instantly. Nothing in the switch statement modifies 'input'.
EDIT: Okay some people have been asking to see all of the code. Here it is: http://pastebin.com/A7qM5nGW
When I said nothing in the switch modified input, it was to hide the fact I hadn't written anything in the switch yet. I've been trying to get the quit part to work right before adding the more complicated stuff. Also, here's a sample file for what I run it on: http://pastebin.com/4c2f4Z5N
Okay my friend found it. It has nothing to do with the while loop itself. I briefly forgot that args[0] is the name of the program. So it's actually running through the parent loop once with nothing, then actually quitting, and then running through the appropriate loop. It was fixed by making the parent loop like so...
foreach(filename; args[1..$]){
...
do{
...
while(input[0] != 'q');
}
as opposed to:
foreach(filename; args){
etc...

Segmentation fault when not deleting files C++

I have a program written in C++ that uses a lot the "system" function to run things like i would run in Shell (using Ubuntu 12.04 32 bits).
The things done in the "system" function generate several files that will be processed. In a second part of the program i process those files.
When the program ends, it doesn't delete those files that was generated by the "system" functions.
When i run the program for the first time it runs fine, when i run in a second time it gives me Segmentation fault (core dumped) in a given loop of the second part. It does less iterations each time i try to run. For example : First run = ran fine. Second run = Segmentation fault(core dumped) in iteration 200. Third run = Segmentation fault(core dumped) in iteration 199. And goes on...
I resolved the problem deleting all files it had generated by "system" function in the end of each "run" of the program.
My question is...any one has any clue about why was it happening?
EDIT*
Below is a few pseudocode to exemplify.....unfortunately i cant put the entire code, its like 600 lines.
int main(void){
// Part 1 of the program.....severam "system" producing files
system("bash produceSeveralFiles.sh");
// Part 2 of the program......process files produced in part 1
processFiles();
// My Solution that i dont know why solves it
system("rm AllFiles");
return 0;
}
EDIT*
Way i checked that it goes until the loop...
for(int i = 0; i < 300 ; i++){
printf("\n%d",i);
...//a few code here
printf("\n%d",i);
}
Second run i see "i" goes until 200....third run i see "i" goes until 199....and go on...thats the way i checked where it was giving the Segmentation Core (dumped).
EDIT*
O well im ashamed now, just checked the old Script that was generating the files before the program and it gives the same problem....if i do the generation in a separated file and then only runs the "processFiles()" in the program, it gives the same error. The old Script has an "rm files" command in the end that wasnt concerned about. (thx to user #beta....just checked the old script cause your commentary)
Well....but anyway the question continue, but now i know its not only with the "system" function.