Window instantly closing when opening compiled .exe in C - c

I have tried so many things. Running from command line, running from cmd, running with /K, putting system("pause"); getchar(); getch(); before return 0 and I simply can't get it to run. I'm writing in Notepad++, compiling in Cygwin and the window appears blank for the split second it appears (according to my screenshot, it could have been taken too early). Basically I've tried anything I could Google myself to. So I figured it must be something wrong with my code that the debugger doesn't show.
#include <stdio.h>
int main()
{
float lt1, lt2, dmg, x;
lt1=10;
lt2=30;
while(lt2>dmg)
{
while(x>0 || lt2>dmg)
{
dmg=dmg+x*lt1;
x--;
return (dmg);
}
x=x+0.01;
return (x);
}
printf("Horde factor is: %f", x);
return 0;
}
I would appreciate any help I can get, and I hope you will bear over with my inexperience.

You have undefined behavior in your code.
When you declare a local variable without assigning anything to it, its value is indeterminate. Usage of this variable will be undefined behavior until you assign a value to it.
In this case it's the dmg and x variables that causes this problem.

Its because of these statement :
return (dmg); //this ends the code execution .. because you have returned something from main()
x=x+0.01;
return (x); // even this one is wrong
you are exiting the code there and never getting to the printf ..
there should only be one return in main() .. and at the end.
More problems with your code:
you don't initialise dmg and x , but you use them as parameters for while loop
float lt1, lt2, dmg, x; // dmg,x uninitialized
In the outer while loop .. its an infinite loop as you don't do anything to the parameters of that loop to get out of it.
Like I said above .. there should be only 1 return in main()
Maybe instead of returning you should look into break; ( i don't know if thats what you want or not as I don't understand your code )

Related

while loop has terminating sign still works well

I was implementing Newton Raphson method in C. The code work well. There is no error in the code.
#include<stdio.h>
#include<math.h>
#define f(x)(x * sin(x)+cos(x))
#define df(x)(x*cos(x))
int main()
{
float x,h,e;
e=0.0001;
printf("Enter the initial value of x:\n");
scanf("%f",&x);
do
{
h=-f(x)/df(x);
x=x+h;
}
while(fabs(h)>e);
printf("The value of the root is=%f",x);
return(0);
}
/*
Output:
Enter the initial value of x: 3
The value of the root is = 2.798386
However, I was surprised I mean how did this code work? As per c rule while statement does not have any terminating semicolon. However, in my code while(fabs(h)>e); has a semicolon yet it run well.
Can anyone tells me how does it work?
What you mean is putting
while(...);
{
//some code
}
that will be interpreted as
while(...){
//looping without any instruction (probably an infinite loop)
}
{
//some code that will be executed once if the loop exits
}
do-while loop has the code executed before the condition (so at least once differently from simple while loop). The correct syntax has a semicolumn:
do{
//your code to be executed at least once
}while(...);
So the answer to your question is that :
do
{
h=-f(x)/df(x);
x=x+h;
}
while(fabs(h)>e);
is not a while statement, it's a do-while statement.

C - a certain function in my program wont work with either returning an integer nor using a pointer

This is my first time posting of this forum and I'm doing is just because of this problem. I've been working on a program for a while(just for fun) and to make things simple for my self I used loads of global variables, but now I've been trying to make the individual functions more independent and flexible. A certain function is giving a a lot of issues for some reason.
int which_move(int ac,int bc,int cc){
int illcheck;
int ill_done;
int ill_pos;
int true_move;
true_move=3;
ill_done=-1;
for(u=6;u>=0;u--){
ill_pos=ert-1;
illcheck=0;
for(y=0;y<ill_len[u];y++){
if(buff[ill_pos]==ill_move[u][y]){
++illcheck;
if(ill_pos==0)
ill_pos=100;
--ill_pos;
if(illcheck==ill_len[u]){
ill_done=u;
break;
}
}
else
break;
}
if(ill_done!=-1)
break;
}
if(ac==1||ill_done==1||ill_done==2||ill_done==6)
true_move=0;
if(bc>2)
true_move=1;
if(cc>2)
true_move=2;
if(ill_done==0||ill_done==3||ill_done==4)
true_move=4;
if(ill_done==5)
true_move=5;
return true_move;
}
and this is how i call the function:
int open_move;
open_move=which_move(acheck,bcheck,ccheck);
and open_move never match true_move.
I've tried to convert to something like this
int which_move(int *true_move,int ac,int bc,int cc)
and remove int true_move; and the return of return true_move; and implement the function like this:
int open_move;
which_move(open_move,acheck,bcheck,ccheck);
still i get it to work.
I've googled til chrome starts lagging because of too many tabs open and tried every trick I can find, but I'm not getting any wiser. Please help me with what I'm doing wrong.
Thanks from a hobbyist.

Codeblocks C Debugging

I've got a new challenge to return the factorial of a number. Got ideas on how to do this, but the challenger has given some starting code - which is shown below.
Now this isn't how I would have started it (with my extremely limited experience!) - BUT I wasn't sure how system would grab some text & place within an int array - hence I tried running it within codeblocks, debugging and looking at the watch table. However I can't see 'num'.
So I tried copying num to num1:
int num1[30] = {0};
memset(num1[0],num[0], sizeof(num));
that doesn't seem to affect anything...
So question really is - is there something wrong with my codeblocks config (it debugs other programs and I've tried both cygwin & MiniGW) or is there another reason for this behavious?
#include <stdio.h>
#include <string.h>
void FirstFactorial(int num[]) {
// code goes here
printf("%d", num);
}
int main(void) {
// keep this function call here
FirstFactorial(gets(stdin));
return 0;
}

Assign a value to external variable from function

I've been reading here a lot but never posted until now.
My problem is that I'm stuck with some code. What I'm trying to do is receive a value through UART from Matlab and assign to a single variable that is gonna stick through the entire program.
This is the test code I'm running:
void start_comm(){
//Stuck in loop untill Matlab gives signal
// Spams character 'A' while waiting
while (!uart_is_rx_ready (CONF_UART)){
printf("%c\n",'A');
delay_ms(100);
}
// Start reading data sent from Matlab
// P,I,D & samplingstime data
uint8_t p_char, i_char, d_char, samp_char1, samp_char2;
while (!uart_is_rx_ready (CONF_UART)){};
uart_read(CONF_UART, &p_char);
// Print out everything out again for testing
printf("%c\n", p_char);
}
This code works, everything prints out fine. What I need is to be able to use the value in p_char in other functions and I need it to be the same value as the one sent from Matlab i.e. if it's 5 then I could printf in another function and it would print a 5.
I've tried return p_char to a different variable but it would just revert to 0 at the start of the loop. I've also tried the following test code where I try to set the variable as static:
**file1.h**
extern int a;
**file1.c**
#include file1.h
void function(){
static int a;
scanf("%i", &a);
}
**main.c**
#include file1.h
int main() {
function();
while(1){
printf("%i", a);
}
}
Looking over the code, I'm pretty sure I'm doing something wrong with the static and extern, but I'm lost.
EDIT: Figured out the problem, it was indeed Matlab code. I needed to add a delay to it to account for the time it took to communicate with the microcontroller.
Update your file1.c to read:
#include file1.h
int a;
void function(){
scanf("%i", &a);
}
This puts a in the global scope. If you keep extern int a in your .h file, C files that include that header will know about it.

Console application crashes for unknown reason

I have my piece of code which is shown below
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int a;
printf("Please select a choice \n1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist");
scanf("%d,&a");
if(a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}
On running the code, it displays the menu and if i input 1, it takes a few seconds before it crashes and displays the info
document1.exe has stopped working
How can I debug this problem. I am using dev c++ 4.9.9.2
Your scanf statement is wrong. You are not passing argument (pointer) to it.
Change
scanf("%d,&a");
to
scanf("%d",&a);
For a C solution - I am not certain
aside from previously mentioned
error in scanf formatting (which your compiler should have warned about),
which would have put the result.. well, god knows where on stackā€¦
I'm not certain, but I suspect the compiled code would take the next location in ram as an address, and write there.
Borked stack == really, really, really hard to debug errors.
Since you are using a c++ compiler, if you can use c++, you may wish to consider evaluation of using some of the STL here.
I assume you will be terminating input with a CR
int main (int argv, char** argv]
{
int a;
std::string inputString;
std::cout <<"Please select a choice \n""1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist";
std::getline(std::cin,inputString);
std::stringstream inputStream(inputString);
inputStream >> a; // could also have been parsed with std::stol, or strtol. - my preference due to error checking - what if your user entered 'byte me'?
if (a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}

Resources